dynamic feature flag in spring

佐手、 提交于 2019-12-24 10:55:13

问题


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
  }
} 

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?

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

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


回答1:


you might use @RefreshScope annotation to refresh properties:

1.- Add @RefreshScope on class

@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.- Add flag property and allow exposure the endpoint /refresh in order to refresh new properties.

application.properties

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

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

  conditional.istrue=false

Then you can refresh configurations injected doing:

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

REFERENCES - https://spring.io/guides/gs/centralized-configuration/



来源:https://stackoverflow.com/questions/55246497/dynamic-feature-flag-in-spring

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!