Togglz With Spring Boot Integration
Togglz With Spring Boot Integration
Togglz is an implementation of the feature toggles pattern for Java. In the article, we will see how to integrate togglz into a spring boot project.
Join the DZone community and get the full member experience.Join For Free
In the disruptive world, business wants to experiment a feature for a time period in production, operational team inspect a feature with resource consumption, developers want to be up to date with trunk branch while their development spans for a long time. How all this happen and still application/product works without any hassles?
It can be solved by toggle points protected and accessed on demand which is called feature flags. To know more about, refer RefCard. Instead of many hanging custom made code, if we have library which can organize all the flags horizontally across the software it will be easy. So Togglz does that!
Togglz is an implementation of the Feature Toggles pattern for Java. It is easy to maintain the feature flags in agile development of continuous delivery practices. All the feature states are configured on one location and feature flags incorporated on the implemented places. State of a feature flags can be activated (enabled) or inactive(disabled) by a properties. It can also be done by activation strategy, like when this condition meets, activate the flag.
Togglz ships with the following default strategies:
The strategies are easily understandable from their names, if any details required, it hyperlinks to the actual togglz documentation. We can develop our own custom strategies for activation by implementing the interface ActivationStrategy. Togglz does the service loader mechanism for activation. To do that, we need to register implementation by creating the file called META-INF/services/org.togglz.core.spi.ActivationStrategy which tells the fully qualified implementation class.
Feature State repositories can be maintained in-memory, properties file based, jdbc for rdms databases, monogo databases. We can also add caching state for these repositories. One more thing, togglz requires user information to toggle based on users, so user provider to be configured with Togglz.
Going through a enum based feature implementation with in-memory state repository in a spring boot project. Below libraries to be integrated in the pom.xml. Togglz admin console is a web application to manage the feature flags which has been at the last.
Now declare the enum based feature list class. We can give the label which will appear in the admin console and annotated with @EnabledByDefault to enable it at the time of startup. isActive() helper method used to check from the feature context whether feature is active or not.
Configure the togglz for in memory state repository and inject the Spring security user provider.
To inject the feature provider in the spring beans, the following bean needs to be created
While implementing the rest api, we can add the flag check with the enum helper method to check the feature is active or not. It can also be done by injecting the feature manager to the class which has the provision to check
Now when we hit the api, we can see the response. Its because the feature is enabled by default.
Admin console shows all the feature with the labels provided in the enum class. It also show the settings to dynamically change the activation strategy.
By clicking on the settings, it provides the list of options available.
Working source code is available in github.
Opinions expressed by DZone contributors are their own.