文章目录

场景版本配置

问题原因解决方案其它

场景

版本

JDK 1.8 Spring Boot 2.6.0 Spring Cloud 2021.0.1 Spring Cloud Alibaba 2021.0.1.0

配置

gateway 开启动态路由 spring.cloud.gateway.discovery.locator.enabled: true

#gateway 配置

spring:

cloud:

nacos:

discovery:

server-addr: localhost:8848

namespace: 9b02d316-10f4-40bc-b06b-32cd0e6b3732

group: DEV_GROUP

service: ${spring.application.name}

password: nacos

username: nacos

gateway:

discovery:

locator:

enabled: true

lower-case-service-id: true

routes:

- id: demo-service

uri: lb://demo-server

predicates:

- Path=/demo-server/demo/test/gateway/yaml

#demo-server 配置

spring:

application:

name: demo-server

main:

allow-bean-definition-overriding: true

cloud:

nacos:

discovery:

server-addr: localhost:8848

namespace: 9b02d316-10f4-40bc-b06b-32cd0e6b3732

group: DEV_GROUP

service: ${spring.application.name}

password: nacos

username: nacos

server:

port: 8003

servlet:

context-path: /demo-server

问题

请求gateway代理接口报404错误(非http404错误,gateway服务的404错误信息) gateway网关接口: http://127.0.0.1:8002/demo-server/demo/test/gateway/yaml demo服务接口: http://127.0.0.1:8003/demo-server/demo/test/gateway/yaml

原因

gateway router 转发到错误uri(/demo/test/gateway/yaml)路径上。 检查gateway配置路由信息没有发现错误。 通过debug网关服务FilteringWebHandler类排查问题,发现gateway 开启动态路由(spring.cloud.gateway.discovery.locator.enabled: true),并且该路由的优先级(order=0),高于配置的目标(order=1),所以该路由先于配置的路由拦截请求,转发到了错误的uri路径上。 gateway 动态路由原文: 默认情况下,网关为通过DiscoveryClient创建的路由定义了默认的断言和过滤器。这些断言和过滤器用于确定如何处理进入网关的请求。 默认断言是基于服务ID的路径断言,它会匹配以特定服务ID开头的路径。 默认过滤器是一个重写过滤器,它使用正则表达式从路径中截取掉服务ID,只保留剩余的部分。 如果你想要自定义DiscoveryClient路由使用的断言和过滤器,可以通过设置相应的属性来实现。但在自定义时,需要确保包含默认的断言和过滤器,以保留原有的功能。 所以: /demo-server/demo/test/gateway/yaml 被转换成 /demo/test/gateway/yaml 就报404

解决方案

1、修改动态路由为false,不启动动态路由,所有路由得手动配置

spring:

cloud:

gateway:

discovery:

locator:

enabled: false

2、修改demo-server服务名(demo-server -> demo-service )或者修改demo-server服务的根路径(/demo-server -> /demo-service)

########################## 修改demo-server服务名 ##############################

spring:

application:

name: demo-service

main:

allow-bean-definition-overriding: true

server:

port: 8003

servlet:

context-path: /demo-server

########################## 修改demo-server服务的根路径 ##############################

spring:

application:

name: demo-server

main:

allow-bean-definition-overriding: true

server:

port: 8003

servlet:

context-path: /demo-service

3、修改请求路径 (http://127.0.0.1:8002demo-server/demo/test/gateway/yaml) -> (http://127.0.0.1:8002/demo-server/demo-server/demo/test/gateway/yaml)

其它

公众号

精彩内容

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: