talk is cheep, show your the code
廢話不多說,直接開干
本文基於 2.1.1版本
github地址
例如我們目前將配置放在了github上的這個目錄下
Server端
pom.xml
需要添加一個依賴,一般我們配置中心和注冊中心可以放在一起,因此我就和eureka注冊服務器一起了
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
server的application.yml
導入了jar包,按照spring的尿性,當然是要繼續寫配置信息了
server:
port: 8761
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: service-center
cloud:
config:
server:
git:
uri: # 你的git地址
# git賬號
username: '你的賬號'
# git密碼
password: '你的密碼'
# 默認從master分支讀取配置文件
default-label: develop # for test
# 配置文件的路徑,因為我項目的根目錄是 my-demo,因此這里查詢的路徑就是 m.../conf-repo, 可以配置多個,按逗號分割
search-paths: microservice/conf-repo #
配置文件
關於配置文件,必須按照約定的命名, 就是 {application}-{profile}.properties
或 {application}-{profile}.yml
,不這么命名的話,是沒法后面被加載到的
例如我創建的文件名為 demo-dev.properties,里面的內容為
version=1
name=demo
測試Server
spring cloud config會將文件生成對應的接口,
明顯看出接口是按照 {application}/{profile} 這樣的方式來自動生成的
官方文檔表明有以下幾種
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
client端
pom.xml
需要導入一個jar包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
添加配置文件,不是application.yml
spring cloud config 通過bootstrap.yml 中的內容來對配置文件進行讀取
spring:
cloud:
config:
discovery:
enabled: true
# 對應配置中心的 spring.application.name
service-id: service-center
# name 表示配置文件的 {application} 部分
name: demo
# profile 表示配置文件的 {profile}部分
profile: dev
# label 表示 git上的分支
label: develop
# uri 表示 訪問配置中心的uri
uri: http://localhost:8761/
測試的controller
@RestController
public class ConfigClientController {
@Value("${version}")
private String v;
@GetMapping("/get")
public String get(){
return this.v;
}
}
測試client
啟動,然后發送http請求 http://localhost:8989/get
讀取多個配置文件
我們的配置文件可能需要讀取的不止一個,那么要怎么配置呢,查看了官方文檔以后,配置的方式如下
我們添加一個屬性 demo-prod.properties
spring:
profiles:
active: dev,prod # 添加這個屬性可以讀取多個文件
cloud:
config:
discovery:
enabled: true
service-id: service-center
# name 表示配置文件的 {application} 部分
name: demo
# profile 表示配置文件的 {profile}部分
# profile: dev
# label 表示 git上的分支
label: develop
# uri 表示 訪問配置中心的uri
uri: http://localhost:8761/
相應的,controller的代碼也需要進行修改
@RestController
public class ConfigClientController {
@Value("${version}")
private String v;
@Value("${namespace}")
private String ns;
@GetMapping("/get")
public String get(){
return this.v;
}
@GetMapping("/namespace")
public String getNs(){
return this.ns; // 應該得到 demo
}
}
配置更新
spring cloud config 能夠監聽github上配置的變化,但它不會進行進行變化的通知,而是需要client自己主動的進行pull的操作,將配置更新
這里就需要引入 spring-cloud-starter-actuator
了。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
在 application.yml
中添加配置屬性,將更新的接口暴露出來
management:
endpoints:
web:
exposure:
include: refresh
在我們希望能夠進行配置更新的地方,添加注解 @RefreshScope
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${version}")
private String v;
@Value("${namespace}")
private String ns;
@GetMapping("/get")
public String get(){
return this.v;
}
@GetMapping("/namespace")
public String getNs(){
return this.ns;
}
}
到此,我們的准備工作就好了,然后啟動,訪問/get
接口,然后修改配置文件,再訪問的時候還是舊的值,這個時候使用工具進行post的調用
刷新結束后,再次訪問
在github上可以通過配置webhook的方式,當有新的push操作時,讓github去調用 client的refresh接口
這個方案在client數量較少是還可以,但數量一多,並且每次新增機器都需要修改webhook,顯然不夠智能,因此后面就需要考慮使用 spring-cloud-bus來進行解決了