目录
1. pom.xml 项目依赖
2. 在 Bootstrap.properties 中配置 Nacos server 的地址和应用名
3. 通过 Spring Cloud 原生注解 @RefreshScope 实现配置自动更新:
4. 测试
- Springboot: 2.1.6.RELEASE
- SpringCloud: Greenwich.SR1
如无特殊说明, 本系列文章全采用以上版本
上一篇《Spring Cloud Alibaba | Nacos 服务注册与发现》我们聊了 Nacos 服务注册与发现, 这一篇我们接着聊 Nacos 配置管理.
Nacos 具有配置管理的功能, 在 Spring Cloud 中可以用作配置中心, 代替 Spring Cloud Config 组件, 下面我们聊一下 Nacos 如何和 Spring Cloud 集成配置中心.
创建一个项目: nacos-config
1. pom.xml 项目依赖
- <?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 配置中心
更多版本对应关系请参考:《Spring Cloud Alibaba | Nacos 服务中心初探》.
2. 在 Bootstrap.properties 中配置 Nacos server 的地址和应用名
- spring.cloud.nacos.config.server-addr=127.0.0.1:8848
- spring.application.name=spring-cloud-nacos-config
说明: 之所以需要配置 spring.application.name , 是因为它是构成 Nacos 配置管理 dataId 字段的一部分.
在 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 类型.
3. 通过 Spring Cloud 原生注解 @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;
- }
- }
4. 测试
首先通过调用 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
GitHub - 示例代码
来源: https://www.cnblogs.com/babycomeon/p/11185168.html