SpringBoot整合Nacos自动刷新配置
- 2022 年 1 月 28 日
- 筆記
- nacos, Spring Boot, Spring Cloud, springboot, SpringCloud, 微服务, 自动刷新配置
目的
Nacos作为SpringBoot服务的注册中心和配置中心。
在NacosServer中修改配置文件,在SpringBoot不重启的情况下,获取到修改的内容。
本例将在配置文件中配置一个 cml.age=100 的配置项,程序中编写一个方法读取配置文件,并通过 Get—>/test/age 接口提供给浏览器访问。
- 若配置文件中的 age 修改为 200 ,不用重新启动程序,直接访问 /test/age 接口,将获取到最新的值 200
- 若配置文件中没有age 的配置项,或干脆没有 cml 的配置项,访问 /test/age 接口将返回默认的值 18
环境
- SpringCloud:2020.0.3
- SpringCloudAlibaba:2021.1
- SpringBoot:2.5.2
pom
pom中引入 nacos 相关配置:discovery,config,bootstrap
网上有人说,需要引入 actuator ,其实不用。本例中还集成了 spring-cloud-starter-oauth2 ,根本没有 SpringSecurity 拦截的问题
问题:NacosServer和NacosClient是如何通讯的?如果是http接口方式来回调用,为什么没有被SpringSecurity拦截?是否是rpc?
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.dependencies}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring.cloud.alibaba.dependencies}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- nacos-discovery -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--nacos config -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!--bootstrap-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
</dependencies>
配置文件
bootstrap.yml
server:
port: 9556
spring:
application:
name: app
profiles:
active: test
nacos:
discovery:
username: nacos
password: nacos
server-addr: 192.168.1.61:8848
config:
server-addr: 192.168.1.61:8848
file-extension: yaml
app-dev.yml
此配置指 NacosServer 中的配置文件 app-dev.yml ,仅截取 cml.age 部分
cml:
age: 100
代码
- RefreshScope注解:必须加在 controller 上面,加在主启动内上面不好使。哪些资源需要自动刷新配置就在该controller上面添加此注解,可封装一个 BaseController 。
- @Value(“${cml.age:18}”):读取配置文件中的 cml.age 配置项值,赋给变量 age ,默认值为 18
- getAge:获取年龄接口
- /test/age接口需要添加到 Security.permitAll
问题:RefreshScope注解为什么一定要添加在 controller 上面?为什么在主启动类上面添加不生效
@RefreshScope
@Api(tags = "测试 - api")
@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {
/**
* 获取配置文件中的 cml.age 内容,若未获取到,默认值为18
*/
@Value("${cml.age:18}")
private String age;
@ApiOperation(value = "获取年龄 - 测试配置自动刷新", notes = "获取年龄 - 测试配置自动刷新")
@GetMapping("/age")
public String getAge() {
return age;
}
}
日志
开启 nacos-refresh 日志,打印配置内容更新情况
logging:
level:
com.alibaba.cloud.nacos.refresh: debug
打印的日志:
2022-01-28 13:43:30.574 [com.alibaba.nacos.client.Worker.longPolling.fixed-192.168.1.61_8848-zjrkm-admin] DEBUG com.alibaba.cloud.nacos.refresh.NacosContextRefresher.innerReceive:136 - Refresh Nacos config group=DEFAULT_GROUP,dataId=identityQrCodeAdmin-service-cml-test.yaml,configInfo=spring:
application:
name:
.
.
.
.
.
测试
在不重启SpringBoot服务的情况,多次在 NacosServer 中修改 cml.age 配置项的值,然后通过浏览器访问 /test/age 接口,发现每次都可以获取到最新的 age 值。