首页 > 解决方案 > 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

应用程序服务从 cloud-config 服务加载这些配置

喜欢:

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

@Configuration  @RefreshScope  @Getter
public class FileConfigurationProperty {
  @Value("${file-type}")
  private String fileType;
}

现在当我更新配置值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来刷新范围

我想看看我使用 spring-actuator 的配置是:

GET /actuator/env/maximum-upload-allow=> 1M(因为没有刷新范围)

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

先感谢您。

标签: javaspringspring-bootmicroservicesspring-cloud

解决方案


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


推荐阅读