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