gpt4 book ai didi

java - Spring 中的动态特征标志

转载 作者:行者123 更新时间:2023-11-30 05:43:31 24 4
gpt4 key购买 nike

我的网络应用程序有一个方法test,该方法每两分钟由一个cronjob调用一次,我希望能够在解决方案a解决方案之间动态切换b 带有一些功能标志,无需每次都部署它。

@Scheduled(fixedRateService = "120000")
public void test(){
if(conditionA()) {
// do solution A
} else {
// do solution B
}
}

我正在考虑使用 cookie 来实现此目的,但它仅适用于我打开的 session ,并且其他解决方案仍然可以由其他 session 调用。

有什么方法可以强制在生产中仅运行一个解决方案并动态交换它们,而无需每次都释放它们?

更新:Jonathan Johx 的答案是正确的,我在这里添加一些说明

要更新属性的值,您需要首先将 x-www-form-urlencoded 格式的键/值POST\actuator\env ,然后通过将空负载发布到 \actuator\refresh 来强制重新加载它

最佳答案

您可能会使用 @RefreshScope刷新属性的注释:

1.-在类上添加@RefreshScope

@RefreshScope
@Component
public class Test {

@Value("${conditional.istrue}")
private boolean conditional;

@Scheduled(fixedRateService = "120000")
public void test(){
if(conditional) {
// do solution A
} else {
// do solution B
}
}
}

2.- 添加标志属性并允许公开端点 /refresh 以刷新新属性。

应用程序属性

  conditional.istrue=true
management.endpoints.web.exposure.include=*

3.- 一旦 application.properties 被修改,例如:

  conditional.istrue=false

然后您可以刷新注入(inject)的配置:

 curl localhost:8080/actuator/refresh -d {} -H "Content-Type: application/json"

引用文献-https://spring.io/guides/gs/centralized-configuration/

关于java - Spring 中的动态特征标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55246497/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com