Search results
Refreshing Feature Flags using Togglz and Spring Cloud Config Server
1. OVERVIEW
Your team decided to hide a requirement implementation behind a feature flag using Spring Boot and Togglz.
Now it’s time to switch the toggle state to make the new implementation available. It might also be possible your team needs to switch the flag back if anything goes wrong.
togglz-spring-boot-starter Spring Boot starter’s autoconfigures an instance of FileBasedStateRepository. This requires you to restart your application after changing a toggle value.
You could configure a different StateRepository implementation such as combining JDBCStateRepository or MongoStateRepository with CachingStateRepository to prevent restarting your application.
This blog post helps you with the configuration and implementation of Togglz feature flags to reload new toggles values using Spring Cloud Config Server and Git.
2. APPLICATION DEPENDENCIES
pom.xml
:
Hoxton
is the Spring Cloud release compatible with Spring Boot 2.2.11.RELEASE
.
If you are using a different Spring Boot version, make sure you also use its corresponding Spring Cloud version.
You need spring-cloud-starter-config for this application to read external, Git-backed togglz states via a Spring Cloud Config Server. External configuration helps to prevent changing local togglz configuration, rebuilding, redeploying, and bouncing the application.
3. SPRING CLOUD CLIENT CONFIGURATION
bootstrap.yml
:
We’ll use a local Spring Cloud Config Server listening on 8100
. A file named after this application, springboot2-refresh-togglz.yml
in the Git repository the Configuration Server connects to.
https://bitbucket.org/asimio/demo-config-properties/src/master/springboot2-refresh-togglz.yml has the same togglz flag, togglz.features.USE_NEW_SOMESERVICE.enabled
as application.yml.
4. CONFIGURING FEATURE TOGGLES
application.yml
:
FeatureToggles.java
:
Notice that togglz.features
map includes the key USE_NEW_SOMESERVICE
also defined in the FeatureToggles enum file.
AppConfig.java
:
This is the same code as in Adding Feature Toggles to Spring Boot applications using Togglz.
It still doesn’t account for refreshing the relevant @Bean-annotated methods.
Adding @RefreshScope to the @Bean proxiedSomeService() method prevents the application from starting. It fails to autowire a required bean.
Adding @RefreshScope to the @Bean someService() method doesn’t refresh the right bean.
4.1. CONFIGURING TOGGLZ’s StateRepository
Going through togglz-spring-boot-starter source code I found TogglzAutoConfiguration$StateRepositoryConfiguration.stateRepository().
You just need to borrow the same implementation and add the @RefreshScope annotation.
AppConfig.java
: (Contd):
TogglzAutoConfiguration gets out of the way and allows us to provide our own StateRepository bean.
5. USING FEATURE TOGGLES
- Send a request to the application results in using the legacy service class implementation.
- Enable the
USE_NEW_SOMESERVICE
feature flag found in the Git repository at https://bitbucket.org/asimio/demo-config-properties/src/master/springboot2-refresh-togglz.yml:
- Refresh the application to reload the new value and to refresh the impacted beans:
- Resend same request now results in switching to new service class implementation.
6. CONCLUSION
This blog post helps you to dynamically switch between feature flag states without the need to restart your application using Togglz and Spring Cloud Config Server.
togglz-spring-boot-starter gets out of the way and allows you to provide your own StateRepository bean to switch your implementation toggle without bouncing your application.
Thanks for reading and as always, feedback is very much appreciated. If you found this post helpful and would like to receive updates when content like this gets published, sign up to the newsletter.
7. SOURCE CODE
Accompanying source code for this blog post can be found at:
- https://bitbucket.org/asimio/springboot2-refresh-togglz
- https://bitbucket.org/asimio/demo-config-properties/src/master/springboot2-refresh-togglz.yml