SpringCloud微服務(wù)應(yīng)用config配置中心詳解
前言
在系統(tǒng)架構(gòu)中,和安全、日志、監(jiān)控等非功能需求同樣,配置管理也是一種非功能需求。配置中心是整個微服務(wù)基礎(chǔ)架構(gòu)體系中的一個組件,如下圖,它的功能看上去并不起眼,無非就是簡單配置的管理和存取,但它是整個微服務(wù)架構(gòu)中不可或缺的一環(huán)。另外,配置中心若是真得用好了,它還能推進技術(shù)組織持續(xù)交付和DevOps轉(zhuǎn)型。
一、傳統(tǒng)應(yīng)用配置痛點
配置散亂格式不標(biāo)準(zhǔn)
配置散亂格式不標(biāo)準(zhǔn)
有的用 properties 格式,有的用 xml 格式,還有的存 DB,團隊傾向自造輪子,作法五花八門。
主要采用本地靜態(tài)配置,配置修改麻煩
配置修改通常須要通過一個較長的測試發(fā)布周期。在分布式微服務(wù)環(huán)境下,當(dāng)服務(wù)實例不少時,修改配置費時費力。
易引起生產(chǎn)事故
團隊在發(fā)布的時候?qū)y試環(huán)境的配置帶到生產(chǎn)上,引起資損事故。
配置缺少安全審計和版本控制功能
二、Config 配置中心介紹
配置中心實際上就是分布式系統(tǒng)中集中統(tǒng)一管理線上應(yīng)用程序配置項的管理平臺。
在微服務(wù)架構(gòu)中,當(dāng)系統(tǒng)從一個單體應(yīng)用,被拆分成分布式系統(tǒng)上一個個服務(wù)節(jié)點后,配置文件也必須跟著遷移(分割),這樣配置就分散了。不僅如此,分散中還伴隨著冗余,如下圖所示:
創(chuàng)建配置中心,將配置從各個應(yīng)用中剝離出來,對配置進行統(tǒng)一管理,應(yīng)用自身不需要自己去管理配置。如下圖所示:
總結(jié)起來最主要包括以下三方面核心功能:
配置統(tǒng)一管理
配置項的修改編輯統(tǒng)一在配置中心頁面進行,還包括統(tǒng)一的配置版本管理、環(huán)境隔離、灰度發(fā)布以及熱發(fā)布,在不重啟應(yīng)用的情況下使得修改的配置可以生效起作用。
權(quán)限統(tǒng)一控制
主要控制其配置的讀取權(quán)限以及修改權(quán)限,通過統(tǒng)一的權(quán)限管理提升運維效率。
操作統(tǒng)一審計
記錄用戶操作修改配置的歷史信息,這樣在出現(xiàn)問題的時候可以進行復(fù)盤回查,同時進行操作審計。
三、服務(wù)端Config Server搭建
1.pom依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <!-- 動態(tài)刷新使用 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
2.application啟動類配置
@SpringBootApplication @EnableConfigServer @EnableEurekaClient public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
3.application.yml配置
spring: application: name: config-server # 使用本地文件,也可配置成從git獲取配置文件 profiles: active: - native cloud: config: server: native: search-locations: classpath:/config allow-override: true override-none: true override-system-properties: false discovery: enabled: true fail-fast: true # 注冊eureka地址,提供客戶端eureka訪問 eureka: client: serviceUrl: defaultZone: http://ip:port/eureka/ server: port: 8889
4.test-dev.xml(客戶端應(yīng)讀取的配置)
profile: dev spring: rabbitmq: host: 10.10.10.10 port: 5672 username: admin password: 123456 test: override: client-server refresh: re3
5.項目結(jié)構(gòu)
四、客戶端Config Client搭建
1.pom依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> </dependency> <!-- 動態(tài)刷新使用 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
2.application啟動類配置
@SpringBootApplication @EnableEurekaClient public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } }
3.bootstrap.yml配置
創(chuàng)建bootstrap.yml配置,因為啟動服務(wù)會優(yōu)先采用這個文件的配置,然后是application.yml的配置,服務(wù)器啟動核心配置可能在配置中心,所以必須創(chuàng)建bootstrap.yml,優(yōu)先連接配置中心
spring: cloud: config: # 采用的是test-dev.yml,name是前綴,profile是后綴,label是git存放的分支節(jié)點(本次未使用) name: test profile: dev label: dev # 通過service-id(eureka服務(wù)名)連接配置中心 discovery: enabled: true service-id: config-server eureka: client: serviceUrl: defaultZone: http://ip:port/eureka/
4.application.yml配置
server: port: 8888 spring: application: name: config-client eureka: client: serviceUrl: defaultZone: http://ip:port/eureka/ # 動態(tài)刷新使用 management: endpoints: web: exposure: # 暴露監(jiān)控接口,*為全部接口 include: '*'
5.測試controller
@RestController @RefreshScope public class TestController { @Value("${spring.rabbitmq.host}") String host; @Value("${test.override}") String override; @Value("${test.refresh}") String refresh; @RequestMapping("/config") public String getConfig(){ return host; } @RequestMapping("/override") public String getOverride(){ return override; } @RequestMapping("/refresh") public String getRefresh(){ return refresh; } }
6.項目結(jié)構(gòu)
五.動態(tài)刷新
上述配置中已經(jīng)包含了動態(tài)刷新配置,讓我們一起看一下,是哪些配置起到的作用呢
1.客戶端Config client,添加spring-boot-starter-actuator依賴
<!-- 動態(tài)刷新使用 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
2.Controller層添加注解@RefreshScope
@RestController @RefreshScope public class TestController
3.客戶端application.yml增加配置
# 動態(tài)刷新使用 management: endpoints: web: exposure: # 暴露監(jiān)控接口,*為全部接口 include: '*'
4.客戶端手動調(diào)用動態(tài)刷新接口
http://localhost:8888/actuator/refresh
六.測試
開啟eureka,gateway,config server,config client服務(wù),注意開啟順序
1.調(diào)用TestController中config接口
返回了server中mq的ip,正確
2.調(diào)用TestController中override接口
返回了server中test.override的配置,覆蓋了client的test.override
3.調(diào)用TestController中refresh接口(動態(tài)刷新)
現(xiàn)在,我們修改server中test.refresh配置,由re2改為re3
重啟client server(因為配置中心的配置文件采用的服務(wù)端管理,如果放置git可以不用重啟)
重啟完成后再起請求refresh接口
仍然返回re2,現(xiàn)在我們調(diào)用刷新接口
注意設(shè)置Post的header屬性Content-Type=application/json
刷新成功,并返回了屬性名稱,再次請求refresh接口,得到最新的配置:re3
到此這篇關(guān)于SpringCloud微服務(wù)應(yīng)用-config配置中心(介紹、搭建、動態(tài)刷新、測試)的文章就介紹到這了,更多相關(guān)SpringCloud config配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于Spring MVC在Controller層中注入request的坑詳解
這篇文章主要給大家介紹了關(guān)于Spring MVC在Controller層中注入request的坑的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04Java基于Runtime調(diào)用外部程序出現(xiàn)阻塞的解決方法
這篇文章主要介紹了Java基于Runtime調(diào)用外部程序出現(xiàn)阻塞的解決方法,是一個非常實用的技巧,需要的朋友可以參考下2014-09-09java用list集合存儲學(xué)生信息并算出成績平均值操作
這篇文章主要介紹了java用list集合存儲學(xué)生信息并算出成績平均值操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08Java實現(xiàn)特定范圍的完數(shù)輸出算法示例
這篇文章主要介紹了Java實現(xiàn)特定范圍的完數(shù)輸出算法,簡單說明了完數(shù)的概念、計算原理并結(jié)合實例形式分析了java針對給定范圍內(nèi)的完數(shù)輸出操作實現(xiàn)技巧,需要的朋友可以參考下2017-12-12Java對象轉(zhuǎn)json JsonFormat注解
這篇文章主要介紹了Java對象轉(zhuǎn)json JsonFormat注解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-05-05