Spring Cloud Config 之svn配置仓库与动态刷新(客户端手动手动刷新)

来源:互联网 发布:吉林省财政厅网络完全 编辑:程序博客网 时间:2024/04/30 19:41
上篇完成了客户端的配置,其实咱们的主题还没进入,当你配置文件改动的时候,怎么才能不重新启动自动的加载新的配置信息呢?
这一篇算是半进主题,手动刷新

上篇提到了,客户端启动时,向服务端发请求,请求配置,服务端在中心仓库拿到配置信息后在本地缓存,以后客户端需要配置数据时直接在服务端拿。
现在的问题是,中心库的配置信息改了,我怎么才能给服务端个消息说:你去中心仓库重新加载一下,你现在的数据太旧了。
这里的问题就是发送什么消息给服务端,然后客户端怎么才能在不启动的情况下拿到数据,就像上篇说到的name与test的属性呢?要知道这两个属性实在客户端启动的时候加载的。

好了先解决第一个问题:发送什么消息给服务端,这时要利用一个组件spring-boot-starter-actuator监控系统,里面有个/refresh只要客户端以post请求访问这个地址,服务端就会重新获取
配置,ok,知道了这些我们改造config-svn-client

在pom.xml中加入下面依赖
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

并在bootstrap.properties中加入下列配置
#关闭认证,actuator自带认证,不关闭无法访问/refresh
management.security.enabled=false

并写一个工具类发送post到/refresh地址
代码如下:
public class HttpUtils {
 public static void refresh() {
  HttpURLConnection connection =null;
        try {
            URL url = new URL("http://localhost:9999/refresh");
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.connect();//链接
            InputStream in=connection.getInputStream();//等待响应
            in.close();
        } catch (Exception e) {
         e.printStackTrace();
        }finally {
         if(connection!=null){
             connection.disconnect();
            }
  }
    }
}
新增一个controller来进行访问刷新
@RestController
public class BusRefresh {
 @RequestMapping("/refresh1")
 public String test(){
  HttpUtils.refresh();
  return "success";
 }
}
准备工作完成,接下来分别启动服务端与客户端
访问http://localhost:9999/name 返回结果为test
修改application-test.properties 的name值为test1提交到svn仓库服务器
访问http://localhost:9999/refresh1 进行刷新,以请求新的配置
这时服务端控制台打印如下
2017-12-20 10:52:02.322  INFO 20700 --- [nio-8888-exec-5] o.s.c.c.s.e.NativeEnvironmentRepository  : Adding property source: file:/C:/Users/pactera/AppData/Local/Temp/config-repo-8881403147951708881/config/application-test.properties
2017-12-20 10:52:02.322  INFO 20700 --- [nio-8888-exec-5] o.s.c.c.s.e.NativeEnvironmentRepository  : Adding property source: file:/C:/Users/pactera/AppData/Local/Temp/config-repo-8881403147951708881/config/application.properties
2017-12-20 10:52:02.322  INFO 20700 --- [nio-8888-exec-5] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@6f1fc730: startup date [Wed Dec 20 10:52:02 CST 2017]; root of context hierarchy
说明重新获取配置成功
接下来访问http://localhost:9999/name 发现返回的值还是test并客户端并没有加载到新的配置,为啥呢?

解决第二个问题:客户端获取新的配置,注入到name属性中
改造MyController类,加上如下注解
@RestController
@RefreshScope//此注解,是在访问/refresh后服务端加载新配置,自动把新配置注入
public class MyController {
 //加载application-test.properties的name属性注入
 @Value("${name}")
 private String name;
 
 @RequestMapping("/name")
 public String name(){
  return name;
 }
 //加载application-test.properties的test属性
 //因为没有test属性,所以加载application.properties的test属性注入
 @Value("${test}")
 private String test;
 
 @RequestMapping("/test")
 public String test(){
  return test;
 }
}
重新启动客户端
访问http://localhost:9999/name 返回结果为test1
修改application-test.properties 的name值为test2提交到svn仓库服务器
访问http://localhost:9999/refresh1 进行刷新,以请求新的配置
再次访问http://localhost:9999/name 返回结果为test2
至此手动刷新完成
结束了,完了,拜拜
阅读全文
0 0