spring cloud config 配置中心快速實(shí)現(xiàn)過(guò)程解析
spring-cloud-config 配置中心實(shí)現(xiàn)
Spring Cloud Config 用于為分布式系統(tǒng)中的基礎(chǔ)設(shè)施和微服務(wù)應(yīng)用提供集中化的外部配置支持,分為server端和client端。
server端為分布式配置中心,是一個(gè)獨(dú)立的微服務(wù)應(yīng)用;client端為分布式系統(tǒng)中的基礎(chǔ)設(shè)置或微服務(wù)應(yīng)用,通過(guò)指定配置中心來(lái)管理相關(guān)的配置。
Spring Cloud Config 構(gòu)建的配置中心,除了適用于 Spring 構(gòu)建的應(yīng)用外,也可以在任何其他語(yǔ)言構(gòu)建的應(yīng)用中使用。
Spring Cloud Config 默認(rèn)采用 Git 存儲(chǔ)配置信息,支持對(duì)配置信息的版本管理。
本示例主要內(nèi)容
- 配置中心演示client端和server端實(shí)現(xiàn)
- 配置文件放在git(因github有時(shí)候不太穩(wěn)定,我放到了國(guó)內(nèi)服務(wù)器)
- 版本切換(test、pro、dev)
Spring Cloud Config 特點(diǎn)
- 提供server端和client端支持(Spring Cloud Config Server和Spring Cloud Config Client);
- 集中式管理分布式環(huán)境下的應(yīng)用配置;
- 基于Spring環(huán)境,實(shí)現(xiàn)了與Spring應(yīng)用無(wú)縫集成;
- 可用于任何語(yǔ)言開(kāi)發(fā)的程序;
- 默認(rèn)實(shí)現(xiàn)基于Git倉(cāng)庫(kù)(也支持SVN),從而可以進(jìn)行配置的版本管理;同時(shí)也支持配置從本地文件或數(shù)據(jù)庫(kù)讀取。
代碼構(gòu)建
server端實(shí)現(xiàn)
1.pom.xml添加maven依賴
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies>
2.application.yml配置
server: port: 8001 spring: application: name: cloud-config-server cloud: config: server: git: uri: https://gitee.com/tqlin/spring-boot-demo.git #因?yàn)間ithub有時(shí)候不穩(wěn)定,我這里改到了碼云倉(cāng) searchPaths: /cloud-config/config-repo/ #配置文件目錄 force-pull: true
3.CloudConfigServerApplication.java啟動(dòng)類
@EnableConfigServer @SpringBootApplication public class CloudConfigServerApplication { public static void main(String[] args) { SpringApplication.run(CloudConfigServerApplication.class, args); } }
client端實(shí)現(xiàn)
1.pom.xml添加maven依賴
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
2.bootstrap.properties配置文件
spring.cloud.config.name=easy-config spring.cloud.config.profile=test spring.cloud.config.uri=http://localhost:8001/ spring.cloud.config.label=master
- spring.application.name:對(duì)應(yīng){application}部分
- spring.cloud.config.profile:對(duì)應(yīng){profile}部分
- spring.cloud.config.label:對(duì)應(yīng)git的分支。如果配置中心使用的是本地存儲(chǔ),則該參數(shù)無(wú)用
- spring.cloud.config.uri:配置中心的具體地址(sever端地址)
- spring.cloud.config.discovery.service-id:指定配置中心的service-id,便于擴(kuò)展為高可用配置集群。
特別注意:Spring Cloud 構(gòu)建于 Spring Boot 之上,在 Spring Boot 中有兩種上下文,一種是 bootstrap, 另外一種是 application, bootstrap 是應(yīng)用程序的父上下文,也就是說(shuō) bootstrap 加載優(yōu)先于 applicaton。bootstrap 主要用于從額外的資源來(lái)加載配置信息,還可以在本地外部配置文件中解密屬性。
這兩個(gè)上下文共用一個(gè)環(huán)境,它是任何Spring應(yīng)用程序的外部屬性的來(lái)源。bootstrap 里面的屬性會(huì)優(yōu)先加載,它們默認(rèn)也不能被本地相同配置覆蓋。
3.application.properties配置文件
spring.application.name=cloud-config-client server.port=8002
運(yùn)行示例
1.首先在碼云上面創(chuàng)建一個(gè)文件夾config-repo用來(lái)存放配置文件,我們創(chuàng)建以下三個(gè)配置文件:
// 開(kāi)發(fā)環(huán)境 easy-config-dev.properties 內(nèi)容為:easy.hello=dev config // 測(cè)試環(huán)境 easy-config-test.properties 內(nèi)容為:easy.hello=test config // 生產(chǎn)環(huán)境 easy-config-pro.properties 內(nèi)容為:easy.hello=pro config
根據(jù)上面構(gòu)建的代碼指定的項(xiàng)目地址為:https://gitee.com/tqlin/spring-boot-demo.git 目錄為: /cloud-config/config-repo/
2.分別運(yùn)行server端和client端
找到CloudConfigServerApplication.java、CloudConfigClientApplication.java分別運(yùn)行
3.測(cè)試server端
直接訪問(wèn):http://localhost:8001/easy-config/dev
我們看到成功返回了開(kāi)發(fā)配置文件信息
{ name: "easy-config", profiles: [ "dev" ], label: null, version: "6053b4c1c2343ac27e822b2a9b60c6343be72f96", state: null, propertySources: [ { name: "https://gitee.com/tqlin/spring-boot-demo.git/cloud-config/config-repo/easy-config-dev.properties", source: { easy.hello: "dev config" } } ] }
訪問(wèn):http://localhost:8001/easy-config/test、http://localhost:8001/easy-config/pro,相應(yīng)的會(huì)返回測(cè)試及正式環(huán)境的配置
倉(cāng)庫(kù)中的配置文件會(huì)被轉(zhuǎn)換成web接口,訪問(wèn)可以參照以下的規(guī)則:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
以easy-config-dev.properties為例子,它的application是easy-config,profile是dev。client會(huì)根據(jù)填寫(xiě)的參數(shù)來(lái)選擇讀取對(duì)應(yīng)的配置。
4.測(cè)試client端
訪問(wèn):http://localhost:8002/hello 我們發(fā)現(xiàn)界面成功返回了 test config,說(shuō)明測(cè)試配置文件client端讀取成功了
我們修改bootstrap.properties配置的spring.cloud.config.profile的值為dev,重啟client端,訪問(wèn):http://localhost:8002/hello 這時(shí)候界面返回 dev config,表示開(kāi)發(fā)配置訪問(wèn)成功。
資料
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
如何解決springmvc文件下載,內(nèi)容損壞的問(wèn)題
這篇文章主要介紹了解決springmvc文件下載,內(nèi)容損壞的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06java實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12Spring線程池ThreadPoolExecutor配置并且得到任務(wù)執(zhí)行的結(jié)果
今天小編就為大家分享一篇關(guān)于Spring線程池ThreadPoolExecutor配置并且得到任務(wù)執(zhí)行的結(jié)果,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03Spring Boot 2.x 把 Guava 干掉了選擇本地緩存之王 Caffeine(推薦)
這篇文章主要介紹了Spring Boot 2.x 把 Guava 干掉了選擇本地緩存之王 Caffeine,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01解決@Scheduled定時(shí)器使用@Thransactional事物問(wèn)題
這篇文章主要介紹了解決@Scheduled定時(shí)器使用@Thransactional事物問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08完美解決Logback configuration error detected的問(wèn)題
這篇文章主要介紹了完美解決Logback configuration error detected的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08JAVA實(shí)現(xiàn)異步調(diào)用實(shí)例代碼
在java平臺(tái),實(shí)現(xiàn)異步調(diào)用的角色主要三種角色:調(diào)用者、取貨憑證、真實(shí)數(shù)據(jù)。本篇文章給大家介紹java實(shí)現(xiàn)異步調(diào)用實(shí)例代碼,需要的朋友可以參考下2015-09-09