0

I am using spring boot microservice. Having requirement where MS X generated some custom correlation ID when some flow is started. Now this MS X is calling some API of MS Y, MS Y is sending event to MS Z and so on. Requirement is correlation id generated by MS X should be passed in headers through out the flow. What will be best technique to keep passing this automatically.

user2425109
  • 347
  • 1
  • 2
  • 10

1 Answers1

0

You can create a custom filter by implementing Filter interface.

Inside doFilter method of your filter, you can create and direct the header -which includes correlation id-.

public class CustomFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String correlationId = request.getHeader("correlation-id");
        
        //further check correlation id value
        //generate if null or log if needed 
        //and store in a context to use it further

        filterChain.doFilter(request, servletResponse);
    }
}

Further, in the service flow, you will need to read correlation id from context. Later on, you can pass it accordingly based on how you build inter-service communication.

mdoflaz
  • 551
  • 6
  • 19