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
Hoxton is the Spring Cloud release compatible with Spring Boot
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
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
togglz.features map includes the key
USE_NEW_SOMESERVICE also defined in the FeatureToggles enum file.
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.
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_SOMESERVICEfeature 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.
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.
Hope you enjoy this post, thanks for reading and feedback is always 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:
This website includes affiliate links to Google. This means if you click an affiliate link and buy a product, I might earn a commission at no extra cost to you.