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
相关推荐
4SAPI5 小时前
内容生成与文本润色场景下的大模型 API 聚合网关选型指南
人工智能·gateway·php
Raas1002 天前
AI网关厂商排名?MAI Gateway(魔芋企业级AI网关)vs 开源方案全面对比
人工智能·开源·gateway·ai网关·mai gateway·企业级ai网关·企业级产品
4SAPI2 天前
企业级 RAG 知识库问答大模型 API 生产选型指南:4SAPI、硅基流动与 OpenRouter 的架构权衡
人工智能·gateway·agent·rag
JJJennie7773 天前
2026年大模型网关自建还是买?MAI Gateway等主流方案横评
人工智能·开源·gateway·ai网关
Raas1004 天前
2026年AI网关推荐?MAI Gateway(魔芋企业级AI网关)等主流方案横评
大数据·人工智能·ai·gateway·ai网关·mai gateway
4SAPI4 天前
多模型请求路由怎么设计?成本、速度和能力如何平衡
人工智能·python·gateway·php
Song_RsHt7 天前
Gateway(下):过滤器与统一鉴权
spring boot·spring·gateway
Raas1007 天前
MAI Gateway(魔芋企业级AI网关)选购攻略:AI网关哪个好?避坑指南与对比
大数据·人工智能·ai·gateway·ai网关·企业级ai网关