Gateway

序言

本文给大家介绍一下 Spring Cloud Gateway 的基础概念以及使用方式。

一、快速入门

1.1 引入依赖

xml 复制代码
<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>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.15</version>
  </parent>

  <groupId>com.qcgd</groupId>
  <artifactId>gateway</artifactId>
  <version>1.0.0</version>
  <packaging>jar</packaging>

  <name>gateway</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring-cloud.version>2021.0.9</spring-cloud.version>
    <spring-cloud-alibaba.version>2021.0.5.0</spring-cloud-alibaba.version>
  </properties>

  <dependencies>
    <!--引入 loadbalancer 依赖-->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-loadbalancer</artifactId>
    </dependency>

    <!--引入 gateway 依赖-->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>

    <!--引入 nacos 依赖-->
    <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-starter-alibaba-nacos-discovery</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>

      <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-alibaba-dependencies</artifactId>
        <version>${spring-cloud-alibaba.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

上面只引入了三个依赖:

  1. loadbalancer:负载均衡器
  2. gateway:网关
  3. nacos:注册中心

1.2 编写配置

yaml 复制代码
# 配置端口            
server:
  port: 2091
  
spring:
  application:
    name: gateway
  cloud:
    nacos:
      server-addr: localhost:8848

    # gateway 配置
    gateway:
      routes:
        - id: user-service
          uri: lb://user-service
          predicates:
            - Path=/test/**

1.3 启动服务

1.4 通过 gateway 访问服务

我们可以直接访问 gateway 网关,gateway 可以将请求正确路由到 user-service 从而获取到正确的请求结果。

二、Gateway 工作流程

如上图所示,gateway 的工作流程如下:

  1. 在配置文件中首先定义一条路由信息(路由信息包括路由的 id、具体的路由规则和路由的 uri)
  2. 请求到达 gateway 网关,首先判断是否匹配某条路由规则
  3. 若匹配路由规则,就将请求转发到指定的 uri,处理请求

三、Gateway 配置

yaml 复制代码
spring:
  cloud:
    gateway:
      routes:
        - id: user-service
          uri: lb://user-service
          predicates:
            - Path=/test/**

上面是 gateway 的入门配置。我们发现其核心在:

yaml 复制代码
# id 路由的唯一标识,自定义
- id: user-service
  # uri 表示当匹配路由规则时,请求的转发路径
  uri: lb://user-service
  # predicates 表示路由匹配规则
  # Path 表示可匹配与当前所定义路径模式匹配的路径
  # 例如:如果请求是 /test/helo 那么与 /test/** 是匹配的
  predicates:
    - Path=/test/**

四、路由规则

之前,定义路由信息的时候,我们发现最复杂的应该是如何去定义路由的规则。在实际的开发中,我们有各种各样的开发需求从而导致了我们需要灵活的去定义路由规则。在 spring 中提供了如下的方式,让我们进行路由匹配:

名称 说明 示例
After 是某个时间点后的请求 - After=2037-01-20T17:42:47.789-07:00America/Denver
Before 是某个时间点之前的请求 - Before=2031-04-13T15:14:47.433+08:00Asia/Shanghai
Between 是某两个时间点之前的请求 - Between=2037-01-20T17:42:47.789-07:00America/Denver, 2037-01-21T17:42:47.789-07:00America/Denver
Cookie 请求必须包含某些 cookie - Cookie=chocolate, ch.p
Header 请求必须包含某些 header - Header=X-Request-Id, \d+
Host 请求必须是访问某个 host(域名) - Host=.somehost.org,.anotherhost.org
Method 请求方式必须是指定方式 - Method=GET,POST
Path 请求路径必须符合指定规则 - Path=/red/{segment},/blue/**
Query 请求参数必须包含指定参数 - Query=name, Jack或者- Query=name

具体使用方式,大家可根据需要参考官方文档

五、过滤规则

gateway 不仅仅只是可以进行请求的路由转发,还可以对请求和转发做一些过滤的操作。例如:

yaml 复制代码
spring:
  cloud:
    gateway:
      routes:
      - id: add_request_header_route
        uri: https://example.org
        # 添加过滤规则:将 X-Request-red:blue 标头添加到所有匹配请求的下游请求标头中
        filters:
        - AddRequestHeader=X-Request-red, blue

过滤规则,spring 官方同样提供了许多,大家根据需求自行参考官方文档

往期推荐

  1. 缓存神器-JetCache
  2. Mybatis 缓存机制
  3. 为什么 MySQL 单表数据量最好别超过 2000w
  4. IoC 思想简单而深邃
  5. ThreadLocal
相关推荐
暗夜猎手-大魔王1 天前
转载--Hermes Agent 13 | Gateway 架构:二十余渠道如何复用同一套 Agent Runtime
人工智能·gateway
YJlio3 天前
OpenClaw 2026.5.2 Beta 更新解读:外部插件安装、ClawHub / npm 切换与 Gateway 性能优化
性能优化·npm·gateway·飞书·多维表格·飞书aily·飞书妙搭
v***59833 天前
SpringCloud实战十三:Gateway之 Spring Cloud Gateway 动态路由
java·spring cloud·gateway
团子的二进制世界3 天前
Gateway :微服务架构的核心网关
微服务·架构·gateway
MrMonkeyHou3 天前
Java微服务架构中的双剑合璧:Nacos与Gateway深度解析
java·微服务·架构·gateway
RR13353 天前
Spring MVC and Spring Gateway 的差异,以及报错处理
spring·gateway·mvc
普通网友3 天前
【python】pyspark.errors.exceptions.base.PySparkRuntimeError [JAVA_GATEWAY_EXITED] Java gateway proce
java·python·gateway
YJlio3 天前
OpenClaw v2026.5.26-beta.1 / beta.2 预发布解读:Gateway 加速、transcript 路径统一、多通道修复、语音增强与安装更新链路加固
人工智能·windows·python·ui·缓存·gateway·outlook
普通网友3 天前
AWS VPC Transit Gateway 部署:实现多 VPC(开发 / 测试 / 生产)间流量集中管控
云计算·gateway·aws
青莲网络4 天前
安全第一与合规治理:魔芋 AI 正式发布企业级大模型网关 Mai Gateway
人工智能·安全·gateway