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:
#不使用默認(rèn)的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ù)名+配置的文件后綴(默認(rèn)
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)文章
Java 實戰(zhàn)練手項目之醫(yī)院預(yù)約掛號系統(tǒng)的實現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SpringBoot+Maven+Vue+mysql實現(xiàn)一個醫(yī)院預(yù)約掛號系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平2021-11-11
quartz定時執(zhí)行任務(wù),并配置web.xml的操作方法
下面小編就為大家?guī)硪黄猶uartz定時執(zhí)行任務(wù),并配置web.xml的操作方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07
Java實現(xiàn)json數(shù)據(jù)處理的常用腳本分享
這篇文章主要為大家詳細(xì)介紹了Java實現(xiàn)json數(shù)據(jù)處理的常用腳本,文中的示例代碼講解詳細(xì),具有一定的參考價值,感興趣的小伙伴可以學(xué)習(xí)一下2023-03-03
Spring?Boot統(tǒng)一處理全局異常的實戰(zhàn)教程
最近在做項目時需要對異常進(jìn)行全局統(tǒng)一處理,所以下面這篇文章主要給大家介紹了關(guān)于Spring?Boot統(tǒng)一處理全局異常的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-12-12

