❓1、问题描述

现象版本

SpringCloud 2021.0.5
Nacos 2.2.0

renren-fast框架使用gateway网关路由问题:

gateway网关路由前端发送获取验证码的请求后renren-fastapi失效

前端发送的: http://localhost:88/api/captcha.jpg 通过网关路由->http://localhost:8080/api/captcha.jpg

但是正确地址应该是: http://localhost:8080/renren-fast/captcha.jpg。少了前缀/renren-fastapi应该去掉。

所以使用gateway网关路径重写RewritePath filter实现路由路径正确。

RewritePath=/api/(?<segment>.*),/renren-fast/$\{segment}

实际情况是前端发送请求到网关, 网关路由转发后报如下错误:

503错误: Unable to find instance for renren-fast


📚2. 问题分析

首先我们的路由配置如下:

    gateway:
      routes:
        - id: admin_route
          uri: lb://renren-fast
          predicates:
            - Path=/api/**
          filters:
            - RewritePath=/api/(?<segment>.*),/renren-fast/$\{segment}

这里使用lb负载均衡路由到renren-fast服务所在地址,配置没有问题,路径也是对的,但是却报了503

在这里插入图片描述

为什么会这样,是因为在Spring Cloud 2020版本以后,默认移除了对Netflix的依赖,其中就包括Ribbon,官方默认推荐使用Spring Cloud Loadbalancer正式替换Ribbon,并成为了Spring Cloud负载均衡器的唯一实现。


🚀3. 解决

在当前模块导入Spring Cloud Loadbalancer依赖就可以了。

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-loadbalancer</artifactId>
		</dependency>

在这里插入图片描述


Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐