0

We are using spring boot microservices in our product , we are having up to 10 applications . In order to log we use Log4j MDC to generate transaction id and pass it along the services [http headers] using interceptors and filters and its working fine. The problem is we have to add interceptors and filters in all our applications (say 10) to track this transaction.Is there any way like creating jar and inject in our microservice applications.

Can we achieve this using with minimal code changes in all our application ?

adorearun
  • 119
  • 10

1 Answers1

0

From the filters project which builds a jar to be shared, provide a class which can be scanned by Spring to create the beans with a well defined name. For example:

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 {
  // ...
}

Then configure the app to scan the com.me.common.interceptors package to create the beans. It doesn't matter the code is in a jar.

Within the app, those beans can then be autowired by name, and registered as usual.

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

@Autowired
@Qualifier(InterceptorConfig .FILTER_BEAN_2)
private HandlerInterceptor interceptor2;
Andrew S
  • 2,331
  • 1
  • 8
  • 13