目录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