小工具知识网

当前位置: 首页 > 行业知识 > SpringBoot中引入了springcloud-gateway,路由不生效为什么?

行业知识

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

2026-11-28 08:41:17 来源:互联网转载

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

()

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

pom文件的配置:

()

    4.0.0

    

        org.springframework.boot

        spring-boot-starter-parent

        2.7.18

        

    

    com.jihaixiang

    boot-mybatis

    0.0.1-SNAPSHOT

    boot-mybatis

    Demo project for Spring Boot+mybatis

    

        1.8

    

    

        

            org.springframework.boot

            spring-boot-starter

        

        

            org.springframework.cloud

            spring-cloud-starter-gateway

            3.1.9

        

        

        

            org.springframework.boot

            spring-boot-starter-webflux

        

        

    

    

        

            

                org.springframework.boot

                spring-boot-maven-plugin

            

        

    

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}

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

上一篇:云服务器装php

下一篇:咸鱼服务器忙怎么弄?