springcloud bus中踩过的坑

使用springcloud bus来动态刷新配置的时候,按照教程一步步执行下来,最后执行config-clinet客户端接口 /bus/refresh的完毕,发现返回的配置属性值没有变化,一开始以为是浏览器缓存的问题,清缓存啥的折腾了一遍,没效果。各种百度,看到网上说的要加 @RefreshScope注解啥的,还要配置 

 management:
  endpoints:
    web:
      exposure:
        include: bus-refresh

但是我发现这个是springboot 2.X之后的问题,我用的是springboot1.5x 为什么会出现问题呢,而且我用postman执行了/bus/refresh接口之后,能看到config-client服务下面打印出的日志,明显能看到
Received remote refresh request. Keys refreshed [people.name, people.age] 确实已经获取到刷新信息了,那为什么我请求接口还是没有任何变化呢,后来尝试加了
@RefreshScope注解之后,成功完成刷新。。。。

大概原因大概是这样 :因为其他客户接收到广播后,会通过这个注解找属性进行刷新值;归根到底就是应用仅仅只是接收到广播说“你该刷新了”,然后进行spring cloud的/refresh操作,然后通过@RefreshScope注解寻找对应属性进行重新赋值

以上是我折腾了半天在某个评论中找到的解决办法。。

@RefreshScope
@RestController
public class ConfigController {

@Value("${people.name}")
private String name;
@Value("${people.age}")
private Integer age;

@RequestMapping("/getPeople")
public String getPeople() {
return "姓名:"+this.name+" ; 年龄:"+age;
}
}

猜你喜欢

转载自www.cnblogs.com/changeCode/p/11138887.html
今日推荐