SpringBoot中引入了springcloud-gateway,路由不生效为什么?

今天闲来无事,想看看springcloud-gateway的,于是自开始搭建gateway但是,搭建完成后想验证,发现有问题总会报404,接下来详细说下操作过程,

1.首先创建2个springBoot的工程。一个作为gateway 一个可以作为路由的目标服务,今天我们只说gateway中的配置:

pom文件的配置:

<?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 https://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.7.18</version>

<relativePath/> <!-- lookup parent from repository -->

</parent>

<groupId>com.jihaixiang</groupId>

<artifactId>boot-mybatis</artifactId>

<version>0.0.1-SNAPSHOT</version>

<name>boot-mybatis</name>

<description>Demo project for Spring Boot+mybatis</description>

<properties>

<java.version>1.8</java.version>

</properties>

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-gateway</artifactId>

<version>3.1.9</version>

</dependency>

<!-- Spring Boot Web Starter -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-webflux</artifactId>

</dependency>

</dependencies>

<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

</plugins>

</build>

</project>
2.yml文件配置

spring:

application:

name: cloud-gateway

cloud:

gateway:

routes:

  • id: example_route222

uri: http://192.168.1.4:8092

predicates:

  • Path=/example/**

#filters:

#- StripPrefix=1

#filters:

- RewritePath=/example/(?<segment>.*), /$\{segment}

server:

port: 8091

其中http://192.168.1.4:8092是我的路由目标地址,如何在测试过程中发现并没有路由到http://192.168.1.4:8092,那一定是我们的访问的地址有问题,

比如:当我们http://192.168.1.4:8091/example/test的时候,最终路由到的地址是

http://192.168.1.4:8092/example/tes,那我们就首先要看我们8092的服务端是不是可以直接访问通。

但是如果你配置了 #filters:

#- StripPrefix=1 意味着,如果你访问地址为http://192.168.1.4:8092/example/tes,实际的访问地址就是http://192.168.1.4:8092/tes,因为StripPrefix=1是将example过滤掉了

相关推荐
一 乐3 小时前
酒店预订|基于springboot + vue酒店预订系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·酒店预订系统
格鸰爱童话4 小时前
向AI学习项目技能(六)
java·人工智能·spring boot·python·学习
H_老邪4 小时前
spring boot 学习之路-1.0
spring boot·后端·学习
Flittly4 小时前
【SpringAIAlibaba新手村系列】(18)Agent 智能体与今日菜单应用
java·spring boot·agent
Andya_net5 小时前
Java | 基于 Feign 流式传输操作SFTP文件传输
java·开发语言·spring boot
成为大佬先秃头6 小时前
开放标准(RFC 7519):JSON Web Token (JWT)
spring boot·后端·json·jwt
不懂的浪漫8 小时前
# mqtt-plus 架构解析(八):Spring Boot 自动装配,这些零件是怎么被粘合起来的
spring boot·后端·物联网·mqtt·架构
却话巴山夜雨时i8 小时前
互联网大厂Java面试场景:Spring Boot、微服务与Redis实战解析
spring boot·redis·微服务·kafka·prometheus·java面试·电商场景
希望永不加班8 小时前
SpringBoot 配置绑定:@ConfigurationProperties
java·spring boot·后端·spring
indexsunny9 小时前
互联网大厂Java面试实战:Spring Boot、MyBatis与Kafka在电商场景中的应用
java·spring boot·面试·kafka·mybatis·电商·技术栈