本文介绍了春季动态功能标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网络应用程序有一个方法 test,它每两分钟被一个 cronjob 调用一次,我喜欢能够在 solution asolution 之间动态切换b 带有一些功能标志,无需每次都部署.

my web app has a method test which get invoked by a cronjob every two minutes, and I like to be able dynamically switching between solution a and solution b with some feature flag without deploying it each time.

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

我想为此目的使用 cookie,但它仅适用于我打开的会话,而且其他解决方案仍然可以被其他会话调用.

I was thinking to use a cookie for this purpose but it only works on the session that I have opened, and still, the other solution could be invoked by other sessions.

有什么方法可以让我只在生产中运行一个解决方案并动态交换它们而不必每次都发布它们?

is there any way that I can enforce only one solution running in production and dynamically swapping them without releasing them each time?

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

Update:Jonathan Johx answer is correct, and I add some clarification here

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

to update the value of the properties you need first to POST your key/value in x-www-form-urlencoded format to \actuator\env, then force reloading it with by post an empty payload to the \actuator\refresh

推荐答案

你可以使用 @RefreshScope 注释刷新属性:

you might use @RefreshScope annotation to refresh properties:

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 以刷新新属性.

2.- Add flag property and allow exposure the endpoint /refresh in order to refresh new properties.

application.properties

application.properties

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

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

3.- Once the application.properties is modified for example:

  conditional.istrue=false

然后你可以refresh注入的配置做:

Then you can refresh configurations injected doing:

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

参考资料- https://spring.io/guides/gs/centralized-configuration/

这篇关于春季动态功能标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 05:56