spring cloud学习(七)Spring Cloud Config(续)

Spring Cloud Config(续)

个人参考项目

个人博客 : https://zggdczfr.cn/
个人参考项目 : (整合到上一个案例中)https://github.com/FunriLy/springcloud-study/tree/master/%E6%A1%88%E4%BE%8B5

为 Config Client 配置配置刷新

场景介绍

上一个案例,我们成功配置了 Config Server 与 Config Client。依次启动两个项目。
* 访问API接口 http://localhost:1111/config 来获取配置信息:

The Config Word Is : Hello World !
The Config Word Is : Hello World !
  • 由此可见,配置资源的更新不能即时通知到 Server Client。
实现配置文件更新
  • 引入依赖
        <!-- actuator 监控 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
  • 在 Controller层加入注解@RefreshScope    
    如果 @Controller 是加在单独的类中声明的(不是在Application启动类上声明)。那么@RefreshScope要加在声明@Controller声明的类上,否则refresh之后Conroller拿不到最新的值,会默认调用缓存。

  • 通过POST请求发送到 http://localhost:1111/refresh ,我们可以看到以下内容:  

[
  "configword"
]
The Config Word Is : NewConfig !
原文地址:https://www.cnblogs.com/MaxElephant/p/8109285.html