Java 如何根据Zuul的条件将请求从一条路径路由到不同的URL?
我有spring boot zuul路由web应用程序。我试图实现的是根据某些条件将请求从指定路径路由到不同的URL。所以,我在寻找这样的东西:Java 如何根据Zuul的条件将请求从一条路径路由到不同的URL?,java,spring-boot,url-routing,netflix-zuul,Java,Spring Boot,Url Routing,Netflix Zuul,我有spring boot zuul路由web应用程序。我试图实现的是根据某些条件将请求从指定路径路由到不同的URL。所以,我在寻找这样的东西: application.propertieszuul路由道具: zuul.routes.mypath.path=/mypath/** #lets say zuul web app hosted on localhost:7777 zuul.routes.mypath.url1=http://localhost:8888/path zuul.routes
application.properties
zuul路由道具:
zuul.routes.mypath.path=/mypath/** #lets say zuul web app hosted on localhost:7777
zuul.routes.mypath.url1=http://localhost:8888/path
zuul.routes.mypath.url2=http://localhost:9999/anotherpath
然后,可能使用Zuul Route Filter或smth,否则我想访问此url1
和url2
,并根据我的条件进行路由
另外,到目前为止,我已经有了使用Zuul路由过滤器和应用程序属性的工作解决方案,如下所示。我只需抓取过滤器中的my.custom.url.prop
,并在满足条件的情况下将请求路由到那里。但是,我在寻找正确的方法,Zuul
zuul.routes.mypath.path=/mypath/** #lets say zuul web app hosted on localhost:7777
zuul.routes.mypath.url=http://localhost:8888/path
my.custom.url.prop=http://localhost:9999/anotherPath
过滤器中的一些代码使问题更清楚:
public Object run() {
....
RequestContext ctx = RequestContext.getCurrentContext();
try {
if (isRoutingCondition(ctx.getRequest())) {
ctx.setRouteHost(urlFromMyCustomProp);
} else {
ctx.setRouteHost(RequestContext.getCurrentContext().getRouteHost());
}
} catch (MalformedURLException e) {
logger.error("", e);
}
return null;
}
我不确定我是否理解你的问题。Zuul已经基于URL模式路由URL。您在寻找URL模式以外的内容吗?我想找到一种方法,根据某些条件将对相同“URL模式”的请求路由到不同的路由(2个或更多不同的URL)。您的问题似乎与讨论的动态路由问题非常相似。@IndraBasak谢谢,请看一下。似乎我已经有了类似于本文建议的解决方案,但我正在尝试找到标准/正确的Zuul方法。尽管如此,这可能是Zuul能做到的唯一方法。