SpringCloud安裝Nacos完成配置中心
1. Nacos介紹
官網(wǎng)說明:Nacos 致力于幫助您發(fā)現(xiàn)、配置和管理微服務(wù)。Nacos 提供了一組簡(jiǎn)單易用的特性集,幫助您快速實(shí)現(xiàn)動(dòng)態(tài)服務(wù)發(fā)現(xiàn)、服務(wù)配置、服務(wù)元數(shù)據(jù)及流量管理。
2. docker安裝Nacos
基于liunx centos7,鏡像nacos/nacos-server:v2.1.0
2.1 docker-compose.yaml
version: '3.7'
services:
nacos01:
image: nacos/nacos-server:v2.1.0
container_name: nacos01
# restart: always
ports:
- "8848:8848"
environment:
- TZ=Asia/Shanghai
- MODE=standalone
- JVM_XMS=128m
- JVM_XMX=512M
networks:
- my-net
networks:
#新增的網(wǎng)絡(luò) 內(nèi)部服務(wù)名調(diào)用
my-net:
external: true
如果需要配置本地?cái)?shù)據(jù)庫的可以參考這篇文章
2.2 啟動(dòng)后訪問控制臺(tái)
訪問http://192.168.0.221:8848/nacos/
,賬號(hào)密碼都是nacos
添加一個(gè)orderservice-dev.yaml配置,內(nèi)容如下。
接下來在springboot中集成Nacos
3.Springboot集成Nacos
3.1 pom依賴
父依賴
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.8</version> </parent>
springcloud版本管理包
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>2021.0.3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
nacos配置依賴
<!--引入cloud config--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!--引入nacos config依賴--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <version>2021.0.1.0</version> </dependency> <!--cloud2021.x版本后 不在支持bootstrap No spring.config.import set--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> </dependency>
一定要注意依賴,不然很多問題
3.2 yaml配置
特別注意加載配置文件的順序 bootstrap -> application
,所以不要在application.yml
寫任何配置,不存在最好。新建一個(gè)bootstrap.yml
spring:
application:
#這個(gè)名詞需要和nacos配置的一致
name: orderservice
profiles:
#指定環(huán)境
active: dev
cloud:
#不使用默認(rèn)的config 使用nacos
config:
enabled: false
nacos:
config:
server-addr: 192.168.0.221:8848
file-extension: yaml
接下來從啟動(dòng)日志看下讀取配置的規(guī)則:
Ignore the empty nacos configuration and get it based on dataId[orderservice] & group[DEFAULT_GROUP]
Ignore the empty nacos configuration and get it based on dataId[orderservice.yaml] & group[DEFAULT_GROUP]
Located property source: [BootstrapPropertySource {name='bootstrapProperties-orderservice-dev.yaml,DEFAULT_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-orderservice.yaml,DEFAULT_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-orderservice,DEFAULT_GROUP'}]
- 服務(wù)名+配置的文件后綴(默認(rèn)
properties
后綴)最優(yōu)先,orderservice.${file-extension}
- 根據(jù)環(huán)境配置,
orderservice-${spring.profiles.active}.${file-extension}
3.3 測(cè)試配置動(dòng)態(tài)化
//開啟配置動(dòng)態(tài)刷新 @RefreshScope @Slf4j @RestController public class OrderController { @Value("${hello.world}") private String hello; @GetMapping("/test") public String test() throws Exception { log.info("config -- {}", hello); return hello; } }
3.4 測(cè)試日志
c.e.order.controller.OrderController : config -- 1345671234567887654
//修改為jack再次訪問
c.e.order.controller.OrderController : config -- hello my name is jack
到此這篇關(guān)于SpringCloud安裝Nacos完成配置中心的文章就介紹到這了,更多相關(guān)SpringCloud Nacos配置中心內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot和Vue.js實(shí)現(xiàn)的前后端分離的用戶權(quán)限管理系統(tǒng)
本文主要介紹了SpringBoot和Vue.js實(shí)現(xiàn)的前后端分離的用戶權(quán)限管理系統(tǒng),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04Java調(diào)用商品詳情API的項(xiàng)目實(shí)踐
在現(xiàn)代電子商務(wù)網(wǎng)站中,商品詳情API是一個(gè)重要的組件,本文就來介紹一下Java調(diào)用商品詳情API的項(xiàng)目實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下2023-11-11Spring 報(bào)錯(cuò):元素 "context:component-scan" 的前綴 "context" 未綁定的問題解決
這篇文章主要介紹了Spring 報(bào)錯(cuò):元素 "context:component-scan" 的前綴 "context" 未綁定的問題解決的相關(guān)資料,需要的朋友可以參考下2016-11-11Spring?Boot實(shí)現(xiàn)熱部署的五種方式
這篇文章主要介紹了Spring?Boot?五種熱部署方式,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01Spring MVC請(qǐng)求參數(shù)的深入解析
這篇文章主要給大家介紹了關(guān)于Spring MVC請(qǐng)求參數(shù)解析的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05Java自動(dòng)取款機(jī)ATM案例實(shí)現(xiàn)
本文主要介紹了Java自動(dòng)取款機(jī)ATM案例實(shí)現(xiàn),整個(gè)過程可以分為三部分:登錄賬戶和執(zhí)行取款操作,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08Java實(shí)戰(zhàn)之基于swing的QQ郵件收發(fā)功能實(shí)現(xiàn)
這篇文章主要介紹了Java實(shí)戰(zhàn)之基于swing的QQ郵件收發(fā)功能實(shí)現(xiàn),文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04