SpringBoot使用Nacos配置中心的實(shí)現(xiàn)
本文介紹SpringBoot如何使用阿里巴巴Nacos做配置中心。
1.Nacos簡(jiǎn)介Nacos是阿里巴巴集團(tuán)開源的一個(gè)易于使用的平臺(tái),專為動(dòng)態(tài)服務(wù)發(fā)現(xiàn),配置和服務(wù)管理而設(shè)計(jì)。它可以幫助您輕松構(gòu)建云本機(jī)應(yīng)用程序和微服務(wù)平臺(tái)。
Nacos基本上支持現(xiàn)在所有類型的服務(wù),例如,Dubbo / gRPC服務(wù),Spring Cloud RESTFul服務(wù)或Kubernetes服務(wù)。
尤其是使用Eureka注冊(cè)中心的,并且擔(dān)心Eureka閉源的開發(fā)者們,可以將注冊(cè)中心修改為Nacos,本文主要介紹Nacos配置中心的使用。
Nacos官網(wǎng)如下圖所示,官網(wǎng)地址https://nacos.io/zh-cn/
Nacos安裝可以采用如下兩種方式:
1.官網(wǎng)下載穩(wěn)定版本解壓使用。 2.下載源代碼編譯使用,目前最新的版本是0.8.0版本。本文簡(jiǎn)單介紹一下第二種方式,到Nacos的穩(wěn)定版本下載地址https://github.com/alibaba/nacos/releases,下載最新版,本文下的是tag.gz文件,下載后解壓即安裝完成,然后進(jìn)入解壓目錄后的bin目錄執(zhí)行如下命令啟動(dòng)Nacos。
sh startup.sh -m standalone
啟動(dòng)可以看到控制臺(tái)如圖所示,端口號(hào)是8848(好像是因?yàn)橹槟吕尸敺宓母叨龋姹?.8.0等等信息。
接下來,創(chuàng)建項(xiàng)目,項(xiàng)目中加入使用Nacos配置中心的依賴nacos-config-spring-boot-starter,完整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><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.1.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.dalaoyang</groupId><artifactId>springboot2_nacos_config</artifactId><version>0.0.1-SNAPSHOT</version><name>springboot2_nacos_config</name><description>springboot2_nacos_config</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/com.alibaba.boot/nacos-config-spring-boot-starter --><dependency><groupId>com.alibaba.boot</groupId><artifactId>nacos-config-spring-boot-starter</artifactId><version>0.2.1</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
配置文件中需要配置Nacos服務(wù)的地址,如下所示。
spring.application.name=springboot2-nacos-confignacos.config.server-addr=127.0.0.1:8848
在啟動(dòng)類,加入@NacosPropertySource注解其中包含兩個(gè)屬性,如下:
dataId:這個(gè)屬性是需要在Nacos中配置的Data Id。 autoRefreshed:為true的話開啟自動(dòng)更新。在使用Nacos做配置中心后,需要使用@NacosValue注解獲取配置,使用方式與@Value一樣,完整啟動(dòng)類代碼如下所示。
package com.dalaoyang;import com.alibaba.nacos.api.config.annotation.NacosValue;import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@SpringBootApplication@NacosPropertySource(dataId = 'springboot2-nacos-config', autoRefreshed = true)@RestControllerpublic class Springboot2NacosConfigApplication {public static void main(String[] args) {SpringApplication.run(Springboot2NacosConfigApplication.class, args);}@NacosValue(value = '${nacos.test.propertie:123}', autoRefreshed = true)private String testProperties;@GetMapping('/test')public String test(){return testProperties;}}
由于本文只是簡(jiǎn)單示例使用Nacos做配置中心,所以將啟動(dòng)類加了一個(gè)MVC方法,作為輸出配置信息進(jìn)行測(cè)試,這個(gè)測(cè)試的配置給了一個(gè)默認(rèn)值123,啟動(dòng)項(xiàng)目,訪問http://localhost:8080/test,可以看到如下所示:
訪問Nacos服務(wù),http://localhost:8848/nacos/#/login,默認(rèn)情況用戶名密碼都是nacos,登錄頁如圖所示。
登錄后如圖所示。
接下來點(diǎn)擊右側(cè)加號(hào),添加我們剛剛創(chuàng)建的data id 的服務(wù),并將配置由123修改為111,如圖所示。
然后點(diǎn)擊右下角發(fā)布按鈕,再次訪問http://localhost:8080/test如圖所示。
到這里SpringBoot使用Nacos配置中心就完成了,感興趣可以查看源碼仔細(xì)研究。
到此這篇關(guān)于SpringBoot使用Nacos配置中心的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot Nacos配置中心內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Redis Java Lettuce驅(qū)動(dòng)框架原理解析2. Python中Anaconda3 安裝gdal庫的方法3. Python自動(dòng)化之定位方法大殺器xpath4. python用zip壓縮與解壓縮5. JAVA如何轉(zhuǎn)換樹結(jié)構(gòu)數(shù)據(jù)代碼實(shí)例6. python公司內(nèi)項(xiàng)目對(duì)接釘釘審批流程的實(shí)現(xiàn)7. Notepad++如何配置python?配置python操作流程詳解8. Python 簡(jiǎn)介9. Python操作Excel工作簿的示例代碼(*.xlsx)10. Python importlib模塊重載使用方法詳解
