Gateway动态路由引发的请求404

项目场景:

nacos+gateway 做路由转发


问题描述

请求gateway地址:

gateway/jeecg-boot/income/statistics/login/page/

期望转发到目标服务地址:

ems-back/income/statistics/login/page/

路由配置

	{
		"id": "ems-back",
		"order": 1,
		"predicates": [
			{
				"name": "Path",
				"args": {
					"_genkey_0": "/jeecg-boot/**"
				}
			}
		],
		"filters": [
			{
				"name": "RewritePath",
				"args": {
					"_genkey_0": "/jeecg-boot/(?.*)",
					"_genkey_1": "/$\\{segment}"
				}
			}
		],		
		"uri": "lb://ems-back"
	}

结果请求到地址: 

jeecg-boot/income/statistics/login/page/

最终j请求到错误的服务导致404

原因分析:

gateway router 转发到错误的服务,应该是错误的配置了路由信息。

1.检查naocos的gateway路径配置文件,发现无jeecg-boot的路由配置

2.检查gateway 本地配置信息,也无jeecg-boot的路由配置

3.检查gateway的启动配置,发现gateway启动了动态路由配置

    gateway:
      discovery:
        locator:
          # TODO 动态路由,会把jeecg-boot开头的请求 转发到jeecg-boot的舞曲,order = 0;
          enabled: true

断点排查

FilteringWebHandler 类

    public Mono handle(ServerWebExchange exchange) {
        Route route = (Route)exchange.getRequiredAttribute(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR);
        List gatewayFilters = route.getFilters();
        List combined = new ArrayList(this.globalFilters);
        combined.addAll(gatewayFilters);
        AnnotationAwareOrderComparator.sort(combined);
        if (logger.isDebugEnabled()) {
            logger.debug("Sorted gatewayFilterFactories: " + combined);
        }

        return (new DefaultGatewayFilterChain(combined)).filter(exchange);
    }

route 信息为:

 id = "ReactiveCompositeDiscoveryClient_jeecg-boot"
 uri = {URI@14666} "lb://jeecg-boot"
 order = 0
 predicate = {AsyncPredicate$DefaultAsyncPredicate@14667} "Paths: [/jeecg-boot/**], match trailing slash: true"
 gatewayFilters = {ArrayList@14668}  size = 1
 metadata = {HashMap@14669}  size = 6  

该uri地址为错误的服务

原因找到,gateway 自动配置了路由信息,并且该路由的order=0,优先于我们配置的目标order=1,所以该router先于我们的router拦截请求,转发到了jeecg-boot服务。


解决方案:

  • 修改ems-back, router信息,order =0,这样同样order=0,优先手动配置路由地址
  • 修改项目根路径为非jeecg-boot,这样动态路由jeecg-boot服务就不会收到非jeecg-boot的根路径请求
  • 修改jeecg-boot服务名为其他,这样动态路由地址也会变
  • 修改动态路由为false,不会启动动态路由,所有路由得手动配置
    gateway:
      discovery:
        locator:
          enabled: false

       

你可能感兴趣的:(gateway)