Springboot深入講解nocos的整合與使用
前言
Nacos 致力于幫助您發(fā)現(xiàn)、配置和管理微服務(wù)。Nacos 提供了一組簡(jiǎn)單易用的特性集,幫助您快速實(shí)現(xiàn)動(dòng)態(tài)服務(wù)發(fā)現(xiàn)、服務(wù)配置、服務(wù)元數(shù)據(jù)及流量管理。
Nacos 幫助您更敏捷和容易地構(gòu)建、交付和管理微服務(wù)平臺(tái)。 Nacos 是構(gòu)建以“服務(wù)”為中心的現(xiàn)代應(yīng)用架構(gòu) (例如微服務(wù)范式、云原生范式) 的服務(wù)基礎(chǔ)設(shè)施
1, 創(chuàng)建工程
先創(chuàng)建maven工程,父工程pom如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>configDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>2,啟動(dòng)nacos-server服務(wù)
訪問(wèn)的url是:http://localhost:8848/nacos/ 默認(rèn)端口是8848,賬號(hào)密碼是:nacos/nocos
3,編寫controller進(jìn)行動(dòng)態(tài)配置生效
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author yhq
* @version 1.0
* @date 2022/7/15 19:07
*/
@RestController
@RefreshScope //@RefreshScope:需要配置這個(gè)才能動(dòng)態(tài)更新配置。
public class TestController {
@Value("${name}")
private String name;
@GetMapping("/getName")
public String test(){
return name;
}
}4,添加配置文件boostrap.yaml
springboot默認(rèn)加載配置文件順序:
bootstrap.properties -> bootstrap.yml -> application.properties -> application.yml 其中bootstrap.properties 配置為最高優(yōu)先級(jí)先加載的會(huì)被后加載的覆蓋掉,所以.properties和.yml同時(shí)存在時(shí),.properties會(huì)失效,.yml會(huì)起作用。”
#端口
server:
port: 8888
#配置項(xiàng)目名稱
spring:
application:
#configdemo默認(rèn)是nacos的DateId名稱
name: configdemo
#指定test的配置文件
profiles:
active: test
cloud:
nacos:
config:
server-addr: localhost:8848
#加載yaml的nacos文件
file-extension: yaml
可以看到啟動(dòng)時(shí)進(jìn)行加載了文件如下:

5,nacos配置
配置了configdemo和configdemo-test.yaml
注意的是:它的加載規(guī)則是:# 1.DataId
- 用來(lái)讀取遠(yuǎn)程配置中心的中具體配置文件其完整格式如下:
- ${prefix}-${spring.profile.active}.${file-extension}
a. prefix 默認(rèn)為 spring.application.name 的值,也可以通過(guò)配置項(xiàng) spring.cloud.nacos.config.prefix來(lái)配置。
b. spring.profile.active 即為當(dāng)前環(huán)境對(duì)應(yīng)的 profile,詳情可以參考 Spring Boot文檔。 注意:當(dāng) spring.profile.active 為空時(shí),對(duì)應(yīng)的連接符 - 也將不存在,dataId 的拼接格式變成 ${prefix}.${file-extension}
c. file-exetension 為配置內(nèi)容的數(shù)據(jù)格式,可以通過(guò)配置項(xiàng) spring.cloud.nacos.config.file-extension 來(lái)配置。目前只支持 properties 和 yaml 類型。
如果configdemo和configdemo-test.yaml 都存在name的配置,優(yōu)先configdemo-test.yaml
訪問(wèn)結(jié)果如下:

以上是針對(duì)同個(gè)服務(wù)不同環(huán)境配置應(yīng)用情況。
到此這篇關(guān)于Springboot深入講解nocos的整合與使用的文章就介紹到這了,更多相關(guān)Springboot nocos內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringMVC配置多個(gè)properties文件之通配符解析
這篇文章主要介紹了SpringMVC配置多個(gè)properties文件之通配符解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
深入解析Spring?AI框架如何在Java應(yīng)用中實(shí)現(xiàn)智能化交互的關(guān)鍵
本文詳細(xì)介紹了SpringAI框架在Java應(yīng)用中的應(yīng)用,包括實(shí)體類映射、函數(shù)回調(diào)等核心功能的實(shí)現(xiàn),通過(guò)源碼分析,幫助開(kāi)發(fā)者更好地理解和使用這些高級(jí)特性,提升業(yè)務(wù)效率,感興趣的朋友跟隨小編一起看看吧2024-11-11
Java基礎(chǔ)類學(xué)習(xí)之String詳解
這篇文章主要為大家詳細(xì)介紹了Java基礎(chǔ)類中String的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Java有一定的幫助,需要的可以參考一下2022-12-12
Struts2 Result 返回JSON對(duì)象詳解
這篇文章主要講解Struts2返回JSON對(duì)象的兩種方式,講的比較詳細(xì),希望能給大家做一個(gè)參考。2016-06-06
Springboot-注解-操作日志的實(shí)現(xiàn)方式
這篇文章主要介紹了Springboot-注解-操作日志的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03
Java實(shí)現(xiàn)在正則表達(dá)式中控制大小寫的方法
這篇文章主要介紹了Java實(shí)現(xiàn)在正則表達(dá)式中控制大小寫的方法,結(jié)合實(shí)例形式分析了java正則表達(dá)式中傳遞控制參數(shù)的功能與相關(guān)操作技巧,需要的朋友可以參考下2017-04-04
SpringBoot之如何搭建SpringBoot+Maven項(xiàng)目
這篇文章主要介紹了SpringBoot之如何搭建SpringBoot+Maven項(xiàng)目問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07

