❓1、问题描述
renren-fast
框架使用gateway
网关路由问题:
gateway
网关路由前端发送获取验证码的请求后renren-fast
的api
失效
前端发送的: http://localhost:88/api/captcha.jpg 通过网关路由->http://localhost:8080/api/captcha.jpg
但是正确地址应该是: http://localhost:8080/renren-fast/captcha.jpg。少了前缀/renren-fast
且api
应该去掉。
所以使用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>
发表评论