Spring cloud - Refresh scope -获取Spring bean配置属性的运行时值



我想使用spring-actuator公开一个spring-bean的配置属性的当前值https://docs.spring.io/spring-boot/docs/current/actuator-api/htmlsingle/#env

GET /actuator/env/{props}

我有两个服务:

  • 云配置服务
  • 应用程序服务

云配置有maximum-upload-allow = 1Mfile-type = png两个配置

应用程序服务从云配置服务加载这些配置

:

@Configuration @Getter
public class ApplicationConfigurationProperty {
@Value("${maximum-upload-allow}")
private String maximumImageSize;
}
@Configuration  @RefreshScope  @Getter
public class FileConfigurationProperty {
@Value("${file-type}")
private String fileType;
}
  • 我可以得到我的配置以及通过GET /actuator/env/maximum-upload-allow(1M)和GET /actuator/env/file-type(png)

现在当我更新配置值maximum-upload-allow = 2Mfile-type = jpg

然后通过调用bus-refresh https://cloud.spring.io/spring-cloud-static/spring-cloud-bus/2.1.4.RELEASE/multi/multi__bus_endpoints.html

刷新作用域我想看到我的配置使用弹簧驱动器是:

GET /actuator/env/maximum-upload-allow=>1M(因为没有refreshscope)

GET /actuator/env/file-type=>jpg(我标记为refreshscope)

但实际上,弹簧执行器返回两个新值(2M和jpg)

问:我怎么能得到我的最大上传允许的运行时值是1M(当前值,因为这里没有RefreshScope ?

——更新

@Configuration @Setter @Getter @ConfigurationProperties
public class ApplicationConfigurationProperty {
@Value("${maximum-upload-allow}")
private String maximumImageSize;
}

该配置值在没有@RefreshScope注释的情况下刷新

我认为这里提到的这些是正确的行为https://cloud.spring.io/spring-cloud-static/spring-cloud-bus/2.1.4.RELEASE/multi/multi__bus_endpoints.html

提前谢谢你。

我发现一个简单的解决方案是实现一个新的执行器端点,它是加载@RefreshScope和使用反射读取bean的属性

最新更新