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,編寫(xiě)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)文章
解決Mybatis中result標(biāo)簽識(shí)別不了的情況
這篇文章主要介紹了解決Mybatis中result標(biāo)簽識(shí)別不了的情況,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。2022-01-01Java實(shí)現(xiàn)微信支付的項(xiàng)目實(shí)踐
最近的一個(gè)項(xiàng)目中涉及到了支付業(yè)務(wù),其中用到了微信支付和支付寶支付,本文就來(lái)介紹一下Java實(shí)現(xiàn)微信支付的項(xiàng)目實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10舉例講解Java的RTTI運(yùn)行時(shí)類型識(shí)別機(jī)制
這篇文章主要介紹了Java的RTTI運(yùn)行時(shí)類型識(shí)別機(jī)制,包括泛化的Class引用以及類型檢查instanceof等知識(shí)點(diǎn),需要的朋友可以參考下2016-05-05關(guān)于通過(guò)Java連接mysql對(duì)反斜杠”\“轉(zhuǎn)義的測(cè)試詳解
這篇文章主要給大家介紹了關(guān)于通過(guò)Java連接mysql對(duì)反斜杠”\“轉(zhuǎn)義的測(cè)試的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家理解反斜杠”\“轉(zhuǎn)義具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-06-06SpringBoot?使用AOP?+?Redis?防止表單重復(fù)提交的方法
Spring?Boot是一個(gè)用于構(gòu)建Web應(yīng)用程序的框架,通過(guò)AOP可以實(shí)現(xiàn)防止表單重復(fù)提交,本文介紹了在Spring?Boot應(yīng)用程序中使用AOP和Redis來(lái)防止表單重復(fù)提交的方法,需要的朋友可以參考下2023-04-04java實(shí)現(xiàn)潛艇大戰(zhàn)游戲源碼
潛艇大戰(zhàn)游戲相信大家都玩過(guò),是一款非常有趣的小游戲,那么基于代碼是如何實(shí)現(xiàn)的呢?今天小編給大家?guī)?lái)一篇教程幫助大家學(xué)習(xí)java實(shí)現(xiàn)潛艇大戰(zhàn)游戲,感謝的朋友一起看看吧2021-06-06Java中String和StringBuffer及StringBuilder?有什么區(qū)別
這篇文章主要介紹了Java中String和StringBuffer及StringBuilder?有什么區(qū)別,String?是?Java?語(yǔ)言非?;A(chǔ)和重要的類,更多相關(guān)內(nèi)容需要的小伙伴可以參考下面文章內(nèi)容2022-06-06