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过滤掉了

相关推荐
昵称为空C1 小时前
SpringBoot3 http接口调用新方式RestClient + @HttpExchange像使用Feign一样调用
spring boot·后端
麦兜*10 小时前
MongoDB Atlas 云数据库实战:从零搭建全球多节点集群
java·数据库·spring boot·mongodb·spring·spring cloud
麦兜*10 小时前
MongoDB 在物联网(IoT)中的应用:海量时序数据处理方案
java·数据库·spring boot·物联网·mongodb·spring
汤姆yu10 小时前
基于springboot的毕业旅游一站式定制系统
spring boot·后端·旅游
计算机毕业设计木哥11 小时前
计算机毕设选题推荐:基于Java+SpringBoot物品租赁管理系统【源码+文档+调试】
java·vue.js·spring boot·mysql·spark·毕业设计·课程设计
熙客11 小时前
SpringCloud概述
java·spring cloud·微服务
hdsoft_huge21 小时前
Java & Spring Boot常见异常全解析:原因、危害、处理与防范
java·开发语言·spring boot
AD钙奶-lalala1 天前
SpringBoot实现WebSocket服务端
spring boot·后端·websocket
毕设源码-朱学姐1 天前
【开题答辩全过程】以 4S店汽车维修保养管理系统为例,包含答辩的问题和答案
java·spring boot·汽车
BXCQ_xuan1 天前
软件工程实践二:Spring Boot 知识回顾
java·spring boot·后端