在学习谷粒商城时,登录页面获取验证码时,前端请求发送到gateway,然后通过gateway的路由过滤器转发到renren-fast时,报错
我的配置文件:
gateway:
routes:
- id: rewritepath_route
uri: lb://renren-fast
predicates:
- Path=/api/**
filters:
- RewritePath=/api/(?<segment>.*), /renren-fast/$\{segment}
浏览器验证码无法加载出来,控制台报错代码:503错误
GET http://localhost:88/api/captcha.jpg?uuid=3907c072-429c-45d2-803e-d6c279264b49 503
查问题,也有报相同错误的情况,大致问题说明是:gateway没有装配nacos依赖,但是我本地的gateway服务中添加了nacos的依赖包并且nacos服务可以查到gateway实例
gateway可匹配到的服务名格式: "[a-zA-Z]([a-zA-Z]|\\d|\\+|\\.|-)*:.*"
将renren-fast的应用名修改成了renren_fast,在调用时果然没有查询到renren_fast服务,并报此错误
java.lang.IllegalStateException: Invalid host: lb://renren_fast
at org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter.filter(RouteToRequestUrlFilter.java:85) ~[spring-cloud-gateway-server-3.1.4.jar:3.1.4]
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
*__checkpoint ⇢ org.springframework.web.cors.reactive.CorsWebFilter [DefaultWebFilterChain]
*__checkpoint ⇢ org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain]
*__checkpoint ⇢ HTTP GET "/api/captcha.jpg?uuid=bf93be18-d0e1-485e-8a38-cceb8c3814b3" [ExceptionHandlingWebHandler]
最后将application.yml配置文件修改成了如下
gateway:
routes:
- id: rewritepath_route
uri: http://localhost:8080/renren-fast
predicates:
- Path=/api/**
filters:
- RewritePath=/api/(?<segment>.*), /renren-fast/$\{segment}
能调用了,但是还是不清楚为什么 uri: lb://renren-fast 这样配置就会报错,哪位big old(因为提示不让使用此中文)遇到过相关问题,麻烦提供下思路,多谢
解决了,想到gateway使用lb动态路由导致的问题
原文链接:https://blog.csdn.net/Confused_/article/details/118894977
解决:gateway网关搭配nacos需要引入lb依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
ok结束