SpringCloud Config分布式配置中心使用教程介紹
一、簡(jiǎn)介
Spring Cloud Config為分布式系統(tǒng)中的配置提供服務(wù)器端和客戶端支持??梢约泄芾硭协h(huán)境中應(yīng)用程序的配置文件。其服務(wù)器端存儲(chǔ)的默認(rèn)實(shí)現(xiàn)使用GIT。
優(yōu)勢(shì)
- 提供服務(wù)端和客戶端支持(spring cloud config server和spring cloud config client)
- 集中式管理分布式環(huán)境中的配置信息(所有配置文件統(tǒng)一放在了GIT倉(cāng)庫(kù)中)
- 基于Spring環(huán)境提供配置管理,與Spring系列框架無(wú)縫結(jié)合
- 可用于任何語(yǔ)言開發(fā)環(huán)境,基于Http協(xié)議。
- 默認(rèn)基于GIT倉(cāng)庫(kù)實(shí)現(xiàn)版本控制。
二、使用
1.搭建配置文件倉(cāng)庫(kù)
2.搭建Eureka-Server注冊(cè)中心服務(wù)器
3.搭建Config-Server分布式配置中心服務(wù)器
(1)導(dǎo)入依賴
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.12.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR12</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- spring cloud系列技術(shù)中,唯一命名特殊的啟動(dòng)器依賴。 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies>
(2)編寫配置文件
server:
port: 8888# 增加分布式配置中心服務(wù)端配置。連接什么GIT倉(cāng)庫(kù)
spring:
application:
name: config-server
cloud: # spring cloud常用配置前置
config: # 分布式配置中心配置前置
server: # 服務(wù)端配置
git: # git文件倉(cāng)庫(kù)配置
uri: https://gitee.com/bjsxt_test/config.git # git倉(cāng)庫(kù)具體地址
#username: bjsxt_test # 私有倉(cāng)庫(kù)必須配置用戶名和密碼。
#password: 123456789 # 公開倉(cāng)庫(kù)可以省略用戶名和密碼配置。eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
(3)編寫啟動(dòng)類
/** * EnableConfigServer - 開啟Spring Cloud Config Server的注解。 * 提供分布式配置中心服務(wù)端功能。 */ @SpringBootApplication @EnableConfigServer public class ConfigServerApp { public static void main(String[] args) { SpringApplication.run(ConfigServerApp.class, args); } }
4.搭建Config Client
Config Client對(duì)于Spring Cloud Config是客戶端,對(duì)于Eureka來(lái)說可以是Application Server 也可以是Application Client。
(1)導(dǎo)入依賴
<dependencies> <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> <!-- spring cloud config分布式配置中心客戶端依賴 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> </dependencies>
(2)編寫配置文件
# 新配置文件 bootstrap.yml | properties。是spring cloud config技術(shù)支持的新配置文件。
# 配置文件由config分布式配置中心客戶端讀取,并請(qǐng)求分布式配置中心服務(wù)端,查詢獲取配置文件之后,Spring Boot根據(jù)配置文件,初始化環(huán)境spring:
application:
name: Config-Client
cloud:
config: # spring cloud config 客戶端配置
uri: http://localhost:8888 # 分布式配置中心服務(wù)端地址。 默認(rèn)http://localhost:8888
name: bjsxt # 要讀取的配置文件名,默認(rèn)是spring.application.name的配置值,如果沒有配置,默認(rèn)application
profile: default # 要讀取的配置文件環(huán)境是什么,默認(rèn)default
label: master # 要讀取的配置文件所在分支名稱。默認(rèn)null。從主干分支獲取文件。
(3)服務(wù)接口
public interface ConfigClientService { String test(); }
(4)服務(wù)實(shí)現(xiàn)
@Service public class ConfigClientServiceImpl implements ConfigClientService { @Value("${my.content}") private String content; @Override public String test() { System.out.println("content = " + content); return content; } }
(5)編寫控制器
@RestController public class ConfigClientController { @Autowired private ConfigClientService configClientService; @RequestMapping("/test") public String test(){ return configClientService.test(); } }
(6)編寫啟動(dòng)類
@SpringBootApplication public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class,args); } }
三、熱刷新
(1)導(dǎo)入依賴(和優(yōu)雅關(guān)閉的依賴一樣)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
(2)修改配置文件
management:
endpoints:
web:
exposure:
include: refresh,info,health
(3)修改服務(wù)實(shí)現(xiàn)(在類上添加@RefreshScope注解)
@Service @RefreshScope public class ConfigClientServiceImpl implements ConfigClientService { @Value("${my.content}") private String content; @Override public String test() { System.out.println("content = " + content); return content; } }
(4)測(cè)試熱刷新
http://localhost:8080/actuator/refresh
四、Spring Cloud Bus(消息總線)
(1)導(dǎo)入依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- 總線技術(shù)中的amqp相關(guān)依賴。 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency>
(2)修改配置文件
spring:
rabbitmq:
host: 192.168.91.128
username: bjsxt
password: bjsxt
management:
endpoints:
enabled-by-default: true
web:
exposure:
include: bus-refresh,info,health
(3)測(cè)試
http://localhost:8080/actuator/bus-refresh
到此這篇關(guān)于SpringCloud Config分布式配置中心使用教程介紹的文章就介紹到這了,更多相關(guān)Springcloud Config配置中心內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java創(chuàng)建二維碼并賦予url鏈接的功能實(shí)現(xiàn)
這篇文章給大家分享java創(chuàng)建二維碼并賦予url鏈接的功能實(shí)現(xiàn),需要獲取要賦值給二維碼的鏈接后綴,通過設(shè)置二維碼的訪問路徑等一系列操作,具體實(shí)現(xiàn)代碼跟隨小編一起看看吧2021-06-06使用@SpringBootTest注解進(jìn)行單元測(cè)試
這篇文章主要介紹了使用@SpringBootTest注解進(jìn)行單元測(cè)試,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09使用spring?security?BCryptPasswordEncoder接入系統(tǒng)
這篇文章主要介紹了使用spring?security?BCryptPasswordEncoder接入系統(tǒng)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08SpringSecurity頁(yè)面授權(quán)與登錄驗(yàn)證實(shí)現(xiàn)(內(nèi)存取值與數(shù)據(jù)庫(kù)取值)
Spring Security是一個(gè)能夠?yàn)榛赟pring的企業(yè)應(yīng)用系統(tǒng)提供聲明式的安全訪問控制解決方案的安全框架,本文主要介紹了SpringSecurity頁(yè)面授權(quán)與登錄驗(yàn)證實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06Spring整合Kaptcha谷歌驗(yàn)證碼工具的開發(fā)步驟
這篇文章主要介紹了Spring整合Kaptcha谷歌驗(yàn)證碼工具的開發(fā)步驟,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01線程池ThreadPoolExecutor使用簡(jiǎn)介與方法實(shí)例
今天小編就為大家分享一篇關(guān)于線程池ThreadPoolExecutor使用簡(jiǎn)介與方法實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03聊聊Controller中RequestMapping的作用
這篇文章主要介紹了Controller中RequestMapping的作用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02解決SpringCloud Gateway采用OpenFeign遠(yuǎn)程調(diào)用失敗的問題
在使用SpringCloud網(wǎng)關(guān)進(jìn)行統(tǒng)一鑒權(quán)和認(rèn)證過程中,通過OpenFeign遠(yuǎn)程調(diào)用鑒權(quán)服務(wù)器接口時(shí)可能會(huì)遇到遠(yuǎn)程調(diào)用失敗的問題,這通常是因?yàn)镠ttpMessageConverters沒有被正確注入到Spring容器中2024-09-09