zuul 路由不生效_zuul-路由不生效问题

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 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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值