我正在尝试为我的http://host2:port2/rest-example/app1路由配置ribbon,所以当我转到zuul时,它应该会将我路由到zuul。当我使用"url“属性定义路由时,它可以正常工作,而不使用ribbon:
zuul:
routes:
restful-service:
path: /restful-service/**
url: http://host2:port2/rest-example但是当我尝试使用ribbon时,它看起来像这样:
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状态码)。
发布于 2018-05-20 13:46:19
将您的配置属性更改为以下内容-
zuul:
routes:
restful-service:
serviceId:restful-service
stripPrefix:false
ribbon:
eureka:
enabled:false
restful-service:
ribbon:
listOfServers: host2:port2然后,您需要编写一个zuul预过滤器来更改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";
}
}现在提出一个请求,这将适用于您想要做的事情。
发布于 2018-05-16 16:17:58
listOfServers属性仅支持主机和端口,不支持路径。
https://stackoverflow.com/questions/50348962
复制相似问题