【问题标题】:Spring cloud - Refresh scope - Get runtime value of configuration property of a spring-beanSpring cloud - 刷新范围 - 获取 spring-bean 的配置属性的运行时值
【发布时间】:2021-03-05 03:51:23
【问题描述】:

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

GET /actuator/env/{props}

我有 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;
}
  • 我也可以通过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 进行刷新范围

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

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

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

提前谢谢你。

【问题讨论】:

  • 您是否尝试过将缓存刷新范围延迟到更高的间隔 management.endpoint.beans.cache.time-to-live=60s 如果没有,那么可以在这里找到更多指针gist.github.com/dsyer/a43fe5f74427b371519af68c5c4904c7
  • 我想获得我的旧值(最大上传允许=1M),直到我重新启动我的应用程序服务。我不希望这个配置是可刷新的

标签: java spring spring-boot microservices spring-cloud


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2016-02-07
    • 2019-03-20
    • 1970-01-01
    • 2018-11-14
    • 1970-01-01
    • 2021-01-30
    • 1970-01-01
    • 2012-05-21
    • 1970-01-01
    相关资源
    最近更新 更多