spring cloud gateway 一直404访问不到啊,但是不通过配置的都可以请求到
真的搞不懂,捅咕半天。
通过gateway访问不到
不走gateway可以
目录
gateway模块
pom.xml<?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>com.gate</groupId> <artifactId>gateway-try</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>gateway</artifactId> <properties> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring-cloud.version>2021.0.5</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>
application.yml
server:
port: 9500
spring:
cloud:
gateway:
routes:
- id: account
uri: http://127.0.0.1:9501
predicates:
- Path=/account/**
# filters:
# - StripPrefix=1
启动
controller
- product
pom.xml
<?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>com.gate</groupId>
<artifactId>gateway-try</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>product</artifactId>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-boot.version>2.7.6</spring-boot.version>
</properties>
<dependencies>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-web</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
application.yml
server:
port: 9501
启动
controller
而且很奇怪
gateway模块下加入filters,能访问到http://localhost:9500/account/product,难道不是应该访问的http://localhost:9500/product这里么,但是访问不到
server:
port: 9500
spring:
cloud:
gateway:
routes:
- id: account
uri: http://127.0.0.1:9501
predicates:
- Path=/account/**
filters:
- StripPrefix=1
而且还考虑加入跨域配置,又捅咕半天没啥结果,都试了一下也没解决,应该不是那个问题吧,真的很无语,感觉和视频里没差啊
刚看了个文档,不知道算不算解决办法,在filters中配置RewritePath,这样可以通过http://localhost:9500/account/product 请求到,但还是觉得,不用额外配置filters就应该可以访问到才行吧,搞不清楚蒙圈了。
filters: