Python 官方文档:入门教程 => 点击学习
目录目的环境pom配置文件代码日志测试目的 Nacos作为SpringBoot服务的注册中心和配置中心。 在NacosServer中修改配置文件,在springBoot不重启的情况下
Nacos作为SpringBoot服务的注册中心和配置中心。
在NacosServer中修改配置文件,在springBoot不重启的情况下,获取到修改的内容。
本例将在配置文件中配置一个 cml.age=100 的配置项,程序中编写一个方法读取配置文件,并通过 Get--->/test/age 接口提供给浏览器访问。
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 上面?为什么在主启动类上面添加不生效
@RefreshScope
@api(tags = "测试 - api")
@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {
@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 值。
到此这篇关于SpringBoot整合Nacos自动刷新配置的文章就介绍到这了,更多相关SpringBoot整合Nacos配置内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: 聊聊SpringBoot整合Nacos自动刷新配置的问题
本文链接: https://www.lsjlt.com/news/137953.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0