3

Since version 2.0.0 Togglz offers Activation Strategies to go with a feature. For instance, you can connect a list of server IP addresses that shall have the feature enabled. However, how are these strategies actually attached to a feature? All I saw was that I can change the strategy in the Togglz console or even edit the data by hand in the database.

What I was looking for is some default mechanism rather similar to @EnabledByDefault. I could implement a custom state repository, it could even look for annotations, but I suspected that this solution existed out of the box.

4

2 回答 2

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();

}

使用注释

在特征定义中,使用这样的注解:

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

用于评估的状态存储库

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

/**
 * 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);
    }
}

接线是否在

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

…
@Override
public StateRepository getStateRepository() {
    JDBCStateRepository jdbcStateRepository = new JDBCStateRepository(dataSource);
    DefaultingStateRepository defaultingStateRepository = new
            DefaultingStateRepository(jdbcStateRepository);
    return new CachingStateRepository(defaultingStateRepository, 60_000);
}
…
于 2013-08-23T06:21:58.823 回答
0

可以添加 @DefaultActivationStrategy 以将激活策略设置为默认值。此外,您可以为策略设置默认值。例子:

@EnabledByDefault

@Label("UserNotification")

@DefaultActivationStrategy(
    id = UserNotificationActivationStrategy.ID,
    parameters = {
        @ActivationParameter(name = UserNotificationActivationStrategy.LICENSE_TYPE, value ="PRO")
    }
);

UserNotificationActivationStrategy 是用户定义的激活策略。

参考:https ://github.com/eugenp/tutorials/blob/master/spring-boot/src/main/java/com/baeldung/toggle/MyFeatures.java https://www.togglz.org/apidocs/2.3 .0.final/org/togglz/core/annotation/defaultactivationstrategy

于 2019-06-10T12:52:02.617 回答