ff4j 是一個很不錯的特性開關開發框架,同時官方也提供了spring boot starter 如下是一個簡單的學習試用html
docker方式運行
參考自官方文檔,同時官方也提供了幾個全家桶的集成(基於docker)java
- docker 啓動
docker run -d -p 8090:8080 clunven/ff4j:ff4j-sample-springboot
- 訪問效果
- web console
開啓特性
git
- web 效果
代碼模式
- 項目結構
├── pom.xml
└── src
├── main
│ ├── java
│ │ └── com
│ │ └── dalong
│ │ ├── Application.java
│ │ ├── FF4JConfiguration.java
│ │ └── SampleResource.java
│ └── resources
│ └── ff4j-features.xml
└── test
└── java
- pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dalong</groupId>
<artifactId>ff4j-learning</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.15.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.ff4j</groupId>
<artifactId>ff4j-spring-boot-starter</artifactId>
<version>1.8.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- ff4j-features.xml
<?xml version="1.0" encoding="UTF-8" ?>
<ff4j xmlns="http://www.ff4j.org/schema/ff4j"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.ff4j.org/schema/ff4j http://ff4j.org/schema/ff4j-1.4.0.xsd">
<features>
<feature uid="AwesomeFeature" enable="false" description="some desc">
</feature>
</features>
<properties>
<property name="maxLoginAttempts" type="int" value="12"/>
</properties>
</ff4j>
- 代碼說明
主要是ff4j 以及rest api 的
FF4JConfiguration.java:
package com.dalong;
import org.ff4j.FF4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FF4JConfiguration {
@Bean
public FF4j getFF4j() {
// You cen define ff4j the way you like
// the simplest is to use XML and InMemory but there are dozens of DB available.
return new FF4j("ff4j-features.xml");
// Please add ff4j-store-springjdbc for this sample to work..and a Datasource
//FF4j ff4j = new FF4j();
//ff4j.setFeatureStore(new FeatureStoreSpringJdbc(myDataSource));
//ff4j.setPropertiesStore(new PropertyStoreSpringJdbc(myDataSource));
//ff4j.setEventRepository(new EventRepositorySpringJdbc(myDataSource));
// Enable auditing
//ff4j.audit(true);
// If feature not found in DB, automatically created (as false)
//ff4j.autoCreate(enableAutoCreate);
}
}
- SampleResource.java
package com.dalong;
import org.ff4j.FF4j;
import org.ff4j.spring.autowire.FF4JFeature;
import org.ff4j.spring.autowire.FF4JProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SampleResource {
// BeanPostProcessor will inject value at runtime, soon source for default ConfigurationProperties
@FF4JProperty("maxLoginAttempts")
private int maxLoginAttempts;
@FF4JFeature(value = "AwesomeFeature")
private boolean awesomeFeature;
@Autowired
private FF4j getFF4j;
@RequestMapping(value = "/", method = RequestMethod.GET, produces = "text/html")
public String sayHello() {
StringBuilder response = new StringBuilder("<html><body><ul>");
response.append("<p>Is <span style=\"color:red\">Awesome</span> feature activated ? from ff4j.check(\"AwesomeFeature\") <span style=\"color:blue\">");
response.append(getFF4j.check("AwesomeFeature"));
response.append("</span></body></html>");
return response.toString();
}
@RequestMapping(value = "/maxLoginTries", method = RequestMethod.GET)
public Integer getMaxTries() {
return maxLoginAttempts;
}
}
- 啓動
mvn spring-boot:run
- 效果
說明
以上是一個簡單的試用,實際中最好的是結合持久化存儲方便的處理特性開發github
參考資料
https://github.com/ff4j/ff4j-samples/tree/master/ff4j-sample-springboot-starter
https://github.com/ff4j/ff4j/wiki/Getting-Started
web