Spring Cloud Config統一管理服務配置
一、為什麼要統一管理微服務配置
對於傳統的單體應用而言,常使用配置檔案來管理所有配置,比如SpringBoot的application.yml檔案,但是在微服務架構中全部手動修改的話很麻煩而且不易維護。
微服務的配置管理一般有以下需求:
1.集中配置管理,一個微服務架構中可能有成百上千個微服務,所以集中配置管理是很重要的。
2.不同環境不同配置,比如資料來源配置在不同環境(開發,生產,測試)中是不同的。
3.執行期間可動態調整。
4.配置修改後可自動更新。
好在Spring Cloud Config已經全部實現了上面幾點。
二、Spring Cloud Config簡介和使用
2.1原理
Spring Cloud Config為分散式系統外部化配置提供了伺服器端和客戶端的支援,它包括Config Client 和 Config Server兩個部分。原理是所有的配置資訊都儲存在Config Server,所有的微服務都指向Config Server,
各個微服務啟動時都會請求Config Server來獲取配置資訊,然後快取到本地以提高效能。
2.2編寫Config Server
1.在Git倉庫https://gitee.com/StarskyBoy/cloud-config-repo
(可以使用自己的倉庫、本地檔案系統等)新建幾個配置檔案,例如:
內容分別為(在測試中可能做了些修改):
profile=profile: dev-1.0-change16
profile=production-1.0
profile=test-1.0
profile=default-1.0
2.在cloud專案中建立cloud-config-server微服務,並新增以下依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
3.在啟動類新增 @EnableConfigServer註解
4.編寫application.yml檔案
server:
port: 8050
spring:
application:
name: cloud-config-server
cloud:
config:
server:
git:
#git地址
uri: https://gitee.com/StarskyBoy/cloud-config-repo
#git使用者名稱
username: ******
#git密碼
password: ******
這樣就完成了,可以使用端點來獲取配置檔案,端點與配置檔案的對映規則如下:
/{application}/{profile}[/{lable}]
/{application}-{profile}.yml
/{lable}/{application}-{profile}.yml
/{application}-{profile}.properties
/{lable}/{application}-{profile}.properties
{application}表示微服務的名稱,{profile}代表環境,{lable}表示Git倉庫的分支,預設是master。
本例如果要訪問cloud-foo-dev.properties,則可以訪問這些URL:
http://localhost:8050/cloud-foo/dev
http://localhost:8050/cloud-foo-dev.properties
http://localhost:8050/cloud-foo-dev.yml
備註:
原始碼見cloud專案cloud-config-server微服務。
2.3編寫Config Client
1.建立cloud-config-client微服務,並新增以下依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!--健康監控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--web模組-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--使用Spring Cloud Bus自動重新整理配置,取消註釋-->
<!--<dependency>-->
<!--<groupId>org.springframework.cloud</groupId>-->
<!--<artifactId>spring-cloud-starter-bus-amqp</artifactId>-->
<!--</dependency>-->
2.編寫配置檔案bootstrap.yml
server:
port: 8051
spring:
application:
#對應Config Server所獲取的配置檔案的{application}
name: cloud-foo
cloud:
config:
#指定Config Server的地址,預設是http://127.0.0.1:8888
#本專案,此處埠注意切換
#8050埠是普通Config Server
#8052埠是bus refresh
#8053埠是authenticating
uri: http://192.168.43.171:8053/
#對應Config Server所獲取的配置檔案的{profile}
profile: dev
#指定Git倉庫的分支,對應Config Server所獲取的配置檔案的{label}
label: master
#啟動使用者認證,取消註釋
# username: midou
# password: midouJava
#使用Spring Cloud Bus自動重新整理配置,取消註釋
# rabbitmq:
# host: 192.168.43.37
# port: 5672
# username: admin
# password: admin
#關閉安全認證
management:
security:
enabled: false
#值得注意的是,以上屬性配置在bootstrap.yml,而不是application.yml中,否則部分配置就不能正常工作。。
4.編寫Controller
@RestController
//是否使用/refresh端點重新整理配置
//@RefreshScope
public class ConfigClientController {
@Value("${profile}")
private String profile;
@GetMapping("/profile")
public String hello() {
return this.profile;
}
}
這裡通過註解 @Value("${profile}") 來繫結Git倉庫的profile屬性。
5.測試
先啟動cloud-config-server,再啟動cloud-config-client,訪問http://localhost:8050/profile即可獲得以下結果。
dev-1.0-change16
說明能夠正常的獲取Git倉庫的配置資訊。
備註:
原始碼見cloud專案cloud-config-client微服務。
三、配置檔案的手動重新整理和自動重新整理
3.1通過/refresh端點手動重新整理
1.為cloud-config-client微服務新增spring-boot-starter-actuator依賴,如果有了就不添加了。
2.啟用Controller類上@RefreshScope註解
3.修改Git倉庫中cloud-foo-dev.properties檔案的內容,然後傳送POST請求到http://localhost:8051/refresh,再訪問http://localhost:8051/profile即可獲取最新的配置。
備註
原始碼見cloud專案cloud-config-client微服務。
發POST可用curl命令或者Postman
3.2使用Spring Cloud Bus 實現自動重新整理配置
這裡是配置Config Server,你也可以配置Config Client,原理是差不多的,cloud專案就不做配置了,具體可參考Config Server實現。
1.首先安裝RabbitMQ,安裝步驟這裡不介紹我的csdn部落格裡有。
2.複製cloud-config-server微服務修改為cloud-config-server-refresh-cloud-bus微服務,為專案新增以下依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
3.在application.yml中新增以下內容
server:
port: 8052
spring:
application:
name: cloud-config-server-refresh-cloud-bus
cloud:
config:
server:
git:
#git地址
uri: https://gitee.com/StarskyBoy/cloud-config-repo
#git使用者名稱
username: ******
#git密碼
password: ******
bus:
trace:
enabled: true
rabbitmq:
host: 192.168.43.37
port: 5672
username: admin
password: admin
management:
security:
enabled: false
4.在cloud-config-client微服務中,新增以下依賴及配置
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
rabbitmq:
host: 192.168.43.37
port: 5672
username: admin
password: admin
5.cloud-config-client微服務中,controller中的@RefreshScope註解依舊開啟。
6.修改Git倉庫中cloud-foo-dev.properties檔案的內容,然後傳送POST請求到http://localhost:8052/bus/refresh,再訪問http://localhost:8052/profile即可獲取最新的配置。
備註
原始碼見cloud專案cloud-config-server-refresh-cloud-bus微服務。
發POST可用curl命令或者Postman
四、Config的使用者認證
1.複製cloud-config-server微服務修改為cloud-config-server-authenticating微服務。
2.新增以下依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
3.配置application.yml
server:
port: 8053
spring:
application:
name: cloud-config-server-authenticating
cloud:
config:
server:
git:
#git地址
uri: https://gitee.com/StarskyBoy/cloud-config-repo
#git使用者名稱
username: ******
#git密碼
password: ******
security:
basic:
enabled: true
user:
name: midou
password: midouJava
4.config client配置
-
方式一
spring:
application:
#對應Config Server所獲取的配置檔案的{application}
name: cloud-foo
cloud:
config:
uri: http://midou:[email protected]:8053/
-
方式二
spring:
application:
#對應Config Server所獲取的配置檔案的{application}
name: cloud-foo
cloud:
config:
uri: http://192.168.43.171:8053/
username: midou
password: midouJava
訪問config server會彈出認證視窗,具體自己體驗一把。
備註
原始碼見cloud專案cloud-config-server-authenticating微服務。
五、Config Server高可用
六、Config Server配置內容加解密
備註
五、六部分可見google,就不在這裡闡述了。
原始碼獲取
1.gitee:https://gitee.com/StarskyBoy
2.github: https://github.com/StarskyBoy
3.csdn:https://blog.csdn.net/StarskyBoy
更多精彩內容請關注我,掃我有驚喜