config-server
application.properties
server.port=8005
spring.application.name=config-server
###git倉庫地址
spring.cloud.config.server.git.uri= https://github.com/github-ygy/spring_cloud_test
####git 相對目錄地址 多個,分開
spring.cloud.config.server.git.search-paths=cloud-config-repository
###github public 不用配置username 和 password
#spring.cloud.config.server.git.username =
#spring.cloud.config.server.git.password=
application
@SpringBootApplication
@EnableConfigServer
public class ConfigMasterApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigMasterApplication.class, args);
}
}
輸出
http:
{
"name": "test",
"profiles": [
"prd"
],
"label": null,
"version": "16610aaf21ae2e0934ec3522009ef993829ec086",
"state": null,
"propertySources": [
{
"name": "https://github.com/github-ygy/spring_cloud_test/cloud-config-repository/test-prd.properties",
"source": {
"from": "git-prd-1.0"
}
}
]
}
http:
{
"name": "test",
"profiles": [
"prd"
],
"label": "dev_ygy",
"version": "0978a00b06877b63a89653bea4ec0ac8df779df9",
"state": null,
"propertySources": [
{
"name": "https://github.com/github-ygy/spring_cloud_test/cloud-config-repository/test-prd.properties",
"source": {
"from": "git-prd-2.0"
}
}
]
}
config-client
bootstrap.properties
#######必須使用bootstrap.properties 使用application.properties 將無法解析@Value屬性
#######bootstrap.propeties 會優先於application.properties加載
server.port=8006
####配置中心git倉庫 文件名 與<application> 對應
spring.application.name = test
#####配置中心git倉庫 文件名 啓動配置文件標識 與<profile> 對應
spring.cloud.config.profile = dev
####配置中心git倉庫分支名 與<lable> 對應
spring.cloud.config.label=dev_ygy
####配置中心config server地址
spring.cloud.config.uri=http://localhost:8005/
controller
@RestController
public class ConfigClientController {
@Value("${from}")
private String from ;
@RequestMapping("/getConfig")
public String getConfig() {
return this.from;
}
}
輸出
http:
git-dev-2.0
config-eureka(整合)
失敗響應機制/自動刷新
依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
bootstrap.properties
spring.application.name = config-client
#######必須使用bootstrap.properties 使用application.properties 將無法解析@Value屬性
#######bootstrap.propeties 會優先於application.properties加載
server.port=8006
###eureka管理
eureka.client.serviceUrl.defaultZone=http://pear1:9998/eureka/
spring.cloud.config.discovery.enabled = true
### 失敗響應
spring.cloud.config.fail-fast=true
####引入aop retry依賴
####最大重試次數 默認6次
spring.cloud.config.retry.max-attempts= 6
####最大重試時間間隔
spring.cloud.config.retry.max-interval= 2000
#####初始重試間隔 默認1000
spring.cloud.config.retry.initial-interval = 1500
##### 下一次間隔係數
spring.cloud.config.retry.multiplier= 1.2
####開啓自動刷新 引入actuator 依賴
#### 感覺有bug 待後續解決 屬性註冊不成功,將config-server 項目註冊爲默認 configserver可行。
#spring.cloud.config.discovery.serviceid = config-server
####關閉授權
management.security.enabled=false
####配置中心git倉庫 文件名 與<application> 對應
spring.cloud.config.name = test
#####配置中心git倉庫 文件名 啓動配置文件標識 與<profile> 對應
spring.cloud.config.profile = dev
####配置中心git倉庫分支名 與<lable> 對應
spring.cloud.config.label=dev_ygy
####配置中心config server地址
###spring.cloud.config.uri=http://localhost:8005/
controller
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${from}")
private String from ;
@RequestMapping("/getConfig")
public String getConfig() {
return this.from;
}
}
application
@SpringBootApplication
@EnableDiscoveryClient
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
post請求刷新
public class PostRquest {
public static void main(String[] args) throws Exception {
HttpResponse<String> jsonResponse = Unirest.post("http://localhost:8006/refresh")
.asString();
System.out.println(jsonResponse.getBody());
}
}
輸出
git-dev-2.0
["config.client.version","from"]
git-dev-2.0-refresh