2017-11-15 2 views
0

当社では、Springブートマイクロサービスを使用しており、最大10のアプリケーションを使用しています。ログを出力するために、Log4j MDCを使用してトランザクションIDを生成し、インターセプタとフィルタを使用してサービス[httpヘッダー]に沿って渡します。 問題は、このトランザクションを追跡するために、すべてのアプリケーション(たとえば10)にインターセプタとフィルタを追加する必要があることです.Jarを作成し、マイクロサービスアプリケーションに挿入するような方法はありません。外部依存性を導入するSpringマイクロサービス

すべてのアプリケーションで最小のコード変更でこれを使用できますか?

答えて

0

jarファイルを共有するフィルタプロジェクトから、Springでスキャンしての定義名がのBeanを作成するクラスを提供します。たとえば、次のように続いて

package com.me.common.interceptors; 

public class InterceptorConfig { 
    public static final String INTERCEPTOR_BEAN_1 = "comMeCommonInterceptorsInterceptor1"; 
    public static final String INTERCEPTOR_BEAN_2 = "comMeCommonInterceptorsInterceptor2"; 

    @Bean(name = INTERCEPTOR_BEAN_1) 
    public HandlerInterceptor getInterceptor1() { 
     return new Interceptor1(); 
    } 

    @Bean(name = INTERCEPTOR_BEAN_1) 
    public HandlerInterceptor getInterceptor2() { 
     return new Interceptor2(); 
    } 

} 


public class Interceptor1 implements HandlerInterceptor { 
    // ... 
} 

public class Interceptor2 implements HandlerInterceptor { 
    // ... 
} 

configure the app to scan Beanを作成するcom.me.common.interceptorsパッケージ。コードが瓶に入っていても問題ありません。

アプリ内では、これらのBeanを名前で自動割り当てすることができます。registered as usual

@Autowired 
@Qualifier(InterceptorConfig .FILTER_BEAN_1) 
private HandlerInterceptor interceptor1; 

@Autowired 
@Qualifier(InterceptorConfig .FILTER_BEAN_2) 
private HandlerInterceptor interceptor2; 
関連する問題