SpringCloud Config分布式配置中心使用教程介紹
一、簡介
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倉庫中)
- 基于Spring環(huán)境提供配置管理,與Spring系列框架無縫結(jié)合
- 可用于任何語言開發(fā)環(huán)境,基于Http協(xié)議。
- 默認(rèn)基于GIT倉庫實(shí)現(xiàn)版本控制。
二、使用
1.搭建配置文件倉庫
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倉庫
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)編寫啟動(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來說可以是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)測試熱刷新
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)測試
http://localhost:8080/actuator/bus-refresh
到此這篇關(guān)于SpringCloud Config分布式配置中心使用教程介紹的文章就介紹到這了,更多相關(guān)Springcloud Config配置中心內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java和Rust實(shí)現(xiàn)JSON序列化互轉(zhuǎn)的解決方案詳解
這篇文章主要為大家詳細(xì)介紹了Java和Rust實(shí)現(xiàn)JSON序列化互轉(zhuǎn)的解決方案,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03
深入分析Comparable與Comparator及Clonable三個(gè)Java接口
接口不是類,而是對(duì)類的一組需求描述,這些類要遵從接口描述的統(tǒng)一格式進(jìn)行定義,這篇文章主要為大家詳細(xì)介紹了Java的Comparable,Comparator和Cloneable的接口,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-05-05
Java List Object[]轉(zhuǎn)換成List T的實(shí)例
這篇文章主要介紹了Java List Object[]轉(zhuǎn)換成List T的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09
Java計(jì)算數(shù)學(xué)表達(dá)式代碼詳解
這篇文章主要介紹了Java計(jì)算數(shù)學(xué)表達(dá)式代碼詳解,具有一定借鑒價(jià)值,需要的朋友可以了解下。2017-12-12
使用java基礎(chǔ)類實(shí)現(xiàn)zip壓縮和zip解壓工具類分享
使用java基礎(chǔ)類寫的一個(gè)簡單的zip壓縮解壓工具類,實(shí)現(xiàn)了指定目錄壓縮到和該目錄同名的zip文件和將zip文件解壓到指定的目錄的功能2014-03-03
一文搞懂接口參數(shù)簽名與驗(yàn)簽(附含java python php版)
這篇文章主要為大家介紹了java python php不同版的接口參數(shù)簽名與驗(yàn)簽示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06

