@RefreshScope not working - Spring Boot

家住魔仙堡 提交于 2019-11-29 01:15:55

问题


I am following the approach described here: https://github.com/jeroenbellen/blog-manage-and-reload-spring-properties, the only difference is that in my case, the properties are being used in multiple classes so I have put them all in one utility class CloudConfig and I refer to its variables using the getters. This is what the class looks like:

@Configuration
@RefreshScope
public class CloudConfig {

    static volatile int count; // 20 sec

    @Value("${config.count}")
    public void setCount(int count) {
        this.count = count;
    }

    public static int getCount() {
        return count;
    }

}

and I use the variable count in other classes like CloudConfig.getCount(). I am able to load the properties on bootup just fine but I am not able to dynamically update them on the fly. Can anyone tell what I am doing wrong? If instead of making this config class, I do exactly what the tutorial describes everything works fine but I am having trouble adapting it to my usecase. Can anybody tell what I am missing?


回答1:


Try using @ConfigurationProperties instead. e.g.

@ConfigurationProperties(prefix="config")
public class CloudConfig {

    private Integer count;

    public Integer count() {
        return this.count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

}

The reference doc from spring cloud states:

@RefreshScope works (technically) on an @Configuration class, but it might lead to surprising behaviour: e.g. it does not mean that all the @Beans defined in that class are themselves @RefreshScope. Specifically, anything that depends on those beans cannot rely on them being updated when a refresh is initiated, unless it is itself in @RefreshScope (in which it will be rebuilt on a refresh and its dependencies re-injected, at which point they will be re-initialized from the refreshed @Configuration).




回答2:


Anyone else facing this issue, please make sure the followings:

  1. Your controller is annotated with @RefreshScope
  2. Spring boot actuator is added into your dependency, as it is the module which actually provides these endpoints:

    org.springframework.boot spring-boot-starter-actuator

  3. Refresh endpoint has been updated to:

    http://{ip_address}:{port}/actuator/refresh

  4. Refresh endpoint isn't enabled by default. You have to enable it explicitly in the bootstrap.properties file by adding the following line:

    management.endpoints.web.exposure.include=*

I have enabled all the endpoints, while you can just enable the specific endpoints as well.



来源:https://stackoverflow.com/questions/45137555/refreshscope-not-working-spring-boot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!