【已解决】Gateway路由转发-报503 Service Unavailable

文章描述了一个在SpringCloud2021.0.5版本中,使用Gateway进行路由转发时遇到503错误的问题。问题根源在于Ribbon被移除,而未引入SpringCloudLoadbalancer。解决方案是添加SpringCloudLoadbalancer依赖以实现负载均衡。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


❓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>

在这里插入图片描述


评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小满@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值