当前位置: 代码网 > it编程>编程语言>Java > SpringCloudGateway路由失效问题

SpringCloudGateway路由失效问题

2024年10月31日 Java 我要评论
springcloudgateway整合nacos路由失效报错如下java.lang.illegalstateexception: invalid host: lb://mall_admin_serv

springcloudgateway整合nacos路由失效

报错如下

java.lang.illegalstateexception: invalid host: lb://mall_admin_service

负载均衡配置的没有问题,对应的服务名称也没有问题,断点跟踪了一下,在这个routetorequesturlfilter类中报错,

代码很简单

		if ("lb".equalsignorecase(routeuri.getscheme()) && routeuri.gethost() == null) {
			// load balanced uris should always have a host. if the host is null it is
			// most
			// likely because the host name was invalid (for example included an
			// underscore)
			throw new illegalstateexception("invalid host: " + routeuri.tostring());
		}

在读取配置文件的时候,没有解析到对应的host主机

		private static final string scheme_regex = "[a-za-z]([a-za-z]|\\d|\\+|\\.|-)*:.*";
		static final pattern schemepattern = pattern.compile(scheme_regex);
		// 部分代码截取
		if (hasanotherscheme(routeuri)) {
			// this is a special url, save scheme to special attribute
			// replace routeuri with schemespecificpart
			exchange.getattributes().put(gateway_scheme_prefix_attr,
					routeuri.getscheme());
			routeuri = uri.create(routeuri.getschemespecificpart());
		}

这里包含了一段正则表达式验证,验证配置的服务名称是否合法,很明显我们的是以下划线配置的,所以验证失败,uri.create创建的时候host解析失败配置为空的。

也能了解了一下springcloudgateway实现原理,所有的过滤器均以globalfilter 的子接口实现,同样是一些列的过滤器链。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com