Springboot深入講解nocos的整合與使用
前言
Nacos 致力于幫助您發(fā)現(xiàn)、配置和管理微服務。Nacos 提供了一組簡單易用的特性集,幫助您快速實現(xiàn)動態(tài)服務發(fā)現(xiàn)、服務配置、服務元數(shù)據(jù)及流量管理。
Nacos 幫助您更敏捷和容易地構(gòu)建、交付和管理微服務平臺。 Nacos 是構(gòu)建以“服務”為中心的現(xiàn)代應用架構(gòu) (例如微服務范式、云原生范式) 的服務基礎設施
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,啟動nacos-server服務
訪問的url是:http://localhost:8848/nacos/ 默認端口是8848,賬號密碼是:nacos/nocos
3,編寫controller進行動態(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:需要配置這個才能動態(tài)更新配置。 public class TestController { @Value("${name}") private String name; @GetMapping("/getName") public String test(){ return name; } }
4,添加配置文件boostrap.yaml
springboot默認加載配置文件順序:
bootstrap.properties -> bootstrap.yml -> application.properties -> application.yml 其中bootstrap.properties 配置為最高優(yōu)先級先加載的會被后加載的覆蓋掉,所以.properties和.yml同時存在時,.properties會失效,.yml會起作用。”
#端口
server:
port: 8888
#配置項目名稱
spring:
application:
#configdemo默認是nacos的DateId名稱
name: configdemo
#指定test的配置文件
profiles:
active: test
cloud:
nacos:
config:
server-addr: localhost:8848
#加載yaml的nacos文件
file-extension: yaml
可以看到啟動時進行加載了文件如下:
5,nacos配置
配置了configdemo和configdemo-test.yaml
注意的是:它的加載規(guī)則是:# 1.DataId
- 用來讀取遠程配置中心的中具體配置文件其完整格式如下:
- ${prefix}-${spring.profile.active}.${file-extension}
a. prefix 默認為 spring.application.name 的值,也可以通過配置項 spring.cloud.nacos.config.prefix來配置。
b. spring.profile.active 即為當前環(huán)境對應的 profile,詳情可以參考 Spring Boot文檔。 注意:當 spring.profile.active 為空時,對應的連接符 - 也將不存在,dataId 的拼接格式變成 ${prefix}.${file-extension}
c. file-exetension 為配置內(nèi)容的數(shù)據(jù)格式,可以通過配置項 spring.cloud.nacos.config.file-extension 來配置。目前只支持 properties 和 yaml 類型。
如果configdemo和configdemo-test.yaml 都存在name的配置,優(yōu)先configdemo-test.yaml
訪問結(jié)果如下:
以上是針對同個服務不同環(huán)境配置應用情況。
到此這篇關(guān)于Springboot深入講解nocos的整合與使用的文章就介紹到這了,更多相關(guān)Springboot nocos內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于通過Java連接mysql對反斜杠”\“轉(zhuǎn)義的測試詳解
這篇文章主要給大家介紹了關(guān)于通過Java連接mysql對反斜杠”\“轉(zhuǎn)義的測試的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家理解反斜杠”\“轉(zhuǎn)義具有一定的參考學習價值,需要的朋友們下面來一起看看吧。2017-06-06SpringBoot?使用AOP?+?Redis?防止表單重復提交的方法
Spring?Boot是一個用于構(gòu)建Web應用程序的框架,通過AOP可以實現(xiàn)防止表單重復提交,本文介紹了在Spring?Boot應用程序中使用AOP和Redis來防止表單重復提交的方法,需要的朋友可以參考下2023-04-04Java中String和StringBuffer及StringBuilder?有什么區(qū)別
這篇文章主要介紹了Java中String和StringBuffer及StringBuilder?有什么區(qū)別,String?是?Java?語言非?;A和重要的類,更多相關(guān)內(nèi)容需要的小伙伴可以參考下面文章內(nèi)容2022-06-06