スプリングブート機能フラグ
そして、より多く.
目次
手元の問題
This article specifically targets a case where you can add a toggle check before any method you want before actually getting into the method execution. Be it at a controller level to prevent an API to be executed OR a service method OR a repository method. This pattern can be extended with any feature toggle implementation you use.
Code for the blog can be found here.
vikasgarghb / blog-feature-flags-springスプリングブート機能フラグの使用法.
プロジェクト設定
To begin with you will need,
- Java 11+
- Gradle 7+
- IDE of your choice
- Spring Boot 2.5.4+
依存
Apart from the other spring dependencies, you will need to include spring-aop and aspectjweaver. There are much documentation available for Aspect based programming with Spring. Won't go into much details about it. But one thing which this article focuses on is how aspect can be used to call a method before executing actual method.
AspectJ's @Before
annotation allows us to accomplish this. As long as correct pointcut expression is provided, the method decorated with this annotation will be called before the actual method.
For eg,
@Before("execution (* blog.vgarg.features..*(..)) && @annotation(checkFeatureFlag)")
public void checkFeatureFlag(JoinPoint joinPoint, CheckFeatureFlag checkFeatureFlag) {
String flag = checkFeatureFlag.flag();
if (!featureFlagService.isFeatureFlagSet(flag)) {
throw new FeatureNotEnabledException();
}
}
@CheckFeatureFlag(flag = "flags.goodmorning")
@GetMapping("/good-morning")
public ResponseEntity<String> getGoodMorning() {
return new ResponseEntity<>(greetingService.getGoodMorning(), HttpStatus.OK);
}
checkFeatureFlag
method will be executed by spring before getGoodMorning
method gets executed.
機能負荷
So, for this post, I am using file based features but the approach can be extended to use with any other feature provider.
public interface FeatureFlagService {
boolean isFeatureFlagSet(String flag);
}
I have defined an interface with a simple single method to check the status of any given feature flag. As mentioned for this post the implementation reads the toggle from a file named, featureflags.properties
residing in src/main/resources
(can be anywhere as long as it is on classpath).
強化 As mentioned before this can be extended for other feature support providers. For eg, with launch darkly the implementation will involve using the SDK provided by them. As long as the LD setup is done correct way, using their SDK is pretty simple. More can be read here .
備考
This is very bare minimum implementation of the functionality and has the capability of being extended into more. This only applies when the feature check needs to be applied before the actual method. For eg, if a new API needs to be deployed but not released, this check can come in handy.
But as any other solution this is not one size fits all solution. Would be happy to hear from the community what solutions have they tried.
Reference
この問題について(スプリングブート機能フラグ), 我々は、より多くの情報をここで見つけました https://dev.to/vikasgarghb/feature-flags-with-spring-boot-3l1lテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol