Zuul路线没有使用功能区listOfServers(没有Eureka)进行财产映射

时间:2018-05-15 11:23:00

标签: java spring-cloud spring-cloud-netflix netflix-ribbon

我正在尝试为我的zuul路线配置功能区,所以当我转到http://host1:port1/restful-service/app1时,它应该将我转到http://host2:port2/rest-example/app1。 当我使用" url"定义路线时,它可以正常工作属性,不使用功能区:

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

但是当我尝试使用功能区时,看起来像这样:

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

ribbon:
  eureka:
    enabled: false

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

它只允许我路由到http://host2:port2/rest-example而不是直接路由到所选服务http://host2:port2/rest-example/app1(它返回404状态代码)。

2 个答案:

答案 0 :(得分:0)

listOfServers属性仅支持主机和端口,而不支持路径。

答案 1 :(得分:0)

将配置属性更改为以下内容 -

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

   ribbon:
        eureka:
             enabled:false

   restful-service:
        ribbon:
            listOfServers: host2:port2

然后您需要编写一个zuul Pre-Filter来更改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";
   }

 }

现在发出请求,这将适用于您想要做的事情。