spring zuul中基于严格匹配的匹配路由

Csh*_*hah 6 spring-boot netflix-zuul

总结: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
Run Code Online (Sandbox Code Playgroud)

对于输入路径:"/v1/customer/card/product/" ,它应该选择 - http://localhost:8800/v1/customer/card/product但它选择 http://localhost:8400/v1/复合的。我的期望是路径模式匹配按指定的顺序发生并且更严格,但似乎它不会那样工作。

当您为类似的输入路径定义了多个路由时,您能告诉我 ZUUL 是如何工作的吗?

谢谢

PS - 当我在 AWS 中通过 Docker 运行时我可以看到这个问题,但是当我从 eclipse 运行时没有出现这个问题。zuul 路由的顺序取决于 spring zuul jar (spring-cloud-starter-netflix-zuul - 2.0.0.RELEASE vs 2.0.1.RELEASE)

小智 3

对于根据最佳匹配选择路线,我发现此线程很有帮助(感谢@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;
    }
}
Run Code Online (Sandbox Code Playgroud)

@maximdim:

我没有重复逻辑来查找“最佳匹配”,而是简单地从请求属性中获取它,Spring 的 HandlerMapping 将其保存在其中。也许有点老套,但似乎对我有用。