Files
spring-boot-demo/openfeature/README.md
T

68 lines
4.0 KiB
Markdown

# openfeature
Companion code for **Feature Flags in Spring Boot with OpenFeature and flagd** on
[ankurm.com](https://ankurm.com/spring-boot-feature-flags-openfeature-flagd/).
A Spring Boot 4.2 app evaluates feature flags through the vendor-neutral OpenFeature client, with flagd (a
small flag daemon that watches a JSON file) behind it. One flag file, one `/checkout` endpoint, and a test
suite that writes what it saw to `output/`.
| Piece | Version | Checked against |
|---|---|---|
| Spring Boot | 4.2.0 | `maven-metadata.xml` for `spring-boot-starter-parent` (GA, jar published 19 Nov 2026) |
| JDK | 25 | Temurin 25.0.5 |
| OpenFeature Java SDK `dev.openfeature:sdk` | 1.23.0 | `maven-metadata.xml` (`<release>`) |
| flagd provider `dev.openfeature.contrib.providers:flagd` | 0.15.0 | `maven-metadata.xml` (`<release>`) |
| flagd daemon | 0.19.0 | built with `go install github.com/open-feature/flagd/[email protected]` (the Go module proxy lists it as tagged 17 Nov 2026) |
## Run it
```bash
export JAVA_HOME=/path/to/jdk-25
export FLAGD_BIN=/path/to/flagd # or have flagd on the PATH
mvn -B test # 11 tests; transcripts 01-08 are written to output/
./scripts/run-all.sh # the above plus the HTTP demo (output/09)
```
The 6 tests in `LiveFlagdTest` start a real flagd process on free ports and are **skipped** if no flagd binary
is found; the other 5 need nothing but the JDK. To run the application by hand:
```bash
./scripts/run-flagd.sh & # flagd on :8013, watching flags/flags.json
mvn spring-boot:run # app on :8080
curl -H 'X-User-Id: u3' -H 'X-Tier: premium' localhost:8080/checkout
```
Set `flags.mode=file` (for example `--flags.mode=file`) to evaluate `flags/flags.json` inside the JVM and skip
the daemon.
## Layout
| Path | What it is |
|---|---|
| [`flags/flags.json`](flags/flags.json) | the flag definitions (boolean, string, int, object, targeting, fractional, disabled) |
| [`FlagConfig.java`](src/main/java/com/ankurm/openfeature/FlagConfig.java) | registers the provider, hook and event handlers once |
| [`CheckoutFlags.java`](src/main/java/com/ankurm/openfeature/CheckoutFlags.java) | the only class that knows flag keys and defaults |
| [`CheckoutController.java`](src/main/java/com/ankurm/openfeature/CheckoutController.java) | `GET /checkout`, returns every decision with its variant and reason |
| [`LoggingHook.java`](src/main/java/com/ankurm/openfeature/LoggingHook.java) | one log line per evaluation |
| [`LiveFlagdTest.java`](src/test/java/com/ankurm/openfeature/LiveFlagdTest.java) | transcripts 01-06 against a real daemon |
| [`FileResolverTest.java`](src/test/java/com/ankurm/openfeature/FileResolverTest.java) | transcript 07, no daemon |
| [`BothStatesTest.java`](src/test/java/com/ankurm/openfeature/BothStatesTest.java) | transcript 08, `InMemoryProvider` |
| [`scripts/http-demo.sh`](scripts/http-demo.sh) | transcript 09: flagd + the app + curl + a live flag edit + flagd killed |
## Captured output
| File | What it shows |
|---|---|
| [`output/01-typed-evaluation.txt`](output/01-typed-evaluation.txt) | five flag types and their reasons |
| [`output/02-targeting.txt`](output/02-targeting.txt) | targeting by tier and e-mail domain |
| [`output/03-gradual-rollout.txt`](output/03-gradual-rollout.txt) | 1000 users at 10% and 50%, stickiness |
| [`output/04-live-reload.txt`](output/04-live-reload.txt) | editing the flag file while connected |
| [`output/05-flagd-down.txt`](output/05-flagd-down.txt) | flagd absent at start, and killed mid-run |
| [`output/06-errors.txt`](output/06-errors.txt) | unknown flag, typo, wrong type |
| [`output/07-file-resolver.txt`](output/07-file-resolver.txt) | the same file evaluated with no daemon |
| [`output/08-test-both-states.txt`](output/08-test-both-states.txt) | testing code behind a flag |
| [`output/09-http-demo.txt`](output/09-http-demo.txt) | the whole thing over HTTP |
Timings and gRPC error text drift between runs; the transcripts keep only the first 50 characters of an error.