Spring cloud - 刷新范围 - 获取 spring-bean 的配置属性的运行时值

问题描述

我想使用 spring-actuator https://docs.spring.io/spring-boot/docs/current/actuator-api/htmlsingle/#env

公开 spring-bean 的配置属性的当前值

GET /actuator/env/{props}

我有 2 个服务:

  • 云配置服务
  • 申请服务

云配置有 2 个配置,分别是 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)

但实际上,spring-actuator 返回两个新值(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 的属性