Spring?Cloud?整合?nacos實(shí)現(xiàn)動(dòng)態(tài)配置中心的詳細(xì)步驟
上一篇文章講解了Spring Cloud
整合 nacos
實(shí)現(xiàn)服務(wù)注冊與發(fā)現(xiàn),nacos
除了有服務(wù)注冊與發(fā)現(xiàn)的功能,還有提供動(dòng)態(tài)配置服務(wù)的功能。本文主要講解Spring Cloud
整合nacos
實(shí)現(xiàn)動(dòng)態(tài)配置服務(wù)。主要參考官方部署手冊點(diǎn)我。
前提條件
先下載nacos
并啟動(dòng)nacos
服務(wù)。操作步驟詳見Nacos 快速入門。
整合步驟
1. 添加依賴
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <version>2.2.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.2.12.RELEASE</version> </dependency>
版本nacos
2.1.x.RELEASE 對應(yīng)的是 Spring Boot
2.1.x 版本。版本 2.0.x.RELEASE 對應(yīng)的是 Spring Boot 2.0.x 版本,版本 1.5.x.RELEASE 對應(yīng)的是 Spring Boot
1.5.x 版本。版本不匹配的話,會出現(xiàn)很多莫名其妙的問題。nacos
依賴版本要和nacos
服務(wù)端版本要一致。
2. 新建 nacos 配置
在nacos
控制臺添加配置列表:
設(shè)置dataId
為nacos-config
,文件后綴為Properties
,設(shè)置內(nèi)容user.name=jack
:
3. bootstrap.properties 配置
在application.yml
同目錄下創(chuàng)建bootstrap.yml
文件,并配置Nacos
服務(wù)地址以及namespace
(沒有就不需要配置):
spring: application: name: nacos-config-client cloud: nacos: config: server-addr: 127.0.0.1:8848 namespace: 68468122-8955-45ee-a5b7-3d87972325b1
4. 配置dataId
dataId
對應(yīng)步驟2
里面的dataId
,有兩種配置方式,一種是官方自動(dòng)構(gòu)建dataId
,另一種是指定dataId
。
4.1 自動(dòng)配置 dataId
在Nacos Spring Cloud
中,dataId的完整格式如下:
${prefix}-${spring.profiles.active}.${file-extension}
prefix
默認(rèn)為spring.application.name
的值,也可以通過配置項(xiàng)spring.cloud.nacos.config.prefix
來配置。spring.profiles.active
即為當(dāng)前環(huán)境對應(yīng)的profile
。 注意:當(dāng)spring.profiles.active
為空時(shí),對應(yīng)的連接符 - 也將不存在,dataId
的拼接格式變成${prefix}.${file-extension}
file-exetension
為配置內(nèi)容的數(shù)據(jù)格式,可以通過配置項(xiàng)spring.cloud.nacos.config.file-extension
來配置。目前只支持properties
和yaml
類型。
比如項(xiàng)目名稱為nacos-config-client
,當(dāng)前環(huán)境為test
,格式文件為properties
,那就需要新建一個(gè)dataId
為nacos-config-client.properties
配置。
4.2 手動(dòng)設(shè)置 dataId
在NacosConfigProperties
類里面name
字段就是配置dataId
:
public class NacosConfigProperties { /** * nacos config dataId name. */ private String name; //省略其他配置 }
在bootstrap.yml
添加spring.cloud.nacos.config.name
就可以設(shè)置dataId
。
5.獲取數(shù)據(jù)
通過@Value
就能獲取配置文件的數(shù)據(jù):
@Component @RefreshScope public class TestConfig { @Value(value = "${user.name:null}") private String test; public String getTest(){ return test; }
要實(shí)現(xiàn)配置的自動(dòng)更新,需要添加Spring Cloud
原生注解 @RefreshScope
。controller
直接調(diào)用即可:
@RestController public class TestController { @Autowired private TestConfig testConfig; @GetMapping("/config") public String testConfig(){ String config = testConfig.getTest(); return config; } }
如果想通過@NacosValues
注解獲取數(shù)據(jù),需要引入nacos-config-spring-boot-starter
依賴:
<dependency> <groupId>com.alibaba.boot</groupId> <artifactId>nacos-config-spring-boot-starter</artifactId> <version>0.2.7</version> </dependency>
總結(jié)
nacos-config
配置首先添加spring-cloud-starter-alibaba-nacos-config
依賴。- 在配置列表添加配置
bootstrap.properties
添加nacos server
地址和namespace
- 配置
dataId
有兩種方式- 手動(dòng)配置,配置
spring.cloud.nacos.config.name
- 自動(dòng)配置,根據(jù)
${prefix}-${spring.profiles.active}.${file-extension}
規(guī)則配置,其中prefix
為項(xiàng)目名稱,spring.profiles.active
為項(xiàng)目運(yùn)行環(huán)境,file-extension
配置內(nèi)容的數(shù)據(jù)格式。
- 手動(dòng)配置,配置
- 通過
@Value(value = "${user.name:null}")
設(shè)置在字段上就能獲取到屬性,要實(shí)現(xiàn)自動(dòng)更新配置需要添加@RefreshScope
注解。
源碼
參考文獻(xiàn)
Nacos 融合 Spring Cloud,成為注冊配置中心
到此這篇關(guān)于Spring Cloud 整合 nacos 實(shí)現(xiàn)動(dòng)態(tài)配置中心的文章就介紹到這了,更多相關(guān)Spring Cloud 整合 nacos內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
舉例講解Java設(shè)計(jì)模式中的對象池模式編程
這篇文章主要介紹了Java設(shè)計(jì)模式中的對象池模式編程示例分享,對象池模式經(jīng)常在多線程開發(fā)時(shí)被用到,需要的朋友可以參考下2016-02-02Spring Cloud Zipkin服務(wù)端追蹤服務(wù)
這篇文章主要介紹了Spring Cloud Zipkin服務(wù)端追蹤服務(wù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04java接入創(chuàng)藍(lán)253短信驗(yàn)證碼的實(shí)例講解
下面小編就為大家分享一篇java接入創(chuàng)藍(lán)253短信驗(yàn)證碼的實(shí)例講解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01SpringBoot創(chuàng)建多模塊項(xiàng)目的全過程記錄
這篇文章主要給大家介紹了關(guān)于SpringBoot創(chuàng)建多模塊項(xiàng)目的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01詳解使用spring boot admin監(jiān)控spring cloud應(yīng)用程序
本篇文章主要介紹了詳解使用spring boot admin監(jiān)控spring cloud應(yīng)用程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11Java SpringBoot啟動(dòng)指定profile的8種方式詳解
這篇文章主要介紹了spring boot 如何指定profile啟動(dòng)的操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09Java Swing實(shí)現(xiàn)餐廳點(diǎn)餐系統(tǒng)源碼(收藏版)
這篇文章主要介紹了Java Swing實(shí)現(xiàn)餐廳點(diǎn)餐系統(tǒng)源碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02