zuul-路由不生效问题

2019-12-27  本文已影响0人  追梦小蜗牛
woman-wearing-black-blazer-sitting-on-wooden-bench-3309467.jpg

问题场景:

在用zuul网关做路径路由的时候,路径明明配置好了,但是就是路由不过去,producer是目标服务名,在eureka里面注册的。目标服务的url:/apple/small

配置信息:

spring.application.name=zuul-test
server.port=8888

eureka.client.serviceUrl.defaultZone=http://localhost:9000/eureka/

zuul.routes.producer.path=/apple/**

简单思考:

一开始想的是自己的语法写错了,因为这是一个基本的功能,没那么复杂。但是再三尝试配置的各种写法,依然不行。然后就意识到这个问题没表面看起来那么简单,然后就想一探究竟,看看到底是为什么,然后就开始跟踪源码了......一跟踪,就发现了好多秘密,发现问题应该就出在下面的这段代码里面:

protected Route getRoute(ZuulRoute route, String path) {
        if (route == null) {
            return null;
        }
        if (log.isDebugEnabled()) {
            log.debug("route matched=" + route);
        }
        String targetPath = path;
        String prefix = this.properties.getPrefix();
        if (prefix.endsWith("/")) {
            prefix = prefix.substring(0, prefix.length() - 1);
        }
        if (path.startsWith(prefix + "/") && this.properties.isStripPrefix()) {
            targetPath = path.substring(prefix.length());
        }
        if (route.isStripPrefix()) {
            int index = route.getPath().indexOf("*") - 1;
            if (index > 0) {
                String routePrefix = route.getPath().substring(0, index);
                targetPath = targetPath.replaceFirst(routePrefix, "");
                prefix = prefix + routePrefix;
            }
        }
        Boolean retryable = this.properties.getRetryable();
        if (route.getRetryable() != null) {
            retryable = route.getRetryable();
        }
        return new Route(route.getId(), targetPath, route.getLocation(), prefix,
                retryable,
                route.isCustomSensitiveHeaders() ? route.getSensitiveHeaders() : null,
                route.isStripPrefix());
    }

因为这里面好多截取路径的操作,应该就是在某个地方把我写的路径给改变了,然后继续找......
有行关键代码,这里面的第二个参数就是要找的路径:

    return new Route(route.getId(), targetPath, route.getLocation(), prefix,
                retryable,
                route.isCustomSensitiveHeaders() ? route.getSensitiveHeaders() : null,
                route.isStripPrefix());

进去这个方法看一下,对应里面的path,就是最后URI的路径,可以跟一下代码,这里就不跟了:

    public Route(String id, String path, String location, String prefix,
            Boolean retryable, Set<String> ignoredHeaders, boolean prefixStripped) {
        this(id, path, location, prefix, retryable, ignoredHeaders);
        this.prefixStripped = prefixStripped;
    }

既然,出现问题的位置大概锁定了,就好办了,debug一下代码,看看这个值是在哪里被修改的,如何修改的,这里有两个调试过程中的截图,这个图的targetPath的值是/,而我配置文件配置的是zuul.routes.producer.path=/apple/

zuul.png

下面这个图的targetPath的值是:/small,而我的路径是/apple/small


producer.png

也就是说程序自动的把路径截取掉了,那就要找找在哪个地方截取的,看下面的代码,当stripPrefix这个属性为true的时候,会处理里面的targetPath,会调用replaceFirst方法,把前面的值用空字符串替换掉,具体是如何替换的,你们可以写个demo,debug一下:

        if (route.isStripPrefix()) {
            int index = route.getPath().indexOf("*") - 1;
            if (index > 0) {
                String routePrefix = route.getPath().substring(0, index);
                targetPath = targetPath.replaceFirst(routePrefix, "");
                prefix = prefix + routePrefix;
            }
        }

关键的是stripPrefix这个属性,只有当它为true的时候,才会截取,那就去看看这个属性值的定义:

    /**
     * Flag saying whether to strip the prefix from the path before forwarding.
     */
    private boolean stripPrefix = true;

大概意思是说在请求之前会跳过前缀,它这个前缀指的是*前面的字符串,不知道它截取的目的是什么,还是因为版本的问题,留着以后研究。如果不想让被截取,我们可以在配置文件里面配置这个属性值为false,它默认的值是true。

zuul.routes.producer.strip-prefix=false

总结:在编程的过程中,大部分碰到的问题,如果尝试了几种常见的解决方案解决不了的话,赶紧深入进去,查看源码,看看它的工作机制,这样才能更快的定位问题,找到最后的解决方案。孤独和失败并不可怕,真正可怕的是自卑。

上一篇下一篇

猜你喜欢

热点阅读