SpringCloudGateway路由失效问题
目录
- SpringCloudGateway整合nacos路由失效
- 报错如下
- 代码很简单
- 总结
SpringCloudGateway整合nacos路由失效
报错如下
Java.lang.IllegalStateException: Invalid host: lb://mall_admin_service
负载均衡配置的没有问题,对应的服务名称也没有问题,断点跟踪了一下,在这个RouteToRequestUrlFhttp://www.devze.comilter类中报错,
代码很简单
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 PZWsHgkzCKattern 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创建的www.devze.com时候host解析失败配置为空的。
也能了解了一下SpringCloudGateWay实现原理,所有的过滤器均以GlobalFilter 的子接口实现,同样是一些列的过滤器链。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程客栈(www.devze.com)。
加载中,请稍侯......
精彩评论