spring Cloud-config(客户端配置自动刷新)

之前讲的配置管理, 只有在应用启动时会读取到GIT的内容, 之后只要应用不重启,GIT中文件的修改,应用无法感知, 即使重启Config Server也不行

下面就是要讲配置的自动刷新

1. 让客户端支持/refresh方法

a. 首先,在pom.xml中添加以下依赖。spring-boot-starter-actuator是一套监控的功能,可以监控程序在运行时状态,其中就包括/refresh的功能。

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

b. 其次,开启refresh机制, 需要给加载变量的类上面加载@RefreshScope注解,其它代码可不做任何改变,那么在客户端执行/refresh的时候就会更新此类下面的变量值,包括通过config client从GIT获取的配置

@SpringBootApplication
@RestController
@RefreshScope
public class ConfigClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }

    @Value("${hello}")
    String hello;
    @RequestMapping(value = "/hello")
    public String hello(){
        return hello;
    }
}

c). 启动应用, 查看http://localhost:8881/hello

d). 再次修改config-client-dev.properties的内容

e). 用chome的postman发送POST请求:http://localhost/refesh

转载自:http://www.cnblogs.com/chry/p/7260778.html

原文地址:https://www.cnblogs.com/linhongwenBlog/p/8697535.html