如何解决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 = 1M
和 file-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 = 2M
和 file-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 的属性
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。