您好,登錄后才能下訂單哦!
這篇文章主要介紹“構(gòu)建Spring Cloud配置服務(wù)器的步驟”,在日常操作中,相信很多人在構(gòu)建Spring Cloud配置服務(wù)器的步驟問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”構(gòu)建Spring Cloud配置服務(wù)器的步驟”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
實(shí)現(xiàn)步驟:
1. 在Configuration Class標(biāo)記@EnableConfigServer
2. 配置文件目錄(基于git)
cloud.properties(默認(rèn)) //默認(rèn)環(huán)境,跟隨代碼倉(cāng)庫(kù)
cloud-dev.properties(proflie="dev")//開(kāi)發(fā)環(huán)境
cloud-test.properties(proflie="test")//測(cè)試環(huán)境
cloud-staging.properties(proflie="staging")//預(yù)發(fā)布環(huán)境
cloud-prod.properties(proflie="prod")//生產(chǎn)環(huán)境
3. 服務(wù)端配置版本倉(cāng)庫(kù)
spring.cloud.config.server.git.uri=https://github.com/****test****/springcloud.git
spring.cloud.config.server.git.searchPaths=properties
spring.cloud.config.label=master
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=
完整配置項(xiàng):
spring.application.name = config-server
server.port=9090
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
spring.cloud.config.server.git.uri=https://github.com/****test****/springcloud.git
spring.cloud.config.server.git.searchPaths=properties
spring.cloud.config.label=master
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=
構(gòu)建Spring Cloud配置客戶端
實(shí)現(xiàn)步驟:
1. 創(chuàng)建 bootstrap.properties 或者 bootstrap.yml 文件
2. bootstrap.properties 或者 bootstrap.yml 文件中配置客戶端信息(以下是bootstrap.properties )
##配置服務(wù)器URI
spring.cloud.config.uri = http://localhost:9090/
##配置客戶端應(yīng)用名稱
spring.cloud.config.name = cloud
##激活配置
spring.cloud.config.profile = prod
##在github上的分支名
spring.cloud.config.label = master
3. 設(shè)置敏感性安全
management.endpoints.web.exposure.include=*
4. 設(shè)置健康檢查
management.endpoint.health.show-details=always
@RefreshScope用法
@RestController
@RefreshScope
public class EchoController {
@Value("${my.name}")
private String myName;
@GetMapping("/my-name")
public String getName() {
return myName;
}
}
用戶調(diào)用/actuator/refresh控制客戶端配置更新
實(shí)現(xiàn)定時(shí)更新客戶端
@SpringBootApplication
@EnableScheduling
public class SpringCloudConfigServerClientApplication {
private final ContextRefresher contextRefresher;
private final Environment environment;
@Autowired
public SpringCloudConfigServerClientApplication(ContextRefresher contextRefresher,
Environment environment) {
this.contextRefresher = contextRefresher;
this.environment = environment;
}無(wú)錫人流醫(yī)院 http://www.wxbhffk.com/
public static void main(String[] args) {
SpringApplication.run(SpringCloudConfigServerClientApplication.class, args);
}
@Scheduled(fixedRate = 5*1000, initialDelay = 3*1000)
public void autoRefresh() {
Set updatePropertyNames = contextRefresher.refresh();
updatePropertyNames.forEach(name -> System.err.println(">>>>>>"+name+environment.getProperty(name)));
if(!updatePropertyNames.isEmpty()) {
System.err.printf("[Thread :%s] 當(dāng)前配置已更新,具體項(xiàng)目:%s \n",
Thread.currentThread().getName(),
updatePropertyNames);
}
}
}
健康檢查
意義
比如應(yīng)用可以任意地輸出業(yè)務(wù)健康、系統(tǒng)健康等指標(biāo)
端點(diǎn)URI:/actuator/health
實(shí)現(xiàn)類:HealthEndpoint
健康指示器:HealthIndicator,
HealthEndpoint:HealthIndicator,一對(duì)多
自定義實(shí)現(xiàn)HealthIndicator
1. 實(shí)現(xiàn)AbstractHealthIndicator
public class MyHealthIndicator extends AbstracHealthIndicator{
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception{
builder.up().withDetail("MyHealthIndicator",">>Up>>");
}
}
2. 暴露MyHealthIndicator為bean
@Bean
public MyHealthIndicator myHealthIndicator(){
return new MyHealthIndicator();
}
3. 依賴
org.springframework.hateoas
spring-hateoas
到此,關(guān)于“構(gòu)建Spring Cloud配置服務(wù)器的步驟”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。