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
相关推荐
随风,奔跑3 小时前
Spring Cloud Alibaba(四)---Spring Cloud Gateway
后端·spring·gateway
jiayong237 小时前
Hermes Agent 的 Skills、Plugins、Gateway 深度解析
ai·gateway·agent·hermes agent·hermes
鬼蛟9 小时前
Gateway
gateway
武超杰10 小时前
Spring Cloud Gateway 从入门到实战
spring cloud·gateway
StackNoOverflow11 小时前
Spring Cloud Gateway 服务网关详解
gateway
tsyjjOvO12 小时前
服务网关 Gateway 从入门到精通
gateway
甜鲸鱼1 天前
JWT过滤器:从单体应用到微服务架构
微服务·架构·gateway·springcloud
notfound40431 天前
解决SpringCloudGateway用户请求超时导致日志未记录情况
java·spring boot·spring·gateway·springcloud
接着奏乐接着舞2 天前
gateway
gateway
一个public的class4 天前
前后端 + Nginx + Gateway + K8s 全链路架构图解
前端·后端·nginx·kubernetes·gateway