你好,欢迎阅读,本文是系列文章中的第4篇。
Part1 - Nacos 是什么?
Part2 - Nacos 环境搭建
Part3 - Nacos 服务发现实践
Part4 - Nacos 分布式配置实践
本文目的是使用 Nacos 管理服务的配置。
步骤:
(1)创建一个 SpringBoot 服务,使用本地配置方式。
(2)Nacos 中创建配置。
(3)SpringBoot 服务整合 Nacos,改为使用 Nacos 中的配置。
(4)实现配置动态刷新。
1. 创建服务
pom.xml
...
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.2.5.RELEASEversion>
<relativePath/>
parent>
<groupId>com.examplegroupId>
<artifactId>configdemoartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>configdemoname>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>11java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Hoxton.SR3version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>2.2.1.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
...
添加配置(application.yaml):
test:
name: local-config
添加一个测试接口,用于输出配置值:
...
@RestController
public class TestController{
@Value("${test.name}")
private String test_name;
@GetMapping("/test")
public String testpriority(){
return test_name;
}
}
启动后访问:
http://localhost:8080/test
效果:
![image-20201101185433206](/Users/a/Library/Application Support/typora-user-images/image-20201101185433206.png)
成功输出了本地配置。
2. Nacos 中新建配置
进入 Nacos 控制的配置列表页面:
![e4c4adadb2d4542f81f48c059c2607a0.png](https://i-blog.csdnimg.cn/blog_migrate/6a0eec83e72850fe0bef06d6720a08fc.jpeg)
点击新建配置的按钮,在新建配置页面添加:
![8cfcb328f3f6b19384408ec8898c3a8d.png](https://i-blog.csdnimg.cn/blog_migrate/98359a939d0ccfad2d9ed382b3a88188.jpeg)
Data ID 项填写服务名 configdemo.yaml,配置格式选择 YAML,配置内容中填写:
test:
name: nacos-config
这里把配置的值改为了 “nacos-config”,用于与之前的 "local-config" 作为区分,然后点击页面底部的发布按钮。
回到配置列表页面,即可看到刚刚新建的配置:
![9b91e0432f561fe58691d1398aae9f24.png](https://i-blog.csdnimg.cn/blog_migrate/0ec47159f8d9fec0dd657ae3808b5c68.jpeg)
3. 服务整合 Nacos 配置
Nacos 配置的依赖在第一步已经添加好了,下面主要是修改配置。
首先,把之前的 application.yaml 文件名改为 bootstrap.yaml,这是因为 bootstrap.yaml 的优先级是最高的。
然后,在 bootstrap.yaml 中添加配置:
spring:
application:
name: configdemo
cloud:
nacos:
config:
server-addr: localhost:8848
file-extension: yaml
注意,现在服务本地配置文件中已经没有 "test.name" 这个配置项了。
重新启动服务,在启动日志中会看到如下提示:
![a55fc21890080392574fd5785a671069.png](https://i-blog.csdnimg.cn/blog_migrate/5e5d089ae2206bb50274f8ca5e8e0d03.jpeg)
再次刷新页面:
![0c781cb211324fa700a2ff92a996b816.png](https://i-blog.csdnimg.cn/blog_migrate/15785a2cf52be70781f5fbc8f2c22fc8.jpeg)
此次输出的值为 Nacos 中配置的 "nacos-config",说明已经成功读取了 Nacos 中的配置信息。
现在需要说明一下服务为什么可以自动找到 Nacos 中的配置文件。
服务启动时,会读取 bootstrap.yaml 中指定的 nacos config 服务器地址,然后根据 spring.application.name
配置项的值与 spring.cloud.nacos.config.file-extension
的值构造出 Data ID,由此就可以读取到 Nacos 中的配置文件了。
4. 动态刷新
如果希望在 Nacos 修改配置之后,实例可以动态获取最新值,则需要添加一个注解 @RefreshScope
。
代码如下:
@RefreshScope
@RestController
public class TestController{
@Value("${test.name}")
private String test_name;
@GetMapping("/test")
public String testpriority(){
return test_name;
}
}
重新启动服务。
下面在 Nacos 控制台修改配置:
![bd9994dfd86d291284d799c0d20ce754.png](https://i-blog.csdnimg.cn/blog_migrate/e00a575d9b925579b69a9882ced63e74.jpeg)
在现有值的后面添加了字符串 “-new”,以作为区分:
![c61412dc9a181fb74f3f87bd66b135b5.png](https://i-blog.csdnimg.cn/blog_migrate/8b5cf1a80f58a92ea656ac30d8839af1.jpeg)
然后点击页面底部的发布按钮,点击后,会弹出新旧配置对比窗口:
![91aa9ae327019848b5972b1d9a25f2a7.png](https://i-blog.csdnimg.cn/blog_migrate/1415c93cac075ada43d93abf4e10b399.jpeg)
确认无误后,点击确认发布按钮。
这样配置就修改完成了,下面刷新页面:
![4d6403f9d562630e00e7c86c06e1b9dc.png](https://i-blog.csdnimg.cn/blog_migrate/dba42f30ebd53d088268d36131c93c7e.jpeg)
可以看到,已经变成了刚刚修改后的值。
注意,修改配置后可没有重启服务,说明已经成功实现了配置的动态刷新。