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.
Asked
Active
Viewed 346 times
1 Answers
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
-
Thanks for reply so what will be the mechanism for passing to other services in API and Kafka events? Will it be forwarded manually. – user2425109 Feb 15 '23 at 03:18
-
Please check enriched answer – mdoflaz Feb 15 '23 at 04:05