Feature flagging with azure app configurations.
Charu Jain
Java, Spring Boot, Kafka, Microservices,Docker, Azure, APIM, Redis, EDD, MS Pattern,Mambu, NeoBanks, Banking Modernization Expert
What is feature flagging?
Feature flags (also commonly known as feature toggles) is a software engineering technique that turns select functionality on and off during runtime, without deploying new code.
Advantages:
Feature management helps developers address the following problems:
Feature Management Platforms:
There are widely recognised feature management platforms (FMP) for maintaining the feature flags. LaunchDarkly, Flagsmith, Flagship, Unleash, BulletTrain and Split are a few of the major feature management solution providers that can be hosted in aks clusture.
Third Party SAAS based solution's are also available which provides JAVA SDK and Android SDK for backend and frontend services and also provides user segmentation facilities but they have cost associated with them. e.g Harness, Optimizely
Azure application configuration for Feature flagging:
In this blog post we will be mainly focussing on azure app config for feature flagging.
Azure App Config is recommended as it has lower cost and provides all the desired features and functionalities for feature flagging such as user segmentation and audit trail. Also Azure App Config is also used by any platform for storing application configurations.
Manage feature flags in Azure App Configuration
We can store all feature flags in Azure App Configuration and administer them from a single place. App Configuration has a portal UI named Feature Manager that's designed specifically for feature flags.
We can add feature flags from below screen:
We can also edit the feature flag and enable filtering/targetting on them from below screen:
Here we have set targetting on Users with email: [email protected] and Groups: contoso.com
These settings result in the following behavior:
领英推荐
Integrating Feature flagging in spring boot applications:
1) Create a spring boot project with Spring Initializr and choose maven as build tool.
2) Open the pom.xml file in a text editor and add the following to the list of <dependencies>:
<dependency
<groupId>com.azure.spring</groupId>
<artifactId>spring-cloud-azure-appconfiguration-config-web</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>spring-cloud-azure-feature-management-web</artifactId>
<version>5.2.0</version>
</dependency>>
3) Navigate to the resources directory of your app and open bootstrap.properties. If the file does not exist, create it. Add the following line to the file.
spring.cloud.azure.appconfiguration.stores[0].connection-string= ${APP_CONFIGURATION_CONNECTION_STRING}
spring.cloud.azure.appconfiguration.stores[0].feature-flags.enabled=true
Set an environment variable named APP_CONFIGURATION_CONNECTION_STRING, and set it to the connection string to your App Configuration store.
4) Create a new Java file named HelloController.java in the package directory of your app.
@RestController
public class HelloController {
?@Autowired
private FeatureManager featureManager;
@GetMapping("/demo")
public String demo() {
if (Boolean.TRUE.equals(featureManager.isEnabledAsync("FeatureA").block())) {
return "enabled"; // actual code base that needs to be enabled
}
return "disabled";
}
};
Enabling FF for Targeting:
Referring to our above image, lets say we want feature flags to be enabled only for user with email: [email protected] and group contoso.com
Then, We have to register this bean:
@Configuration
public class TargetConfiguration {
@Bean("Microsoft.Targeting")
public TargetingFilter targetingFilter(TargetingContextAccessor contextAccessor) {
return new TargetingFilter(contextAccessor, new TargetingEvaluationOptions().setIgnoreCase(true));
}
}
Also have to create another component class:
@Component
public class TargetingContextAccessorImpl implements TargetingContextAccessor {
@Override
public void configureTargetingContext(TargetingContext context) {
String email = "[email protected]"
context.setUserId(email);
ArrayList<String> groups = new ArrayList<String>();
groups.add(email.split("@")[1]);
context.setGroups(groups);
}
}
This way targetting is enabled in our spring code.
Apart from Targetting, we can do filtering on time and can other custom filters too. Hence, feature flagging with azure is an easy, efficient and effective way to manage our code.
References:
https://learn.microsoft.com/en-us/azure/azure-app-configuration/manage-feature-flags
https://learn.microsoft.com/en-us/azure/azure-app-configuration/quickstart-feature-flag-spring-boot?tabs=spring-boot-3%2Cwindowscommandprompt
Head Of Engineering
1 年She is continuously filling gap which generally required in micro service project. She has built lot of cross cutting libraries which accelerate MS project.