4

I am creating a sample consist of 3 (Service A, Service B and Service C) microservices. All the 3 services along with the gateway service ( zuul ) get registered with eureka. My use case is :

1) All the request will enter the system via gateway service -- working

2) For every request targeted for Service C, gateway service should first call Service A to perform certain action. On positive response from Service A, the request should get forwarded to Service B. Once we get positive response from both Service A and Service B, the request should finally be forwarded to Service C.

I want to achieve the above use case dynamically using zuul routing filter and eureka. I looked into PreDecoration filter and tried to the following. My gateway service is running 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;
    }

}
krajwade
  • 133
  • 1
  • 12
  • First of all you need to write what happened when you ran this code so that someone could help you. You are asking for someone else to write the code for you this not the place to do that. Also if for one single request you need to traverse through different services based on success response the logic should be in the service itself like the request will go to service A it will do its work on success will call service B then on success of service B call service C. You need to have a mechanism for services to talk to each other. i doubt zuul will handle that for you but i might be wrong. – Grinish Nepal Sep 10 '16 at 16:03
  • Thanks @Grinish for a quick response. When I run the above code, the zuul directly forwards the request to Service C. It should atleast get forwarded to Service A. Also the solution that you suggested needs a tight integration between Service B and other Services. I can have many microservices and request to all the services should first pass through Service A, then B and finally to the requested service. With your solution, Service B needs to be aware of all the service and based on the request type it will forward the request to a particular service. This is not what I intend to do. – krajwade Sep 11 '16 at 06:19
  • can i see your zuul routing that you have in application.yml that way you can forward the request to Service A then make service A, call Service B and then Service C on every success response But I still doubt zuul can do that for you. It will have to be first service that Zuul forwards to will have to call all other that you need. – Grinish Nepal Sep 11 '16 at 16:26
  • This is my zuul routing. The idea is to use Zuul pre filter for routing request to service A and B and then finally to C `zuul: routes: all: path: /** url: http://peer1:876` – krajwade Sep 11 '16 at 16:40

1 Answers1

2

As suggested by @Grinish Looks like Zuul does not support what I intended to do. I ended up using Feign Client to invoke Service A and B from inside the Zuul pre filter.

Community
  • 1
  • 1
krajwade
  • 133
  • 1
  • 12