当前位置: 首页 > news >正文

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

相关文章:

  • 下载chromedrive,使用自动化
  • 说说JVM的垃圾回收机制
  • 【算法】火柴排队(离散化、归并排序)
  • 在Linux/Ubuntu/Debian中使用lsof和fuser查看/解除文件占用
  • day-23 分发糖果
  • CentOS无法解析部分网站(域名)
  • GraphQL入门之查询片段的使用
  • python 小游戏《2048》字符版非图形界面
  • 论文阅读_参数微调_P-tuning_v2
  • Vue2 引入使用ElementUI详解
  • 基于word2vec 和 fast-pytorch-kmeans 的文本聚类实现,利用GPU加速提高聚类速度
  • cmd常用指令
  • 【Docker】Prometheus 容器部署及应用
  • 软件杯 深度学习 python opencv 实现人脸年龄性别识别
  • Selenium-webdriver_manager判断是否已经下载过驱动(复用缓存驱动)
  • 【C++】了解一下编码
  • FPGA的时钟资源
  • 【C++刷题】优选算法——动态规划第一辑
  • 欧科云链做客Google Cloud与WhalerDAO专题论坛,畅谈Web3数据机遇
  • vue常用指令
  • 讲一个香港儿童的故事,《劏房的天空》获“周庄杯”特等奖
  • 人民日报钟声:通过平等对话协商解决分歧的重要一步
  • 我国成功发射遥感四十号02组卫星
  • 普京提议重启俄乌直接谈判后,特朗普表态了
  • 著名蒙古族音乐学者马•斯尔古愣逝世,享年86岁
  • 武汉旅游体育集团有限公司原党委书记、董事长董志向被查