For example, as shown below, prepare a boolean variable called ʻuseNewAlgorithm to use a new algorithm, and use it in situations where the new algorithm and the old algorithm are used properly according to the true / false of ʻuseNewAlgorithm
.
function reticulateSplines(){
var useNewAlgorithm = false;
// useNewAlgorithm = true; // UNCOMMENT IF YOU ARE WORKING ON THE NEW SR ALGORITHM
if( useNewAlgorithm ){
return enhancedSplineReticulation();
}else{
return oldFashionedSplineReticulation();
}
}
function oldFashionedSplineReticulation(){
// current implementation lives here
}
function enhancedSplineReticulation(){
// TODO: implement better SR algorithm
}
FeatureFlag is often used when providing new features with a fixed release timing, canary releases, A / B testing, etc.
FF4J
https://ff4j.github.io/
FF4j is a Java library that can easily implement FeatureToggle.
--Real-time Toggle switching using Web Console and REST API --Persistence of state using various storages --Feature usage monitoring
Various functions such as are provided.
Create a project by selecting only Spring Reactive Web, lombok from Spring Initializr that everyone loves.
Add a dependency to pom.xml.
pom.xml
<dependency>
<groupId>org.ff4j</groupId>
<artifactId>ff4j-spring-boot-starter</artifactId>
<version>1.8</version>
</dependency>
Create Bean definition & Feature for FF4j
.
FF4JConfiguration.java
import org.ff4j.FF4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FF4JConfiguration {
@Bean
public FF4j ff4j() {
FF4j ff4j = new FF4j()
.createFeature(awesomeFeature())
.createFeature(greatFeature())
.createFeature(excellentFeature());
return ff4j;
}
private Feature awesomeFeature() {
return new Feature("AwesomeFeature", true);
}
private Feature greatFeature() {
return new Feature("GreatFeature", false);
}
private Feature excellentFeature() {
return new Feature("ExcellentFeature", false);
}
}
This time, we have defined three features, ʻAwesomeFeature,
GreatFeature, and ʻExcellentFeature
, and turned on only ʻAwesomeFeature`.
Implement Controller using FF4j. In the Controller that returns only a simple message, if the three defined features are turned on, it is added to the message according to the features.
GreetingController.java
@RestController
@RequiredArgsConstructor
public class GreetingController {
//Constructor injection
private final FF4j ff4j;
@GetMapping
public String greeting() {
List<String> features = new ArrayList<>();
addFeatures(features);
String greeting = String.format("Hello, %s World!!", String.join(" ", features));
return greeting;
}
//Processing of each feature
private void addFeatures(List<String> features) {
if(ff4j.check("AwesomeFeature")) {
features.add("Awesome");
}
if(ff4j.check("GreatFeature")) {
features.add("Great");
}
if(ff4j.check("ExcellentFeature")) {
features.add("Excellent");
}
}
}
Launch the application and go to http: // localhost: 8080.
By default, ʻAwesome Feature is enabled, so the message will be displayed as
Hello, Awesome World! `.
Next, install the FF4j management console. In the previous state, it was necessary to switch Feature on / off on the source code, but by using WebConsole, it is possible to switch Feature on / off while the application is running.
Add a dependency to pom.xml.
<dependency>
<groupId>org.ff4j</groupId>
<artifactId>ff4j-web</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
Add a new bean definition.
@Configuration
@ConditionalOnClass({ConsoleServlet.class, FF4jDispatcherServlet.class})
@AutoConfigureAfter(FF4JConfiguration.class)
public class FF4JWebConfiguration extends SpringBootServletInitializer {
@Bean
public ServletRegistrationBean<FF4jDispatcherServlet> ff4jDispatcherServletRegistrationBean(FF4jDispatcherServlet ff4jDispatcherServlet)
{
ServletRegistrationBean<FF4jDispatcherServlet> bean = new ServletRegistrationBean<>(ff4jDispatcherServlet, "/ff4j-web-console/*");
bean.setName("ff4j-console");
bean.setLoadOnStartup(1);
return bean;
}
@Bean
@ConditionalOnMissingBean
public FF4jDispatcherServlet getFF4jDispatcherServlet(FF4j ff4j) {
FF4jDispatcherServlet ff4jDispatcherServlet = new FF4jDispatcherServlet();
ff4jDispatcherServlet.setFf4j(ff4j);
return ff4jDispatcherServlet;
}
}
Start and access http: // localhost: 8080 / ff4j-web-console.
You can see the currently defined Features in the Features menu, Feature turns on / off as the toggle is turned on / off.
I will also post a demo of the contents explained above taken with GIF animation. For your reference.
I gave a brief introduction of FF4j. Feature Toggle is a powerful technique, but if you don't set an operation policy, a chaotic code base will be created, so I want to be careful when operating it. It is necessary to have a framework such as separating the features to be DI for each profile and defining coding standards and review points.
martinFowler.com - FeatureToggle: https://martinfowler.com/articles/feature-toggles.html FF4j: https://ff4j.github.io/ Repository used this time: https://github.com/IshinFUKUOKA/ff4j-demo
Recommended Posts