使用 FF4j 对业务进行开关操作

FF4j 是一个 java 开关框架,可以轻松的实现功能切换,是 Feature Toggle 模式的实现,功能十分的强大,如果后期需要大量使用,则可以继续集成项目种的 web 控制台等组件。

op-ff4j-spring-boot-start 集成了该框架,使用 Redis 作为持久化工具。

测试方法:com.project.platform.ff4j.Ff4jTest

@SpringBootTest
@Slf4j
public class Ff4jTest {

    @Autowired
    private FF4j ff4j;

    @Test
    public void addTest() {
        // Work a bit with CRUD
        Feature f1 = new Feature("f1", true, "My firts feature", "Group1");
        ff4j.getFeatureStore().create(f1);

        PropertyString p1 = new PropertyString("p1", "v1");
        ff4j.getPropertiesStore().createProperty(p1);
    }

    @Test
    public void disableFeatureTest() {
        ff4j.check("f1");
        ff4j.disable("f1");
        boolean checkF1 = ff4j.check("f1");

        Assert.equals(false, checkF1);
    }

    @Test
    public void getPropertyTest() {
        String p1 = ff4j.getPropertiesStore().readProperty("p1").toString();
        log.info(p1);

        String p11 = (String) ff4j.getPropertiesStore().readProperty("p1").getValue();
        log.info(p11);
    }

    @Autowired
    @Qualifier("sayHelloImpl1")
    private SayHello sayHello;

    @Test
    public void aopTest() {
        // Add the Autoproxy located in package org.ff4j.aop in your Spring Context @see com.project.platform.PlatformApplication
        // Put @Flip annotation on an interface and create different implementations
        ff4j.check("sayHello");

        ff4j.disable("sayHello");
        sayHello.say(); // 你好

        ff4j.enable("sayHello");
        sayHello.say(); // hello
    }

}
@Flip(name = "sayHello", alterBean = "sayHelloImpl2")
public interface SayHello {
    void say();
}

@Component("sayHelloImpl1")
public class SayHelloImpl1 implements SayHello {
    @Override
    public void say() {
        System.out.println("你好");
    }
}

@Component("sayHelloImpl2")
public class SayHelloImpl2 implements SayHello {
    @Override
    public void say() {
        System.out.println("hello");
    }
}

配置使用 redis 进行存储以及需要注意的问题

@Configuration
public class FF4jConfig {

    /**
     * FF4j 是一个 java 开关框架,可以轻松的实现功能切换
     * FF4j 是 Feature Toggle 模式的实现
     *
     * 这里使用 redis 进行持久化存储
     * 如果要使用 aop,需要对 org.ff4j.aop 下的包进行 spring bean 扫描 @see com.project.platform.ff4j.Ff4jTest#aopTest()
     */
    @Bean
    public FF4j getFF4j() {
        RedisConnection connection = new RedisConnection();
        // new RedisConnection("localhost", 6379, "requiredPassword");
        // new RedisConnection(new JedisPool("localhost", 6379)); Defined your own pool with all capabilities of {@link redis.clients.jedis.JedisPool}

        FF4j ff4j = new FF4j();
        FeatureStoreRedis fStore = new FeatureStoreRedis(connection);
        ff4j.setFeatureStore(fStore);
        PropertyStoreRedis pStore = new PropertyStoreRedis(connection);
        ff4j.setPropertiesStore(pStore);
        ff4j.setEventRepository(new EventRepositoryRedis(connection));

        ff4j.setEnableAudit(true);
        ff4j.setAutocreate(true); // ff4j.check() 后自动创建
        return ff4j;
    }
}
posted @ 2022-11-07 13:39  LiuChengloong  阅读(300)  评论(0编辑  收藏  举报