スプリングブート機能フラグ


機能フラグは非常に広範なトピックであり、定期的に使用する言語に関係なく戻ってきている.コードに外部で機能を管理するか、コードの一部として展開するための広範なサポートを提供する複数のプラットフォームがあります.例のいくつかは
  • 打ち上げる
  • トグル
  • プロパティ( file )ベース
    そして、より多く.

  • 目次
  • Problem at hand
  • Project Setup
  • Dependencies
  • Feature Loading
  • Enhancement
  • Remarks

  • 手元の問題

    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.