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 的子接口实现,同样是一些列的过滤器链。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论