2

I am trying to configure ribbon for my zuul routes, so when I go to http://host1:port1/restful-service/app1 it should route me to http://host2:port2/rest-example/app1. It works properly when I define the route with "url" property, without using ribbon:

zuul:
  routes:
      restful-service:
          path: /restful-service/**
          url: http://host2:port2/rest-example

But when I try to use ribbon, which looks like this:

zuul:
  routes:
    restful-service:
      path: /restful-service/**
      serviceId: restful-service

ribbon:
  eureka:
    enabled: false

restful-service:
  ribbon:
    listOfServers: host2:port2/rest-example

It only allows me to route to http://host2:port2/rest-example but not to the chosen service directly http://host2:port2/rest-example/app1 (it returns 404 status code).

g00glen00b
  • 41,995
  • 13
  • 95
  • 133
Prokopatti
  • 23
  • 5

2 Answers2

2

Change your configuration properties to the following -

    zuul:
        routes:
              restful-service:
                       serviceId:restful-service
                       stripPrefix:false

   ribbon:
        eureka:
             enabled:false

   restful-service:
        ribbon:
            listOfServers: host2:port2

Then you will need to write a zuul Pre-Filter to change the requestURI

      public class CustomPreFilter extends ZuulFilter {

      public Object run() {

       RequestContext context=RequestContext.getCurrentContext();
       String oldrequestURI=(String) context.get("requestURI");
       String newrequestURI=oldrequestURI.replace("restful-service", "rest-example");
       context.put("requestURI",newrequestURI);
       return null;
     }

      public boolean shouldFilter() {

       HttpServletRequest httpServletRequest=RequestContext.getCurrentContext().getRequest();
    if(httpServletRequest.getRequestURI().contains("/restful-service"))
        return true;
    else
        return false;
   }

       @Override
       public int filterOrder() {
       return PreDecorationFilter.FILTER_ORDER+1;
   }

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

 }

Now make a request, this will work for what you want to do.

Indraneel Bende
  • 3,196
  • 4
  • 24
  • 36
  • Thanks for the solution. Unfortunately it seems like it only works for only one service, as there are hardcoded names in the filter. My example contains one service, but the ultimate configuration will contain few of them. – Prokopatti May 29 '18 at 07:27
  • Could u edit ur question with all ur requirements. I can edit my answer and try to make it generic. – Indraneel Bende May 29 '18 at 12:26
  • Or u can just write multiple filters for different routes. Just change the criteria in should filter. – Indraneel Bende May 29 '18 at 12:28
0

the listOfServers property only supports host and port, not path.

Prokopatti
  • 23
  • 5