【问题标题】:dynamic feature flag in spring春季动态特征标志
【发布时间】:2019-03-19 17:08:03
【问题描述】:

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

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

我正在考虑为此目的使用 cookie,但它只适用于我打开的会话,而且其他解决方案仍然可以被其他会话调用。

有什么方法可以让我在生产环境中只执行一个解决方案并动态交换它们而不每次都释放它们?

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

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

【问题讨论】:

    标签: java spring toggle


    【解决方案1】:

    你可以使用@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以刷新新属性。

    application.properties

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

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

      conditional.istrue=false
    

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

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

    参考文献 - https://spring.io/guides/gs/centralized-configuration/

    【讨论】:

    • 谢谢,我猜这只有在你有外部配置并提交时才有效?
    • 不一定,因为你的项目资源中可以有一个application.properties,如果是这样的话,你就不需要对某些服务进行外部配置了。
    • 我明白了,但是当我尝试通过curl localhost:8080/actuator/refresh -d { conditional.istrue=false } -H "Content-Type: application/json" 命令将conditional.istrue 从默认的true 更新为false 时,值没有改变,我错过了任何步骤吗?
    • 不需要传入{}条件.isTrue=false,需要从application.properties @rawData设置
    • 如果值在读取的同时更新/刷新会发生什么?这不是比赛条件吗?
    猜你喜欢
    • 2019-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-18
    • 1970-01-01
    • 1970-01-01
    • 2017-09-15
    • 2017-10-06
    相关资源
    最近更新 更多