1

我想根据数据库值启用我的新功能。如果为特性设置了数据库值,则应启用新代码。否则它应该切换并流向旧代码。有没有办法在 Java/Spring 中实现这一点?我不想经常访问数据库。我想在请求开始时打一个电话。有这方面的例子吗?如果是这样,请告诉我。谢谢

4

1 回答 1

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 方面..

于 2019-02-26T19:24:03.317 回答