数据库驱动功能切换

时间:2019-02-26 16:56:42

标签: java spring featuretoggle

我想基于数据库值启用我的新功能。如果为该功能设置了数据库值,则应启用新代码。否则,它应该切换并流向旧代码。有没有一种方法可以在Java / Spring中完成?我不想经常访问数据库。我正在考虑在请求开始时打个电话。有什么例子吗?如果是这样,请告诉我。谢谢

1 个答案:

答案 0 :(得分:2)

创建要素类实现:

public enum CustomFeatures implements Feature {
    @Label("Activates customFeature")
    MY_CUSTOM_FEATURE;

    public boolean isActive() {
        return FeatureContext.getFeatureManager().isActive(this);
    }
}

创建功能提供者:

@Bean
public FeatureProvider featureProvider() {
    return new EnumBasedFeatureProvider(CustomFeatures.class);
}

创建实体和存储库:

@Entity
public class Feature {

    private String name;
    private Boolean active;
    // getters-setters
}

创建@Component,它将查询数据库并设置新功能状态

@Component
public class FeatureToggler {

   private final FeatureRepository featureRepository;
   private final FeatureManager featureManager;

   private FeatureToggler(FeatureRepository featureRepository, FeatureManager featureManager) {
      this.featureRepository = featureRepository;
      this.featureManager = featureManager;
   }

   @Schedule(fixedRate=60000)
   public void refreshFeatureToggles() {
        featureManager.setFeatureState(new FeatureState(CustomFeatures.MY_CUSTOM_FEATURE, featureRepository.findByName().isActive);
   }


}

现在您可以使用检查功能是否启用了以下功能:

if(CustomFeatures.MY_CUSTOM_FEATURE.isActive()) {
}

或使用Spring方面。.