1. pom.xml中添加mvn依赖
<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-spring-boot-starter</artifactId>
<version>2.6.1.Final</version>
</dependency>
<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-spring-security</artifactId>
<version>2.6.1.Final</version>
</dependency>
<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-console</artifactId>
<version>2.6.1.Final</version>
</dependency>
2. 定义feature-enums(可以根据需要确定是否定义isActive方法)
package package com.togglz.test;;
import org.togglz.core.Feature;
import org.togglz.core.annotation.EnabledByDefault;
import org.togglz.core.annotation.Label;
import org.togglz.core.context.FeatureContext;
import org.togglz.core.manager.FeatureManager;
public enum FeaturesToggle implements Feature {
@EnabledByDefault
@Label("test togglz")
HELLO_WORLD;
public boolean isActive() {
final FeatureManager featureManager = FeatureContext.getFeatureManager();
return featureManager.isActive(this);
// return FeatureContext.getFeatureManager().isActive(this);
}
}
3. 注册bean,可以在application配置文件中配置,也可以单独配置
(1)application配置文件中配置
package com.togglz.test;
import com.thoughtworks.dora.backend.togglez.FeaturesToggle;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.togglz.core.manager.EnumBasedFeatureProvider;
import org.togglz.core.spi.FeatureProvider;
import org.togglz.core.user.UserProvider;
import org.togglz.spring.security.SpringSecurityUserProvider;
@Configuration
public class AppConfiguration {
@Bean
public FeatureProvider featureProvider() {
return new EnumBasedFeatureProvider(FeaturesToggle.class);
}
@Bean
public UserProvider getUserProvider() {
return new SpringSecurityUserProvider("admin");
}
//存储方式可选(不配置也OK)
// @Bean
// public StateRepository getStateRepository() {
// return new FileBasedStateRepository(new File("feature-toggle.properties"));
// }
}
如果不使用togglz-spring-security包,userProvider也可以使用如下方式:
@Bean
public UserProvider getUserProvider() {
return () -> new SimpleFeatureUser("admin", true);
}
(2)指定配置文件
package com.togglz.test;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.togglz.core.Feature;
import org.togglz.core.manager.TogglzConfig;
import org.togglz.core.repository.StateRepository;
import org.togglz.core.repository.file.FileBasedStateRepository;
import org.togglz.core.repository.mem.InMemoryStateRepository;
import org.togglz.core.user.SimpleFeatureUser;
import org.togglz.core.user.UserProvider;
import org.togglz.spring.security.SpringSecurityUserProvider;
import java.io.File;
@Configuration
@Component
public class FeatureToggleConfig implements TogglzConfig {
@Bean
public Class<? extends Feature> getFeatureClass() {
return FeaturesToggle.class;
}
@Bean
public StateRepository getStateRepository() {
return new FileBasedStateRepository(new File("feature-toggle.properties"));
}
@Bean
public UserProvider getUserProvider() {
return new SpringSecurityUserProvider("admin");
}
}
其中State Repositories用于存储feature状态,可以使用InMemoryStateRepository、FileBasedStateRepository、JDBCStateRepository等方式,如果使用JDBCStateRepository存储,则可使用如下配置
import javax.annotation.Resource;
import javax.sql.DataSource;
//FeatureToggleConfig中的配置
@Resource
private DataSource dataSource;
//togglz_features为table name
@Bean
public StateRepository getStateRepository() {
return new JDBCStateRepository(dataSource,"togglz_features");
}
//application.yml中的配置
spring:
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useSSL=false&allowPublicKeyRetrieval=true
username: user
password: pwd
jpa:
show-sql: true
数据库中会自动创建如下表:
CREATE TABLE togglz_features (
FEATURE_NAME VARCHAR(100) PRIMARY KEY,
FEATURE_ENABLED INTEGER,
STRATEGY_ID VARCHAR(200),
STRATEGY_PARAMS VARCHAR(2000)
)
具体配置参考https://www.togglz.org/documentation/repositories.html
4. 配置application.yml文件
togglz:
enabled: true
feature-enums: com.togglez.test.FeaturesToggle
console:
enabled: true
path: /togglz-console
secured: false
feature-admin-authority: admin
use-management-port: false
5. 使用feature
public class MyClass {
private FeatureManager manager;
public MyClass(FeatureManager manager) {
this.manager = manager;
}
public void togglzTest() {
if (manager.isActive(HELLO_WORLD)) {
...
}
}
}
如果feature-enums定义了isActive方法,则可以使用一下语句
if (FeaturesToggle.HELLO_WORLD.isActive()){
...
}
6. 访问console:http://localhost:8080/togglz-console/index
参考链接:https://www.togglz.org/documentation/spring-boot-starter.html