23SpringCloud - Spring Cloud Config 中使用 Refresh - Go语言中文社区

23SpringCloud - Spring Cloud Config 中使用 Refresh


上一篇文章讲了SpringCloudConfig 集成Git仓库,配和 Eureka 注册中心一起使用,但是我们会发现,修改了Git仓库的配置后,需要重启服务,才可以得到最新的配置,这一篇我们尝试使用 Refresh 实现主动获取 Config Server 配置服务中心的最新配置。

添加依赖

<!-- actuator 监控 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

安全认证

application.properties 添加以下配置.关闭安全认证

#关闭刷新安全认证
management.security.enabled=false

值是false的话,除开health接口还依赖endpoints.health.sensitive的配置外,其他接口都不需要输入用户名和密码了。

开启 refresh

在程序的启动类EurekaProviderApplication通过 @RefreshScope 开启SpringCloudConfig 客户端的 refresh刷新范围,来获取服务端的最新配置,@RefreshScope要加在声明@Controller声明的类上,否则refresh之后Conroller拿不到最新的值,会默认调用缓存。

@RefreshScope
@RestController
@EnableEurekaClient
@SpringBootApplication
public class EurekaProviderApplication {
    @Value("${content}")
    String content;
    @Value("${server.port}")
    String port;
    @RequestMapping("/")
    public String home() {
        return "Hello world ,port:" + port+",content="+content;
    }
    public static void main(String[] args) {
        SpringApplication.run(EurekaProviderApplication.class, args);
    }
}

测试服务

按照顺序依次启动项目

spring-cloud-eureka-service
spring-cloud-config-server
spring-cloud-eureka-provider-1
spring-cloud-eureka-provider-2
spring-cloud-eureka-provider-3
spring-cloud-feign-consumer

启动该工程后,访问服务注册中心,查看服务是否都已注册成功:http://localhost:8761/
在这里插入图片描述

修改Git仓库

修改Git仓库配置,在 content=hello dev后面加个123456
在这里插入图片描述

访问服务

命令窗口,通过curl http://127.0.0.1:9000/hello访问服务,或者在浏览器访问http://127.0.0.1:9000/helloF5 刷新。

发现没有得到最新的值
在这里插入图片描述

刷新配置

通过 Postman 发送 POST请求到:http://localhost:8081/refreshhttp://localhost:8083/refresh,我们可以看到以下内容:
在这里插入图片描述

访问服务

命令窗口,通过curl http://127.0.0.1:9000/hello 访问服务,或者在浏览器访问http://127.0.0.1:9000/hello F5 刷新

发现:服务8082 没有刷新到最新配置 因为没有手动触发更新

在这里插入图片描述

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_20042935/article/details/90475315
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢