ff4j spring boot 基本试用

ff4j 是一个很不错的特性开关开发框架,同时官方也提供了spring boot starter 以下是一个简单的学习试用

docker方式运行

参考自官方文档,同时官方也提供了几个全家桶的集成(基于docker)

  • docker 启动
docker run -d -p 8090:8080 clunven/ff4j:ff4j-sample-springboot
  • 访问效果

  • web console

开启特性

  • 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
  • 效果

说明

以上是一个简单的试用,实际中最好的是结合持久化存储方便的处理特性开发

参考资料

https://github.com/ff4j/ff4j-samples/tree/master/ff4j-sample-springboot-starter
https://github.com/ff4j/ff4j/wiki/Getting-Started

原文地址:https://www.cnblogs.com/rongfengliang/p/12734686.html