文章目录
- 一、问题描述:SpringCloud + GateWay + Eureka访问出现404,Not Found
- 二、解决方案:
- 1、 配置 filters: - StripPrefix=1
- 2、删除冲突依赖
- 3、检查启动类
- 4、检查配置文件
一、问题描述:SpringCloud + GateWay + Eureka访问出现404,Not Found
现象:网关成功注册进Eureka,但是通过网关访问时出现404
Whitelabel Error Page
This application has no configured error view, so you are seeing this as a fallback.Mon Jun 05 16:33:18 CST 2023
[cb64c1a3-3] There was an unexpected error (type=Not Found, status=404).
org.springframework.web.server.ResponseStatusException: 404 NOT_FOUNDat org.springframework.web.reactive.resource.ResourceWebHandler.lambda$handle$1(ResourceWebHandler.java:408)Suppressed: The stacktrace has been enhanced by Reactor, refer to additional information below:
Error has been observed at the following site(s):*__checkpoint ⇢ org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain]*__checkpoint ⇢ HTTP GET "/payment/timeout/1" [ExceptionHandlingWebHandler]
Original Stack Trace:at org.springframework.web.reactive.resource.ResourceWebHandler.lambda$handle$1(ResourceWebHandler.java:408)at reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:44)at reactor.core.publisher.Mono.subscribe(Mono.java:4397)at reactor.core.publisher.FluxSwitchIfEmpty$SwitchIfEmptySubscriber.onComplete(FluxSwitchIfEmpty.java:82)at reactor.core.publisher.MonoFlatMap$FlatMapMain.onComplete(MonoFlatMap.java:181)at reactor.core.publisher.MonoNext$NextSubscriber.onComplete(MonoNext.java:102)at reactor.core.publisher.FluxConcatMap$ConcatMapImmediate.drain(FluxConcatMap.java:368)
二、解决方案:
可以通过以下几种方式尝试解决,不是必须都配置,根据自己项目情况处理。
1、 配置 filters: - StripPrefix=1
配置 filters: - StripPrefix=1,与路由id同级,去除前缀
2、删除冲突依赖
网关中如果有 spring-boot-starter-actuator、spring-boot-starter-web 依赖,删除。
3、检查启动类
启动类没有直接写在主包下面,而是写在了子包下面,需要在启动类加上@ComponentScan(“xxx”),xxx一定是你的主包名。
4、检查配置文件
检查配置文件中gateway相关的层级还有空格情况。
application.yml参考:
server:port: 9527spring:application:name: cloud-gateway #微服务应用的名字cloud:gateway:routes:- id: payment_routh #payment_route #路由的ID,没有固定规则但要求唯一,建议配合服务名uri: http://localhost:8001 #匹配后提供服务的路由地址predicates:- Path=/payment/timeout/** # 断言,路径相匹配的进行路由filters:- StripPrefix=1- id: payment_routh2 #payment_route #路由的ID,没有固定规则但要求唯一,建议配合服务名uri: http://localhost:8001 #匹配后提供服务的路由地址predicates:- Path=/payment/ok/** # 断言,路径相匹配的进行路由filters:- StripPrefix=1
eureka:client:register-with-eureka: true #向注册中心注册自己fetch-registry: true #从EurekaServer抓取已有的注册信息,集群必须设置成true,才能配合ribbon负载均衡service-url:defaultZone: http://eureka7001.com:7001/eurekainstance:instance-id: gateway9527 #主机名称修改prefer-ip-address: true #访问路径可以显示iphostname: cloud-gateway-service
如果这几种办法都没有解决,也可以尝试将target删除,重新编译试试。