简体   繁体   English

如何根据 RestController 路径参数设置 FF4j RegionFlippingStrategy?

[英]How to setup a FF4j RegionFlippingStrategy based on RestController path parameters?

I've just started using FF4j to switch between 2 different API implementations depending on which market the user is on (market = brand/country pair).我刚刚开始使用 FF4j 在 2 个不同的 API 实现之间切换,具体取决于用户所在的市场(市场 = 品牌/国家对)。 Here is the code snippet:这是代码片段:

@RestController
@RequestMapping("/{brand}/{country}")
public class HomeController {

    private final ApiService apiService;

    public HomeController(@Qualifier("new-api") ApiService apiService) {
        this.apiService = apiService;
    }

    @GetMapping("/hey")
    public String hey(@PathVariable("brand") String brand,
                      @PathVariable("country") String country) {
        return apiService.whichApi();
    }

}

interface ApiService {
    @Flip(name = "legacy-api", alterBean = "legacy-api")
    String whichApi();
}

@Component("new-api")
class NewApiApiService implements ApiService {
    @Override
    public String whichApi() {
        return "NEW_API";
    }
}

@Component("legacy-api")
class LegacyApiApiService implements ApiService {
    @Override
    public String whichApi() {
        return "LEGACY_API";
    }
}

I created a RegionFlippingStrategy ( as the doc says ) to define for which market I want to use the legacy-api , but I cannot make it work.我创建了一个RegionFlippingStrategy如文档所述)来定义我想使用legacy-apimarket ,但我无法使其工作。

How can I register my new strategy into FF4j?如何将我的新策略注册到 FF4j?

How can I switch between API dynamically based on the home controller brand/country inputs?如何根据家庭 controller brand/country输入在 API 之间动态切换?

How can I register my new strategy into FF4j?如何将我的新策略注册到 FF4j?

Let's remind the strategy code让我们提醒一下策略代码

public class MarketFlippingStrategy extends AbstractFlipStrategy {

 private final Set<String> setOfGrantedMarket = new HashSet<String>();

 @Override
 public void init(String featureName, Map<String, String> initValue) { 
   super.init(featureName, initValue);  
   assertRequiredParameter("grantedMarket");
   String[] arrayOfRegions = initValue.get("grantedMarket").split(",");
   setOfGrantedRegions.addAll(Arrays.asList(arrayOfRegions));
 }

 @Override
 public boolean evaluate(String fName, FeatureStore fStore, FlippingExecutionContext ctx) {
  return setOfGrantedRegions.contains(ctx.getString("market", true));
 }
}

The FlipStrategy should be registered in the definition of the feature legacy-api : FlipStrategy应该在功能legacy-api的定义中注册:

  • You can do it manually with the web console (Features > Edit the feature > Pick field Strategy and edit the class and parameters (param1=value1).您可以使用 web 控制台手动执行此操作(功能 > 编辑功能 > 选择字段策略并编辑 class 和参数 (param1=value1)。
  • You can do it in a file and import with console ff4j now support xml , yaml or properties .您可以在文件中执行此操作并使用控制台 ff4j 现在支持xmlyamlproperties导入。

With the following definition the feature will be enabled only for the granted markets:使用以下定义,该功能将仅对授权市场启用:

<?xml version="1.0" encoding="UTF-8" ?>
<features>
 <feature uid="legacy-api" enable="true" >
  <flipstrategy class="org.ff4j.sample.strategy.MarketFlippingStrategy" >
    <param name="grantedMarket">country1-brand1,country1-brand2</param>
  </flipstrategy>
 </feature>
</features>

How can I switch between API dynamically based on the home controller brand/country inputs?如何根据家庭 controller 品牌/国家输入在 API 之间动态切换?

The "trick" to is pass the couple brand/country as a single variable market in the FlippingExecutionContext and this would help you to understand but here how it works. “诀窍”是在FlippingExecutionContext中将夫妇品牌/国家作为单个变量market传递, 将帮助您理解它是如何工作的。

@RestController
@RequestMapping("/{brand}/{country}")
public class HomeController {

    private final ApiService apiService;

    public HomeController(@Qualifier("new-api") ApiService apiService) {
        this.apiService = apiService;
    }

    @GetMapping("/hey")
    public String hey(@PathVariable("brand") String brand,
                      @PathVariable("country") String country) {
        FlippingExecutionContext executionContext = new FlippingExecutionContext();
        executionContext.putString("market", brand + "-" + country);
        return apiService.whichApi(executionContext);
    }

}

interface ApiService {
    @Flip(name = "legacy-api", 
          alterBean = "legacy-api", 
          flippingStrategy = MarketFlippingStrategy.class,
          contextLocation = ContextLocation.PARAMETER)
    String whichApi(FlippingExecutionContext context);
}

@Component("new-api")
class NewApiApiService implements ApiService {
    @Override
    public String whichApi(FlippingExecutionContext context) {
        return "NEW_API";
    }
}

@Component("legacy-api")
class LegacyApiApiService implements ApiService {
    @Override
    public String whichApi(FlippingExecutionContext context) {
        return "LEGACY_API";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM