Zuul dynamic routing using prefilter and eureka

I am creating a sample consisting of microservices 3 (services A, services B and services C). All 3 services together with the gateway (zuul) are registered in eureka. My use case:

1) The entire request will enter the system through the gateway - working

2) For each request destined for service C, the gateway service must first call service A to perform certain actions. With a positive response from service A, the request should be sent to service B. As soon as we receive a positive response from both service A and service B, the request should finally be sent to service C.

I want to achieve the above use case dynamically using zuul routing filter and eureka. I looked at PreDecoration and tried the following. My gateway service runs on port 8080

zuul:
  routes:
    all:
      path: /** 
      url: http://localhost:8761


public class CustomFilter extends ZuulFilter{

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        ctx.set("serviceId", "service-a");
        ctx.setRouteHost(new URL("http://localhost:8080"));

        return null;
    }

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 1;
    }

}
+4
source share
1 answer

As suggested by @Grinish It seems that Zuul does not support what I intended to do. I ended up using Feign Client to call Services A and B from the Zuul prefilter.

+2
source

Source: https://habr.com/ru/post/1654116/


All Articles