SpringCloud Config(配置中心)實現配置自動重新整理總結
一、實現原理
1、ConfigServer(配置中心服務端)從遠端git拉取配置檔案並在本地git一份,ConfigClient(微服務)從ConfigServer端獲取自己對應 配置檔案;
2、當遠端git倉庫配置檔案發生改變,ConfigServer如何通知到ConfigClient端,即ConfigClient如何感知到配置發生更新?
Spring Cloud Bus會向外提供一個http介面,即圖中的/bus/refresh。我們將這個介面配置到遠端的git的webhook上,當git上的檔案內容發生變動時,就會自動呼叫/bus-refresh介面。Bus就會通知config-server,config-server會發布更新訊息到訊息匯流排的訊息佇列中,其他服務訂閱到該訊息就會資訊重新整理,從而實現整個微服務進行自動重新整理。
二:實現方式
實現方式一:某個微服務承擔配置重新整理的職責
1、提交配置觸發post呼叫客戶端A的bus/refresh介面
2、客戶端A接收到請求從Server端更新配置並且傳送給Spring Cloud Bus匯流排
3、Spring Cloud bus接到訊息並通知給其它連線在總線上的客戶端,所有總線上的客戶端均能收到訊息
4、其它客戶端接收到通知,請求Server端獲取最新配置
5、全部客戶端均獲取到最新的配置
存在問題:
1、打破了微服務的職責單一性。微服務本身是業務模組,它本不應該承擔配置重新整理的職責。2、破壞了微服務各節點的對等性。3、有一定的侷限性。WebHook的配置隨著承擔重新整理配置的微服務節點發生改變。
改進如下方式二:配置中心Server端承擔起配置重新整理的職責,原理圖如下:
1、提交配置觸發post請求給server端的bus/refresh介面
2、server端接收到請求併發送給Spring Cloud Bus匯流排
3、Spring Cloud bus接到訊息並通知給其它連線到匯流排的客戶端
4、其它客戶端接收到通知,請求Server端獲取最新配置
5、全部客戶端均獲取到最新的配置
三:實現步驟
基本步驟:1、新增依賴 2、修改配置檔案 3、添加註解
備註:這裡給出方式二配置方法,方式一的區別在:因為是某個微服務承擔配置重新整理的職責,所以Server端不需要配置 Rabbitmq和新增bus-amqp的依賴。
<一>Config Server端配置(提前安裝rabbitmq移步連結:)
1、新增依賴
<!-- config-server依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- springcloud-bus依賴實現配置自動更新,rabbitmq -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
2、修改配置檔案Bootstrap.yml檔案
server:
port: 9090
spring:
application:
name: config-server
cloud: #config服務端,從git拉取資料
config:
server:
git:
uri: https://github.com/****/config-repo # 配置git倉庫的地址
username: # git倉庫的賬號
password: # git倉庫的密碼
search-paths: /*/*/*,/* #倉庫下配置檔案搜尋路徑
rabbitmq: #本地環境不需要配置mq,但是需要啟動mq,Springboot會自動連線本地mq
host: localhost
port: 5672
username: guest
password: guest
eureka: #註冊服務
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
#defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/ #eureka高可用
management: #SpringCloud 1.5版本暴露介面,暴露/bus-refresh介面
security:
enabled: false
# endpoints: #SpringCloud 2.0.0版本以後暴露介面方式
# web:
# exposure:
# include: "*"
security: #是否開啟基本的鑑權,預設為true
basic:
enabled: false
本地環境不需要配置mq,但是需要啟動mq,Springboot會自動連線本地mq,後面客戶端也是,如果是線上環境的話,必須要進行配置,原因看看SpringCloud bus如下說明:
3、啟動類增加註解 @EnableConfigServer
至此Config-Server端已配置完畢
<二>Config Client端配置
1、新增依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2、修改配置檔案Bootstrap.yml檔案
server:
port: 9092
spring:
application:
name: config-client #對應微服務配置檔名稱
cloud:
config:
uri: http://localhost:9090/ #config server 端地址
profile: dev #專案配置檔案選擇
label: master #git倉庫的分支
discovery:
enabled: true
service-id: config-server #config-server服務名稱
rabbitmq: #本地環境不需要配置mq
host: localhost
port: 5672
username: guest
password: guest
security: #
basic:
enabled: false
3、添加註解: @RefreshScope新增在需要重新整理的配置檔案上
註明:自動重新整理只能重新整理 @RefreshScope 註解下的配置,一些特殊配置,如資料庫等,需要同樣先設定資料庫連結ConfigServer類,然後通過加 @RefreshScope 註解方式
到這裡Config-Server端和Client端已經配置完畢,先後啟動Server端和Client端,post請求方式進行測試:http://localhost:9090/bus/refresh
<三>配置git的webhook
前面已準備就緒,啟動Server端和Client端,要實現配置自動重新整理需要呼叫/bus-refresh介面通知config-Server
方式一:手動呼叫(post請求):http://localhost:9090/bus/refresh(Server端地址)
方式二:配置git的webhook ,當git端配置發生改變,自動呼叫/bus-refresh介面
<四>附件
config-client-dev.yml配置檔案:
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://127.0.0.1:3306/test_mybatis_db?autoReconnect=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password: 1234
driverClassName: com.mysql.jdbc.Driver
druid:
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1
testWhileIdle: true
filters: stat,wall,log4j
mybatis:
type-aliases-package: com.scnu.springcloudconfigclient.domain
mapper-locations: classpath*:mapper/*.xml
eureka: #註冊服務
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
custom:
username: guest-new
passwd: guest-new
profile: test-new
<五>加解密
引入:配置檔案統一放在配置中心,配置中心檔案明文存在不安全,容易洩露比如資料庫使用者名稱、密碼等,如何實現git倉庫配置檔案為密文時,通過配置中心在Config-Server端進行解密。
<一>對稱加密
1、JCE加密,Oracl官網下載,替換本機JDK下JRE的lib下在兩個檔案。
2、
a) Config-Server 端配置檔案新增:
encrypt: #加密因子
key: foobar
加密因子為foobar,這裡藉助了Server端的加密,因此配置完畢需要啟動Config-Server
b) 啟動Config-Server
$ curl -X post http://localhost:9090/encrypt -d mysecret
加密mysecret為密碼,得到如下加密字串:
682bc583f4641835fa2db009355293665d2647dade3375c0ee201de2a49f7bda
c) 逆向操作:
$ curl -X post http://localhost:9090/decrypt -d 682bc583f4641835fa2db009355293665d2647dade3375c0ee201de2a49f7bda
解密得到:
mysecret
3、使用:
a) Config-Server端配置檔案中加入:
encrypt: #加密因子foobar
key: foobar
b) Git倉庫中配置檔案caiyun-test-dev.yml
profile: '{cipher}682bc583f4641835fa2db009355293665d2647dade3375c0ee201de2a49f7bda'
備註:.properties檔案吧引號去掉
c) Config-client不需要做任何操作
<二>非對稱加密
RSA演算法
1、命令列下執行
$ keytool -genkeypair -alias mytestkey -keyalg RSA -dname "CN=Web Server,OU=Unit,O=Organization,L=City,S=State,C=US" -keypass changeme -keystore server.jks -storepass letmein
執行完生成 server.jks 檔案,加密檔案
2、將Server.jks 檔案放在Config-Server的ClassPass路徑下,
Config-Server 端配置檔案bootstrap.yml中新增:
encrypt:
keyStore:
location: classpath:/server.jks #生成在jks檔案路徑
password: letmein #key store 祕鑰
alias: mytestkey #別名
secret: changeme #私鑰
啟動Config-Server
3、加密
$ curl -X post http://localhost:9090/encrypt -d caiyun-mima
加密caiyun-mima,得到如下加密字串:
682bc583f4641835fa2db009355293665d2647dade3375c0ee201de2a49f7bda
4、使用:
a)Config-Server端配置檔案中加入:
encrypt:
keyStore:
location: classpath:/server.jks #生成在jks檔案路徑
password: letmein #key store 祕鑰
alias: mytestkey #別名
secret: changeme #私鑰
b) Git倉庫中配置檔案caiyun-test-dev.yml
profile: '{cipher}682bc583f4641835fa2db009355293665d2647dade3375c0ee201de2a49f7bda'
備註:.properties檔案吧引號去掉
c) Config-client不需要做任何操作
專案Demo程式碼見github: