SpringBoot使用Nacos配置中心

weixin_34000916發表於2019-01-28

  1.Nacos簡介

  Nacos是阿里巴巴集團開源的一個易於使用的平臺,專為動態服務發現,配置和服務管理而設計。它可以幫助您輕鬆構建雲本機應用程式和微服務平臺。

  Nacos基本上支援現在所有型別的服務,例如,Dubbo / gRPC服務,Spring Cloud RESTFul服務或Kubernetes服務。

  尤其是使用Eureka註冊中心的,並且擔心Eureka閉源的開發者們,可以將註冊中心修改為Nacos,本文主要介紹Nacos配置中心的使用。

  Nacos官網如下圖所示,官網地址https://nacos.io/zh-cn/

  


  2.Nacos安裝

  Nacos安裝可以採用如下兩種方式:

  1.官網下載穩定版本解壓使用。

  2.下載原始碼編譯使用,目前最新的版本是0.8.0版本。本文簡單介紹一下第二種方式,到Nacos的穩定版本下載地址https://github.com/alibaba/nacos/releases,下載最新版,本文下的是tag.gz檔案,下載後解壓即安裝完成,然後進入解壓目錄後的bin目錄執行如下命令啟動Nacos。

  sh startup.sh -m standalone

  啟動可以看到控制檯如圖所示,埠號是8848(好像是因為珠穆朗瑪峰的高度),版本0.8.0等等資訊。

  


  3.SpringBoot使用Nacos

  接下來,建立專案,專案中加入使用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 modelVersion4.0.0/modelVersion parent groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-parent/artifactId version2.1.1.RELEASE/version relativePath/ !-- lookup parent from repository -- /parent groupIdcom.dalaoyang/groupId artifactIdspringboot2_nacos_config/artifactId version0.0.1-SNAPSHOT/version namespringboot2_nacos_config/name descriptionspringboot2_nacos_config/description properties java.version1.8/java.version /properties dependencies dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-devtools/artifactId scoperuntime/scope /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-test/artifactId scopetest/scope /dependency !-- https://mvnrepository.com/artifact/com.alibaba.boot/nacos-config-spring-boot-starter -- dependency groupIdcom.alibaba.boot/groupId artifactIdnacos-config-spring-boot-starter/artifactId version0.2.1/version /dependency /dependencies build plugins plugin groupIdorg.springframework.boot/groupId artifactIdspring-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,可以看到如下所示:

  


  4.使用Nacos修改配置

  訪問Nacos服務,http://localhost:8848/nacos/#/login,預設情況使用者名稱密碼都是nacos,登入頁如圖所示。

  

  登入後如圖所示。

  


  接下來點選右側加號,新增我們剛剛建立的data id 的服務,並將配置由123修改為111,如圖所示。

  


  然後點選右下角釋出按鈕,再次訪問http://localhost:8080/test如圖所示。

  


  到這裡SpringBoot使用Nacos配置中心就完成了,感興趣可以檢視原始碼仔細研究。

  


轉載於:https://juejin.im/post/5c4ec43b51882528735f0324

相關文章