SpringCloudGateway路由失效

2022-07-06 11:37:57

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)thrownewIllegalStateException("Invalid host: "+ routeUri.toString());}

在读取配置文件的时候,没有解析到对应的host主机
在这里插入图片描述

privatestaticfinal String SCHEME_REGEX="[a-zA-Z]([a-zA-Z]|\\d|\\+|\\.|-)*:.*";staticfinal 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 的子接口实现,同样是一些列的过滤器链。

总结—所有的服务名称均不能带有下划线

  • 作者:Xiao_zuo_ya
  • 原文链接:https://blog.csdn.net/Yue_zuozuo/article/details/104132922
    更新时间:2022-07-06 11:37:57