SpringCloud安裝Nacos完成配置中心
1. Nacos介紹
官網(wǎng)說明:Nacos 致力于幫助您發(fā)現(xiàn)、配置和管理微服務(wù)。Nacos 提供了一組簡單易用的特性集,幫助您快速實現(xiàn)動態(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
如果需要配置本地數(shù)據(jù)庫的可以參考這篇文章
2.2 啟動后訪問控制臺
訪問http://192.168.0.221:8848/nacos/
,賬號密碼都是nacos
添加一個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
寫任何配置,不存在最好。新建一個bootstrap.yml
spring:
application:
#這個名詞需要和nacos配置的一致
name: orderservice
profiles:
#指定環(huán)境
active: dev
cloud:
#不使用默認的config 使用nacos
config:
enabled: false
nacos:
config:
server-addr: 192.168.0.221:8848
file-extension: yaml
接下來從啟動日志看下讀取配置的規(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ù)名+配置的文件后綴(默認
properties
后綴)最優(yōu)先,orderservice.${file-extension}
- 根據(jù)環(huán)境配置,
orderservice-${spring.profiles.active}.${file-extension}
3.3 測試配置動態(tài)化
//開啟配置動態(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.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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot和Vue.js實現(xiàn)的前后端分離的用戶權(quán)限管理系統(tǒng)
本文主要介紹了SpringBoot和Vue.js實現(xiàn)的前后端分離的用戶權(quán)限管理系統(tǒng),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04Spring 報錯:元素 "context:component-scan" 的前綴 "context" 未綁定的問題解決
這篇文章主要介紹了Spring 報錯:元素 "context:component-scan" 的前綴 "context" 未綁定的問題解決的相關(guān)資料,需要的朋友可以參考下2016-11-11Java實戰(zhàn)之基于swing的QQ郵件收發(fā)功能實現(xiàn)
這篇文章主要介紹了Java實戰(zhàn)之基于swing的QQ郵件收發(fā)功能實現(xiàn),文中有非常詳細的代碼示例,對正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04