This is a Spring Boot starter that integrates OpenFeature, an open standard for feature flag management.
- OpenFeature Integration: Easily toggle features and manage feature flags in your application.
- Spring Boot Framework: Built with Spring Boot for rapid development.
- Extensibility: Supports multiple feature flag providers via OpenFeature SDK.
Before running the application, ensure you have the following installed:
- Java 17 or higher
- Maven 3.8++
$ git clone https://github.com/iromu/spring-boot-openfeature.git
$ cd spring-boot-openfeature
OpenFeature allows you to integrate with a feature flag management provider. Update the configuration in
application.properties
or application.yml
.
For example, if you're using Unleash:
spring:
openfeature:
unleash:
app-name:${spring.application.name}
environment:development
unleash-api:http://unleash-instance:54242/api/
unleash-token:'default:development.your-api-key'
application:
name:UnleashApplication
Refer to the provider's documentation for specific configuration details.
Using Maven:
$ cd examples
$ mvn clean package
To test feature flags, create a simple feature toggle in your provider and use OpenFeature APIs in your code. Example:
import dev.openfeature.sdk.Client;
@RestController
public class FeatureController {
private final Client client;
public FeatureController(Client client) {
this.client = client;
}
@GetMapping("/feature-status")
public String getFeatureStatus() {
boolean isFeatureEnabled = client.getBooleanValue("my-feature", false);
return isFeatureEnabled ? "Feature is enabled!" : "Feature is disabled.";
}
@GetMapping("/user/{id}")
public Boolean featureOnUserId(@PathVariable("id") final String id) {
return client.getBooleanValue("users-flag", false, new ImmutableContext(Map.of("userId", new Value(id))));
}
}
Navigate to /feature-status
to see the feature toggle in action.
With a strategy defined like:
{
"name": "users-flag",
"enabled": true,
"strategies": [
{
"name": "userWithId",
"parameters": {
"userIds": "111,234"
}
}
]
}
@RestController
public class UserController {
@GetMapping("annotated/user/{id}")
@ToggleOnFlag(key = "users-flag", attributes = "{'userId': #id}", orElse = "featureOnUserIdDisabled")
public String featureOnUserIdAnnotated(@PathVariable("id") final String id) {
return "User allowed";
}
public String featureOnUserIdDisabled(final String id) {
return "User not allowed";
}
}
OpenFeature supports multiple feature flag providers, including:
- Unleash
- Split
- ...
To switch providers, replace the dependency and update configuration as per the provider's documentation.
Key dependencies for this project:
- Spring Boot Starter Web
- Spring Boot OpenFeature Starter
- OpenFeature Provider (e.g., Unleash or Split)
Add the OpenFeature SDK and provider dependencies to your pom.xml
or build.gradle
:
Maven
<project>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.iromu.openfeature</groupId>
<artifactId>spring-boot-starter-openfeature-unleash</artifactId>
<version>${spring-boot-openfeature.version}</version>
</dependency>
</dependencies>
</project>
Gradle
dependencyManagement {
imports {
mavenBom "org.iromu.openfeature:spring-boot-openfeature-dependencies:${springBootOpenFeatureDependenciesVersion}"
}
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.iromu.openfeature:spring-boot-starter-openfeature'
implementation 'dev.openfeature.contrib.providers:unleash'
}
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a new branch
- Make your changes and test thoroughly
- Submit a pull request
This project is licensed under the Apache License 2.0. See the LICENSE file for details.
Happy coding! 🚀