Skip to content

This is a Spring Boot starter that integrates OpenFeature, an open standard for feature flag management.

License

Notifications You must be signed in to change notification settings

iromu/spring-boot-openfeature

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring Starter OpenFeature

Apache License 2 Build Status Sonar Coverage Coveralls Quality Gate Status Maven Central Maven metadata URL OpenSSF Scorecard

This is a Spring Boot starter that integrates OpenFeature, an open standard for feature flag management.

Features

  • 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.

Prerequisites

Before running the application, ensure you have the following installed:

  • Java 17 or higher
  • Maven 3.8++

Getting Started

1. Clone the Repository

$ git clone https://github.com/iromu/spring-boot-openfeature.git
$ cd spring-boot-openfeature

2. Configure OpenFeature Provider

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.

3. Build and Run the Example Applications

Using Maven:

$ cd examples
$ mvn clean package

4. Testing Feature Flags

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.

5. Annotations

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 Providers

OpenFeature supports multiple feature flag providers, including:

  • Unleash
  • Split
  • ...

To switch providers, replace the dependency and update configuration as per the provider's documentation.

Dependencies

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'
}

Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a new branch
  3. Make your changes and test thoroughly
  4. Submit a pull request

License

This project is licensed under the Apache License 2.0. See the LICENSE file for details.

Resources


Happy coding! 🚀

About

This is a Spring Boot starter that integrates OpenFeature, an open standard for feature flag management.

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Packages

 
 
 

Contributors 4

  •  
  •  
  •  
  •  

Languages