概要
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或者其他问题请留言,看到即回。