前言
昨天小伙伴問(wèn)我springcloud gateway 無(wú)法路由轉(zhuǎn)發(fā)的問(wèn)題,現(xiàn)在記錄一下
現(xiàn)在企業(yè)微服務(wù)架構(gòu)基本上都是用springcloud體系了,在國(guó)內(nèi)基本上新項(xiàng)目都用springcloud alibaba,而且基本上都是所有服務(wù)聚合在一個(gè)父項(xiàng)目中。
springcloud gateway可以實(shí)現(xiàn)路由負(fù)載均衡等等功能,但是應(yīng)用過(guò)程中,會(huì)有一些坑。
描述問(wèn)題
配置的沒(méi)問(wèn)題如下:
server:
port: 9999
spring:
application:
name: gateway-server
cloud:
nacos:
discovery:
server-addr: 192.168.229.7:8848
gateway:
discovery:
locator:
enabled: true ##開(kāi)啟了會(huì)自動(dòng)匹配路由規(guī)則
routes: ##配置了手動(dòng)路由規(guī)則,上面的自動(dòng)開(kāi)啟失效
- id: nacos-provider
uri: lb://nacos-provider
predicates:
- Path=/mmm/**
provider代碼:
@RestController
@RequestMapping("/mmm")
public class MMMController {
@Value("${server.port}")
private String port;
@GetMapping("/get")
public String get(){
return port;
}
}
但是測(cè)試始終無(wú)效:
分析原因
springcloud項(xiàng)目基本上都聚合到一個(gè)父項(xiàng)目中,里面各種子模塊如provider、consumer、sentinel、gateway…,其他模塊都沒(méi)問(wèn)題,但是springcloud gateway有點(diǎn)特殊,因?yàn)樗蕾嚵藈eb webflux ,就會(huì)有沖突,所以基本上在pom中就要排除web,這樣就不會(huì)應(yīng)該父項(xiàng)目依賴了web導(dǎo)致沖突了
springcloud gateway 的pom排除web
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</exclusion>
</exclusions>
</dependency>
啟動(dòng)會(huì)成功,但是項(xiàng)目會(huì)有報(bào)錯(cuò)提示:
**********************************************************
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
解決思路
如何解決呢?我的意見(jiàn)是把springcloud gateway項(xiàng)目獨(dú)立出來(lái),不要聚合到父項(xiàng)目中,這樣就不會(huì)有web以來(lái)沖突了
獨(dú)立的springcloud gateway
pom文件如下:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-447261.html
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.13.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.mmm.springcloud.study</groupId>
<artifactId>spring-cloud-gateway-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
</dependencies>
</project>
因?yàn)闆](méi)有聚合到父項(xiàng)目中,所以不需要額外提出web,啟動(dòng)后直接訪問(wèn),成功!文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-447261.html
到了這里,關(guān)于解決springcloud gateway 無(wú)法路由的問(wèn)題的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!