问题描述
使用SpringCloudGateway路由到带有content-path的微服务时总是404。此时的gateway的配置文件以及微服务的配置文件如下:
(此处省略其它配置)
gateway
...routes:-id: acl_service_otheruri: lb://acl-other-serverpredicates:- Path=/other/{segment}...server:port:8000
service1
...server:port:9001servlet:context-path: /other...
此时通过请求http://localhost:8000/other/test2/test2,总是报404
(经过测试,如果微服务中没有context-path,修改Path =/test2/{segment} http://localhost:8000/test2/test2是可以正常路由的)
问题处理
- 修改gateway的配置文件如下
routes:-id: acl_service_otheruri: lb://acl-other-serverpredicates:- Path=/other/**filters:##由于微服务acl-other-server中设置了server.servlet.context-path=/other,要重写路径才能正常路由到该微服务- RewritePath=/other/?(?<segment>.*), /other/$\{segment}
- 重启gateway,http://localhost:8000/other/test2/test2请求成功!!