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
相关推荐
毅炼20 小时前
不写多语言 SDK,怎么让 Python、Go、Node 服务接入注册中心?
java·后端·系统架构·gateway
Raas1004 天前
AI网关支持哪些模型?MAI Gateway(魔芋企业级AI网关)功能实测与最佳实践
大数据·人工智能·gateway·mai gateway·企业级产品
Raas1004 天前
MAI Gateway(魔芋企业级AI网关)技术揭秘:AI网关支持DeepSeek吗?从原理到落地
大数据·人工智能·gateway·ai网关·mai gateway·魔芋·企业级产品
Raas1004 天前
MAI Gateway(魔芋企业级AI网关)技术揭秘:AI网关支持哪些模型?从原理到落地
大数据·人工智能·网关·gateway·mai gateway·企业级产品
多云行者5 天前
什么是LLM Gateway?定义、技术栈与落地方式详解
网关·llm·gateway·api·传统
毅炼5 天前
Rover-Suite 开源发布:轻量服务注册中心与网关一体化方案
java·后端·系统架构·gateway
Raas1006 天前
AI网关核心功能?MAI Gateway(魔芋企业级AI网关)如何赋能企业AI能力
大数据·人工智能·gateway·mai gateway·企业级产品
多云行者8 天前
LLM Gateway科普:大模型统一接入与治理,究竟解决了什么
大模型·llm·gateway·api·大模型统一接入
Raas1009 天前
MAI Gateway(魔芋企业级AI网关)功能全解:AI网关有哪些功能?一文看懂AI网关能力矩阵
大数据·人工智能·矩阵·gateway·mai gateway·企业级产品