Togglz功能的默认激活策略

Mic*_*fel 3 togglz

从版本2.0.0开始,Togglz提供激活策略以使用功能.例如,您可以连接应启用该功能的服务器IP地址列表.但是,这些策略如何实际附加到功能上?我只看到我可以在Togglz控制台中更改策略,甚至可以在数据库中手动编辑数据.

我正在寻找的是一些非常类似的默认机制@EnabledByDefault.我可以实现一个自定义状态存储库,它甚至可以查找注释,但我怀疑这个解决方案是开箱即用的.

Mic*_*fel 5

只是分享我自己的解决方案.

默认值的注释

我定义了应该按照方式使用的注释@EnabledByDefault.这是一个server-ip策略:

/**
 * Allows to specify that the annotated feature should use
 * {@link ServerIPStrategy} if the repository doesn't have any
 * state saved.
 * 
 * @author Michael Piefel
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface UsingServerIPStrategy {

    /**
     * A comma-separated list of server IPs for which
     * the feature should be active.
     */
    String value();

}
Run Code Online (Sandbox Code Playgroud)

使用注释

在功能定义中,使用如下注释:

…
@EnabledByDefault
@UsingServerIPStrategy(value = "192.168.1.211")
@Label("Run regular jobs to send status e-mails to participants")
MAIL_CRON_JOBS;
…
Run Code Online (Sandbox Code Playgroud)

要评估的状态存储库

我想从存储库中获取功能状态(如果已经保存).如果不是,则必须评估注释.为此,需要一个委托存储库:

/**
 * A Togglz {@link StateRepository} that looks for default strategies
 * on the defined features.
 * 
 * @author Michael Piefel
 */
@AllArgsConstructor
public class DefaultingStateRepository implements StateRepository {

    private StateRepository delegate;

    @Override
    public FeatureState getFeatureState(Feature feature) {
        FeatureState featureState = delegate.getFeatureState(feature);
        if (featureState == null) {
            // Look for a default strategy.
            // If none is defined, a null return value is good enough.
            UsingServerIPStrategy serverIPStrategy = FeatureAnnotations
                    .getAnnotation(feature, UsingServerIPStrategy.class);
            if (serverIPStrategy != null) {
                featureState = new FeatureState(feature,
                        FeatureAnnotations.isEnabledByDefault(feature));
                featureState.setStrategyId(ServerIpActivationStrategy.ID);
                featureState.setParameter(ServerIpActivationStrategy.PARAM_IPS,
                        serverIPStrategy.value());
            }
        }

        return featureState;
    }

    @Override
    public void setFeatureState(FeatureState featureState) {
        // write through
        delegate.setFeatureState(featureState);
    }
}
Run Code Online (Sandbox Code Playgroud)

接线就是这样

最后,为了使用存储库,我将它连接到我们的TogglzConfig组件中,推迟到JDBC,但也让它被缓存:

…
@Override
public StateRepository getStateRepository() {
    JDBCStateRepository jdbcStateRepository = new JDBCStateRepository(dataSource);
    DefaultingStateRepository defaultingStateRepository = new
            DefaultingStateRepository(jdbcStateRepository);
    return new CachingStateRepository(defaultingStateRepository, 60_000);
}
…
Run Code Online (Sandbox Code Playgroud)

  • 嘿.我是Togglz的作者.很抱歉回答那么晚.Togglz目前仅支持在没有任何策略的情况下默认启用功能是正确的.说实话,我不是复杂默认状态的忠实粉丝.根据我的经验,默认设置通常在环境之间有所不同,这使得找到声明默认设置的常用方法变得更加困难.但也许应该有一个SPI用于默认状态,可以更容易地定制.如果您认为这是有道理的,请随意为此打开一个问题.顺便说一句:感谢您与社区分享您的解决方案. (2认同)