昨天小伙伴问我springcloud gateway 无法路由转发的问题,现在记录一下
现在企业微服务架构基本上都是用springcloud体系了,在国内基本上新项目都用springcloud alibaba,而且基本上都是所有服务聚合在一个父项目中。
springcloud gateway可以实现路由负载均衡等等功能,但是应用过程中,会有一些坑。
配置的没问题如下:
server:port: 9999
spring:application:name: gateway-servercloud:nacos:discovery:server-addr: 192.168.229.7:8848gateway:discovery:locator:enabled: true ##开启了会自动匹配路由规则routes: ##配置了手动路由规则,上面的自动开启失效- id: nacos-provideruri: lb://nacos-providerpredicates:- Path=/mmm/**
provider代码:
@RestController
@RequestMapping("/mmm")
public class MMMController {@Value("${server.port}")private String port;@GetMapping("/get")public String get(){return port;}
}
但是测试始终无效:
springcloud项目基本上都聚合到一个父项目中,里面各种子模块如provider、consumer、sentinel、gateway…,其他模块都没问题,但是springcloud gateway有点特殊,因为它依赖了web webflux ,就会有冲突,所以基本上在pom中就要排除web,这样就不会应该父项目依赖了web导致冲突了
org.springframework.cloud spring-cloud-starter-gateway org.springframework.boot spring-boot-starter-web
启动会成功,但是项目会有报错提示:
**********************************************************Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway at this time. Please remove spring-boot-starter-web dependency.**********************************************************
最后路由不到指定url。
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.Fri Mar 17 16:34:53 CST 2023
There was an unexpected error (type=Not Found, status=404).
No message available
如何解决呢?我的意见是把springcloud gateway项目独立出来,不要聚合到父项目中,这样就不会有web以来冲突了
pom文件如下:
4.0.0 org.springframework.boot spring-boot-starter-parent 2.2.13.RELEASE com.mmm.springcloud.study spring-cloud-gateway-demo 1.0-SNAPSHOT 8 8 UTF-8 com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery 2.2.5.RELEASE org.springframework.cloud spring-cloud-starter-gateway 2.2.3.RELEASE
因为没有聚合到父项目中,所以不需要额外提出web,启动后直接访问,成功!
上一篇:Yolov8详解与实战