【问题标题】:Match route in spring zuul based on strict matchingspring zuul中基于严格匹配的路由匹配
【发布时间】:2019-03-15 00:36:30
【问题描述】:

总结: 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,但它选择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)

【问题讨论】:

    标签: spring-boot netflix-zuul


    【解决方案1】:

    为了根据最佳匹配选择路线,我发现这个线程很有帮助(感谢@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:

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

    【讨论】:

      【解决方案2】:

      根据Zuul官方文档编辑答案:https://cloud.spring.io/spring-cloud-netflix/multi/multi__router_and_filter_zuul.html

      如果您需要保留路线的顺序,则需要使用 一个 YAML 文件,因为使用属性文件时顺序会丢失。这 以下示例显示了这样一个 YAML 文件:

      application.yml.

       zuul:
        routes:
          users:
            path: /myusers/**
          legacy:
            path: /**
      

      如果您要使用属性文件,旧路径可能会在 用户路径前面,导致用户路径不可达。

      确保您使用的是最新的稳定版 Spring Cloud(目前为2.0.2

      【讨论】:

      • 你确定这将一如既往地确定吗?
      • 根据官方文档编辑。
      • 我一直在使用 yml。我做了更多分析,发现当我通过 docker 启动我的服务时没有出现问题,但是当我在 Kubernetes 集群中启动它并且 url 指向 kube 服务时,它就不起作用了。在我的情况下,它附加了输入路径,而在 docker 或 eclipse 中单独运行时它工作正常。
      【解决方案3】:

      我从 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:
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-26
        • 2021-08-21
        • 2016-06-05
        • 2017-11-08
        • 1970-01-01
        相关资源
        最近更新 更多