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

posted on   荣锋亮  阅读(1757)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2019-04-19 转载一篇阿里云Terraform 开发指南
2019-04-19 zabbix 4.2 支持 timescledb 了
2018-04-19 使用distillery 实现版本的动态升级&& 动态降级
2018-04-19 使用distillery 构建专业的 phoenix 项目软件包
2018-04-19 mix deps HEX_HTTP_CONCURRENCY=1 HEX_HTTP_TIMEOUT=120 timeout
2018-04-19 elixir jenkins 集成构建方式配置
2018-04-19 phoenix 使用activerecord模式框架ecto 访问数据库

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示