gpt4 book ai didi

java - Zuul 路由不能正确映射到功能区 listOfServers(没有 Eureka)

转载 作者:行者123 更新时间:2023-11-30 10:15:46 25 4
gpt4 key购买 nike

我正在尝试为我的 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 状态代码)。

最佳答案

将您的配置属性更改为以下 -

    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";
}

}

现在提出一个请求,这将适用于你想做的事情。

关于java - Zuul 路由不能正确映射到功能区 listOfServers(没有 Eureka),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50348962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com