java - 在长时间运行的过程中刷新之前捕获 spring @RefreshScope bean 的值
问题描述
我正在构建一个微服务系统,并使用 Spring cloud config 通过 GitHub webhook 自动刷新来集中配置。
一切正常,除了在某些情况下,我们需要 bean 的值在整个过程中保持不变。
例如,如果在进程中间发生刷新,这样的代码最终会导致客户端收到更新的值:
@RefreshScope
@ConfigurationProperties
@Component
@Getter
@Setter
public class ServiceConfig {
@Value("${example.property}")
private String exampleProperty;
}
@RestController
@RequestMapping("/department")
public class DepartmentController {
@Autowired
private ServiceConfig serviceConfig;
@GetMapping("/config")
public ResponseEntity<String> getConfig() {
return ResponseEntity.ok(serviceConfig.getExampleProperty());
}
@GetMapping("/config-delayed")
public ResponseEntity<String> getConfigWithDelay() throws InterruptedException {
Thread.sleep(30000L);
return ResponseEntity.ok(serviceConfig.getExampleProperty());
}
}
我知道String value = serviceConfig.getExampleProperty()
在线程休眠之前添加一个应该可以工作,但是在具有许多方法调用的复杂场景中,它将非常难以控制和维护。
有没有简单的方法来实现这一点?我们正处于项目的设计阶段,因此欢迎使用除 spring 之外支持此行为的其他配置管理解决方案。
解决方案
推荐阅读
- java - Quarkus Panache OneToMany 关系未保存在数据库中
- c# - 使用继承的构建器模式
- python - 将一些独立应用程序集成到 Web 环境中的最佳理论上的方法是什么?
- ruby-on-rails - 在 ruby 中添加到 id 的数据库列表中
- azure - 是否可以在 azcopy 中使用正则表达式?
- macos - 如果此系统没有任何打印机,如何在 Mac 上使用 QPageSetupDialog?
- java - 修改值后键值对的颠簸转换规范
- python - 我如何在python中创建检查列表是否为向量的函数
- python - 如何根据python中最近的时间戳左连接两个长度不同的数据帧并且不丢失任何行?
- mybatis - Java从Oracle获取(日期和时间),有的小于1小时,有的正常