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:00[America/Denver]
Before 是某个时间点之前的请求 - Before=2031-04-13T15:14:47.433+08:00[Asia/Shanghai]
Between 是某两个时间点之前的请求 - Between=2037-01-20T17:42:47.789-07:00[America/Denver], 2037-01-21T17:42:47.789-07:00[America/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 天前
【工作记录】Kong Gateway入门篇之简介
gateway·kong
SHUIPING_YANG1 天前
Nginx 返回 504 状态码表示 网关超时(Gateway Timeout)原因排查
运维·nginx·gateway
Volunteer Technology2 天前
SpringCloud Gateway知识点整理和全局过滤器实现
spring·spring cloud·gateway
matrixlzp3 天前
K8S Gateway AB测试、蓝绿发布、金丝雀(灰度)发布
kubernetes·gateway·ab测试
泽济天下4 天前
【工作记录】Kong Gateway 入门篇之部署及简单测试
gateway·kong
JAVA坚守者5 天前
API 网关核心功能解析:负载均衡、容灾、削峰降级原理与实战摘要
gateway·负载均衡·微服务架构·容灾备份·api 网关·削峰降级·云原生技术
大G哥6 天前
实战演练:用 AWS Lambda 和 API Gateway 构建你的第一个 Serverless API
云原生·serverless·云计算·gateway·aws
说淑人6 天前
Spring Cloud & 以Gateway实现限流(自定义返回内容)
java·spring cloud·gateway·限流
zhojiew8 天前
istio in action之Gateway流量入口与安全
安全·gateway·istio
Absinthe_苦艾酒8 天前
SpringCloud之Gateway基础认识-服务网关
spring cloud·微服务·gateway