还在用redis做功能开关?out了,来看看togglz是怎么玩的

概要

Togglz是一个Java库,用于管理应用程序中的功能标记(Feature Toggle)。它允许在运行时动态地启用或禁用某些功能,而无需重新部署应用程序,从而提高了应用程序的灵活性和可维护性。
官网在这里,今天带你入门。更加高级的用法后续更新。本文从单体console配置与数据库配置讲解。

整体架构流程

Togglz的核心组件包括
1、Feature: 表示一个可切换的功能。
2、FeatureManager: 管理所有的Feature,提供查询和切换功能的方法。
3、StateRepository: 存储每个Feature的状态(启用或禁用)。
4、UserProvider: 提供当前用户的信息,用于判断功能对哪些用户可见。
5、ActivationStrategy: 定义功能的激活策略,如根据用户、系统参数等条件来决定是否激活某个功能。

技术名词解释

Feature Toggle: 也称为 Feature Flag,是一种允许在运行时启用或禁用应用程序某些功能的技术。
Activation Strategy: Togglz提供了多种激活策略,如 UserRoleActivationStrategy (根据用户角色激活)、SystemPropertyActivationStrategy (根据系统属性激活)等。
State Repository: Togglz支持多种状态存储方式,如内存存储、文件存储、数据库存储等。

技术细节

(一)基于console控制台

1、定义Feature:
public enum MyFeatures implements Feature {
    FEATURE_ONE,
    FEATURE_TWO;

    public boolean isActive() {
        return FeatureContext.getFeatureManager().isActive(this);
    }
}
2、检查Feature状态:
if (MyFeatures.FEATURE_ONE.isActive()) {
    // 执行Feature One相关的代码
}
3、与Spring Boot集成:
<dependency>
    <groupId>org.togglz</groupId>
    <artifactId>togglz-spring-boot-starter</artifactId>
    <version>2.9.3</version>
</dependency>
4、在application.properties中配置:
togglz.features.FEATURE_ONE.enabled=true
togglz.features.FEATURE_TWO.enabled=false
5、在application.properties中启用管理控制台:
togglz.console.enabled=true
togglz.console.path=/togglz
togglz.console.secured=true

(二)基于数据库管理

在Togglz中使用数据库来管理功能标记的状态,可以确保状态在应用程序的多个实例之间保持一致。下面是如何使用数据库管理Togglz的详细步骤:

1、添加数据库依赖:
<dependency>
    <groupId>org.togglz</groupId>
    <artifactId>togglz-spring-boot-starter</artifactId>
    <version>2.9.3</version>
</dependency>
<dependency>
    <groupId>org.togglz</groupId>
    <artifactId>togglz-core</artifactId>
    <version>2.9.3</version>
</dependency>
2、配置数据源:

在application.properties中配置数据源:

spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=username
spring.datasource.password=password
3、创建Togglz表:

Togglz需要一个表来存储功能标记的状态。可以手动创建这个表,也可以让Togglz 自动创建
如果你使用的是Spring Boot,可以在application.properties中设置:

togglz.jdbc.create-schema=true

这将让Togglz在应用程序启动时自动创建所需的表。
如果你想手动创建表,可以使用以下SQL(以MySQL为例):

CREATE TABLE TOGGLZ (
  FEATURE_NAME VARCHAR(100) PRIMARY KEY, --MyFeatures的key值,不可重复
  FEATURE_ENABLED INTEGER  --1则打开,否则关闭。如果没有设置key值,也是默认关闭
);
4、配置StateRepository(*****):
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.togglz.core.manager.FeatureManagerBuilder;
import org.togglz.core.repository.StateRepository;
import org.togglz.core.repository.jdbc.JDBCStateRepository;
import org.togglz.core.spi.FeatureProvider;
import org.togglz.core.user.UserProvider;
import org.togglz.spring.security.SpringSecurityUserProvider;

import javax.sql.DataSource;

@Configuration
public class TogglzConfiguration {

    //多数据源可以指定@Qualifier指定
    @Autowired 
    private DataSource dataSource;

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

    @Bean
    public StateRepository stateRepository() {
        JDBCStateRepository jdbcStateRepository = new JDBCStateRepository(dataSource);
        
        // (可选)设置数据表的名称,默认为"TOGGLZ"
        jdbcStateRepository.setTableName("my_togglz");
        
        // (可选)设置列名映射,默认为"FEATURE_NAME"和"FEATURE_ENABLED"
        jdbcStateRepository.setFeatureNameColumn("my_feature_name");
        jdbcStateRepository.setFeatureEnabledColumn("my_feature_enabled");
        
        return jdbcStateRepository;
    }

    @Bean
    public UserProvider userProvider() {
        return new SpringSecurityUserProvider();
    }

    @Bean
    public FeatureManager featureManager(FeatureProvider featureProvider, 
                                         StateRepository stateRepository,
                                         UserProvider userProvider) {
        return new FeatureManagerBuilder()
                .featureProvider(featureProvider)
                .stateRepository(stateRepository)
                .userProvider(userProvider)
                .build();
    }
}

在这个配置中:
featureProvider定义了应用程序中的功能。这里我们使用了EnumBasedFeatureProvider,假设你的功能是在一个名为MyFeatures的枚举中定义的。
stateRepository定义了功能状态的存储方式。这里我们使用了JDBCStateRepository,它将功能状态存储在数据库中。我们通过构造函数传入了一个DataSource对象,Spring会自动将其注入。
userProvider定义了如何获取当前用户。这里我们使用了SpringSecurityUserProvider,假设你的应用程序使用Spring Security进行用户管理。
featureManager是Togglz的核心对象,它将FeatureProvider, StateRepository和UserProvider组合在一起。

5、注意事项

1、多数据源中如果不指定数据源默认使用主数据源,若你指定了手动创建表,表不存在或者MyFeatures 中枚举值不存在时默认enabled=false,若表不存在切未指定手动创建表,则会在主数据源中创建表,请留意。
2、togglz是实时

小结

Togglz是一个强大的功能标记管理库,它使得Java应用程序能够更灵活、更可控地发布和管理功能。通过合理使用Togglz,可以显著降低功能发布的风险,提高应用程序的可维护性。同时,Togglz良好的扩展性和与主流框架的集成能力,使其成为Java开发者管理功能标记的首选工具。

需要demo或者其他问题请留言,看到即回。

  • 57
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Neoest

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值