目錄html
Springboot: 2.1.6.RELEASEjava
SpringCloud: Greenwich.SR1git
如無特殊說明,本系列文章全採用以上版本github
上一篇《Spring Cloud Alibaba | Nacos服務註冊與發現》咱們聊了Nacos服務註冊與發現,這一篇咱們接着聊Nacos配置管理。web
Nacos具備配置管理的功能,在Spring Cloud中能夠用做配置中心,代替Spring Cloud Config組件,下面咱們聊一下Nacos如何和Spring Cloud集成配置中心。spring
建立一個項目:nacos-configapache
<?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.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.springcloud</groupId> <artifactId>nacos-config</artifactId> <version>0.0.1-SNAPSHOT</version> <name>nacos-config</name> <description>Demo project for Spring Boot</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.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <version>0.9.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
主要引入spring-cloud-starter-alibaba-nacos-config,爲開啓nacos配置中心bootstrap
更多版本對應關係請參考:《Spring Cloud Alibaba | Nacos服務中心初探》。app
spring.cloud.nacos.config.server-addr=127.0.0.1:8848 spring.application.name=spring-cloud-nacos-config
說明:之因此須要配置 spring.application.name ,是由於它是構成 Nacos 配置管理 dataId字段的一部分。curl
在 Nacos Spring Cloud 中,dataId 的完整格式以下:
${prefix}-${spring.profile.active}.${file-extension}
prefix
默認爲 spring.application.name
的值,也能夠經過配置項 spring.cloud.nacos.config.prefix
來配置。spring.profile.active
即爲當前環境對應的 profile
,詳情能夠參考 Spring Boot
文檔。 注意:當 spring.profile.active
爲空時,對應的鏈接符 - 也將不存在,dataId
的拼接格式變成 ${prefix}.${file-extension}
file-exetension
爲配置內容的數據格式,能夠經過配置項 spring.cloud.nacos.config.file-extension
來配置。目前只支持 properties
和 yaml
類型。@RefreshScope
實現配置自動更新:package com.springcloud.nacosconfig.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Created with IntelliJ IDEA. * * @Date: 2019/7/14 * @Time: 18:13 * @email: inwsy@hotmail.com * Description: */ @RestController @RequestMapping("/config") @RefreshScope public class ConfigController { @Value("${useLocalCache:false}") private boolean useLocalCache; @RequestMapping("/get") public boolean get() { return useLocalCache; } }
首先經過調用 Nacos Open API
向 Nacos Server
發佈配置:dataId 爲example.properties
,內容爲useLocalCache=true
curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=spring-cloud-nacos-config.properties&group=DEFAULT_GROUP&content=useLocalCache=false"
運行 NacosConfigApplication
,調用 curl http://localhost:8080/config/get
,返回內容是 true
。
再次調用 Nacos Open API
向 Nacos server
發佈配置:dataId 爲example.properties
,內容爲useLocalCache=false
curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=spring-cloud-nacos-config.properties&group=DEFAULT_GROUP&content=useLocalCache=true"
再次訪問 http://localhost:8080/config/get
,此時返回內容爲false
,說明程序中的useLocalCache
值已經被動態更新了。
至此,Nacos配置管理已經介紹完成。
參考:
https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html