springboot?bootstrap.yml?nacos配置中心問題
一.本地Nacos安裝環(huán)境: Win7 ,JDK8 ,maven3.5
1.下載安裝包
2.啟動nacos服務(wù),bin文件下下面startup.cmd
3.訪問 http://localhost:8848/nacos
二.整合Springboot
Springboot版本如下
? ? <parent> ? ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? ? <artifactId>spring-boot-starter-parent</artifactId> ? ? ? ? <version>2.1.5.RELEASE</version> ? ? ? ? <relativePath/> <!-- lookup parent from repository --> ? ? </parent>
nacos的版本
<!--nacos--> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>com.alibaba.cloud</groupId>? <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>? <version>0.2.1.RELEASE</version>? </dependency>
1.在Springboot項目中添加 bootstrap.yml文件
目的在于:用于指定nacos的位置,(IP和端口)其他配置:例如項目名稱,nacos中配置文件的文件類型,如yml,text等等
配置如下:
Note:這里并沒有指定配置文件的名字,因為默認(rèn)的配置文件名是gabriel.yaml,連接上配置中心后,會自動去獲取這個默認(rèn)的配置文件。
spring: ? application: ? ? name: gabriel ? cloud: ? ? nacos: ? ? ? config: ? ? ? ? server-addr: 127.0.0.1:8848 ? ? ? ? file-extension: yaml
2.在nacos頁面上配置項目所需要的相關(guān)依賴
注意:
項目中的 application.name屬性 必須要與nacos配置文件的名稱相同(也就是說,如果不指定配置文件名字,想使用默認(rèn)設(shè)置,那么服務(wù)名與nacos配置文件名必須相同)nacos配置文件,必須攜帶相應(yīng)的后綴(這里是yaml),必須要與bootstrap.yml中配置的后綴一致,即如果配置了 file-extension: yaml ,則 nacos中的文件名必須是xxxx.yaml, 后面的yaml不能省略,否則會報錯依賴名稱必須要正確,這里如果引入了下面的依賴,雖然也會讀取配置文件,但是無法啟動自動刷新配置的功能(這個問題很坑)
<dependency> ? ? <groupId>org.springframework.cloud</groupId> ? ? <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> ? ? <version>0.2.0.RELEASE</version> </dependency> ?
配置文件的名字為 gabriel.yaml


3.在需要添加自動刷新的Controller類上
加入@RefreshScope注解,啟動自動刷新nacos的配置
@RestController
@RequestMapping(value = "/test")
@RefreshScope
public class TestController {
? ? @Value(value = "${service.name:}")
? ? private String serverName;
? ? /**
? ? ?*
? ? ?* @return
? ? ?*/
? ? @RequestMapping(value = "/get_nacos")
? ? public String getNacos(){
? ? ? ? System.out.println(serverName);
? ? ? ? return serverName;
? ? }
}?總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Idea工具中創(chuàng)建 SpringBoot工程及入門詳解
這篇文章主要介紹了Idea工具中創(chuàng)建 SpringBoot工程及入門分析詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02
SpringBoot集成shiro,MyRealm中無法@Autowired注入Service的問題
今天小編就為大家分享一篇關(guān)于SpringBoot集成shiro,MyRealm中無法@Autowired注入Service的問題,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03
Springboot把外部依賴包納入Spring容器管理的兩種方式
這篇文章主要給大家介紹了Springboot把外部依賴包納入Spring容器管理的兩種方式,Spring.factories和org.springframework.boot.autoconfigure.AutoConfiguration.imports,有感興趣的小伙伴可以參考閱讀本文2023-07-07
spring security獲取用戶信息的實現(xiàn)代碼
這篇文章主要介紹了spring security獲取用戶信息的實現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
SpringBoot根據(jù)注解動態(tài)執(zhí)行類中的方法實現(xiàn)
本文主要介紹了SpringBoot根據(jù)注解動態(tài)執(zhí)行類中的方法實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08
Springboot手動連接庫并獲取指定表結(jié)構(gòu)的示例代碼
這篇文章主要介紹了Springboot手動連接庫并獲取指定表結(jié)構(gòu)的示例代碼,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07

