spring zuul中基于严格匹配的路由
Match route in spring zuul based on strict matching
总结:
ZUUL 没有为输入路径选择正确的目的地 url,因为它没有对输入路径进行严格匹配。
下面是我的 zuul 路线:
zuul:
routes:
auth:
path: /v1/txn/**
url: http://localhost:8900/v1/cardhostauth
cardproduct:
path: /v1/customer/card/product/**
url: http://localhost:8800/v1/customer/card/product
cardcomposite:
path: /v1/customer/**
url: http://localhost:8400/v1/composite
对于输入路径:“/v1/customer/card/product/”,应选择-http://localhost:8800/v1/customer/card/product but it chooses http://localhost:8400/v1/composite。我的期望是路径模式匹配按照指定的顺序发生并且更严格,但似乎不是那样工作的。
当你为相似的输入路径定义了多个路由时,你能告诉我 ZUUL 是如何工作的吗?
感谢
P.S - 当我在 AWS 中通过 Docker 运行 时,我可以看到这个问题,但是当我从 eclipse 运行 时,这个问题没有出现。 zuul 路由的顺序是否取决于 spring zuul jar (spring-cloud-starter-netflix-zuul - 2.0.0.RELEASE vs 2.0.1.RELEASE)
对于根据最佳匹配选择路线,我发现此线程很有帮助(感谢@maximdim)。基本上你可以编写自己的自定义路由器来选择最佳匹配路由。
https://github.com/spring-cloud/spring-cloud-netflix/issues/2408
希望这对您有所帮助。
(这是我的第一个答案,如果不是一个好的答案,我深表歉意。)
取自 github 线程的示例如下:
public class CustomRouteLocator extends SimpleRouteLocator {
public CustomRouteLocator(String servletPath, ZuulProperties properties) {
super(servletPath, properties);
}
@Override
protected ZuulRoute getZuulRoute(String adjustedPath) {
// Spring's AbstractUrlHandlerMapping already found best matching path for us
// and stored it into request attribute. See AbstractUrlHandlerMapping.exposePathWithinMapping
RequestContext ctx = RequestContext.getCurrentContext();
String bestMatchingPattern = (String)ctx.getRequest().getAttribute(HandlerMapping.class.getName() + ".bestMatchingPattern");
if (bestMatchingPattern == null) {
return super.getZuulRoute(adjustedPath);
}
if (!matchesIgnoredPatterns(adjustedPath)) {
return locateRoutes().get(bestMatchingPattern);
}
return null;
}
}
@maximdim:
Instead of duplicating logic to find 'best match' I simply getting it from request attribute, where Spring's HandlerMapping saves it. A bit hacky perhaps, but seems to work for me.
根据Zuul官方文档编辑答案:https://cloud.spring.io/spring-cloud-netflix/multi/multi__router_and_filter_zuul.html
If you need your routes to have their order preserved, you need to use
a YAML file, as the ordering is lost when using a properties file. The
following example shows such a YAML file:
application.yml.
zuul:
routes:
users:
path: /myusers/**
legacy:
path: /**
If you were to use a properties file, the legacy path might end up in
front of the users path, rendering the users path unreachable.
确保您使用的是 Spring Cloud 的最新稳定版本(当前 2.0.2)
我从 2 年前就开始使用 zuul,我认为使用 zuul 路由的最佳方法是使用一些发现服务,例如注册您的应用程序的 Eureka 服务。
所以你只需要添加 service-id
zuul:
routes:
auth:
path: /v1/txn/**
service-id: txn-service
strip-prefix: false
sensitiveHeaders:
cardproduct:
path: /v1/customer/card/product/**
service-id: card-service // here you need to put application name .
strip-prefix: false
sensitiveHeaders:
cardcomposite:
path: /v1/customer/**
service-id: customer-service
strip-prefix: false
sensitiveHeaders:
总结: ZUUL 没有为输入路径选择正确的目的地 url,因为它没有对输入路径进行严格匹配。
下面是我的 zuul 路线:
zuul:
routes:
auth:
path: /v1/txn/**
url: http://localhost:8900/v1/cardhostauth
cardproduct:
path: /v1/customer/card/product/**
url: http://localhost:8800/v1/customer/card/product
cardcomposite:
path: /v1/customer/**
url: http://localhost:8400/v1/composite
对于输入路径:“/v1/customer/card/product/”,应选择-http://localhost:8800/v1/customer/card/product but it chooses http://localhost:8400/v1/composite。我的期望是路径模式匹配按照指定的顺序发生并且更严格,但似乎不是那样工作的。
当你为相似的输入路径定义了多个路由时,你能告诉我 ZUUL 是如何工作的吗?
感谢
P.S - 当我在 AWS 中通过 Docker 运行 时,我可以看到这个问题,但是当我从 eclipse 运行 时,这个问题没有出现。 zuul 路由的顺序是否取决于 spring zuul jar (spring-cloud-starter-netflix-zuul - 2.0.0.RELEASE vs 2.0.1.RELEASE)
对于根据最佳匹配选择路线,我发现此线程很有帮助(感谢@maximdim)。基本上你可以编写自己的自定义路由器来选择最佳匹配路由。
https://github.com/spring-cloud/spring-cloud-netflix/issues/2408
希望这对您有所帮助。 (这是我的第一个答案,如果不是一个好的答案,我深表歉意。)
取自 github 线程的示例如下:
public class CustomRouteLocator extends SimpleRouteLocator {
public CustomRouteLocator(String servletPath, ZuulProperties properties) {
super(servletPath, properties);
}
@Override
protected ZuulRoute getZuulRoute(String adjustedPath) {
// Spring's AbstractUrlHandlerMapping already found best matching path for us
// and stored it into request attribute. See AbstractUrlHandlerMapping.exposePathWithinMapping
RequestContext ctx = RequestContext.getCurrentContext();
String bestMatchingPattern = (String)ctx.getRequest().getAttribute(HandlerMapping.class.getName() + ".bestMatchingPattern");
if (bestMatchingPattern == null) {
return super.getZuulRoute(adjustedPath);
}
if (!matchesIgnoredPatterns(adjustedPath)) {
return locateRoutes().get(bestMatchingPattern);
}
return null;
}
}
@maximdim:
Instead of duplicating logic to find 'best match' I simply getting it from request attribute, where Spring's HandlerMapping saves it. A bit hacky perhaps, but seems to work for me.
根据Zuul官方文档编辑答案:https://cloud.spring.io/spring-cloud-netflix/multi/multi__router_and_filter_zuul.html
If you need your routes to have their order preserved, you need to use a YAML file, as the ordering is lost when using a properties file. The following example shows such a YAML file:
application.yml.
zuul: routes: users: path: /myusers/** legacy: path: /**
If you were to use a properties file, the legacy path might end up in front of the users path, rendering the users path unreachable.
确保您使用的是 Spring Cloud 的最新稳定版本(当前 2.0.2)
我从 2 年前就开始使用 zuul,我认为使用 zuul 路由的最佳方法是使用一些发现服务,例如注册您的应用程序的 Eureka 服务。
所以你只需要添加 service-id
zuul:
routes:
auth:
path: /v1/txn/**
service-id: txn-service
strip-prefix: false
sensitiveHeaders:
cardproduct:
path: /v1/customer/card/product/**
service-id: card-service // here you need to put application name .
strip-prefix: false
sensitiveHeaders:
cardcomposite:
path: /v1/customer/**
service-id: customer-service
strip-prefix: false
sensitiveHeaders: