SpringBoot使用Nacos配置中心的實現(xiàn)
本文介紹SpringBoot如何使用阿里巴巴Nacos做配置中心。
1.Nacos簡介Nacos是阿里巴巴集團開源的一個易于使用的平臺,專為動態(tài)服務發(fā)現(xiàn),配置和服務管理而設計。它可以幫助您輕松構(gòu)建云本機應用程序和微服務平臺。
Nacos基本上支持現(xiàn)在所有類型的服務,例如,Dubbo / gRPC服務,Spring Cloud RESTFul服務或Kubernetes服務。
尤其是使用Eureka注冊中心的,并且擔心Eureka閉源的開發(fā)者們,可以將注冊中心修改為Nacos,本文主要介紹Nacos配置中心的使用。
Nacos官網(wǎng)如下圖所示,官網(wǎng)地址https://nacos.io/zh-cn/
Nacos安裝可以采用如下兩種方式:
1.官網(wǎng)下載穩(wěn)定版本解壓使用。 2.下載源代碼編譯使用,目前最新的版本是0.8.0版本。本文簡單介紹一下第二種方式,到Nacos的穩(wěn)定版本下載地址https://github.com/alibaba/nacos/releases,下載最新版,本文下的是tag.gz文件,下載后解壓即安裝完成,然后進入解壓目錄后的bin目錄執(zhí)行如下命令啟動Nacos。
sh startup.sh -m standalone
啟動可以看到控制臺如圖所示,端口號是8848(好像是因為珠穆朗瑪峰的高度),版本0.8.0等等信息。
接下來,創(chuà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服務的地址,如下所示。
spring.application.name=springboot2-nacos-confignacos.config.server-addr=127.0.0.1:8848
在啟動類,加入@NacosPropertySource注解其中包含兩個屬性,如下:
dataId:這個屬性是需要在Nacos中配置的Data Id。 autoRefreshed:為true的話開啟自動更新。在使用Nacos做配置中心后,需要使用@NacosValue注解獲取配置,使用方式與@Value一樣,完整啟動類代碼如下所示。
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;}}
由于本文只是簡單示例使用Nacos做配置中心,所以將啟動類加了一個MVC方法,作為輸出配置信息進行測試,這個測試的配置給了一個默認值123,啟動項目,訪問http://localhost:8080/test,可以看到如下所示:
訪問Nacos服務,http://localhost:8848/nacos/#/login,默認情況用戶名密碼都是nacos,登錄頁如圖所示。
登錄后如圖所示。
接下來點擊右側(cè)加號,添加我們剛剛創(chuàng)建的data id 的服務,并將配置由123修改為111,如圖所示。
然后點擊右下角發(fā)布按鈕,再次訪問http://localhost:8080/test如圖所示。
到這里SpringBoot使用Nacos配置中心就完成了,感興趣可以查看源碼仔細研究。
到此這篇關于SpringBoot使用Nacos配置中心的實現(xiàn)的文章就介紹到這了,更多相關SpringBoot Nacos配置中心內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網(wǎng)!
相關文章:
1. js select支持手動輸入功能實現(xiàn)代碼2. PHP橋接模式Bridge Pattern的優(yōu)點與實現(xiàn)過程3. asp.net core項目授權(quán)流程詳解4. html中的form不提交(排除)某些input 原創(chuàng)5. CSS3中Transition屬性詳解以及示例分享6. bootstrap select2 動態(tài)從后臺Ajax動態(tài)獲取數(shù)據(jù)的代碼7. vue使用moment如何將時間戳轉(zhuǎn)為標準日期時間格式8. 開發(fā)效率翻倍的Web API使用技巧9. jsp文件下載功能實現(xiàn)代碼10. ASP常用日期格式化函數(shù) FormatDate()
