Spring Cloud GateWay 使用教程

前言: 文章内容来自Spring Cloud Gateway官网

1. How to Include Spring Cloud Gateway

添加依赖

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
	</dependency>

如果包含启动器,但不希望启用网关,请在yml或properties中设置 spring.cloud.gateway.enabled=false

2. Glossary

3. How It Works

工作原理如下图所示:

4. 配置 路由 谓词 工厂 和 网关过滤工厂

4.1. 快捷配置

spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: https://example.org
        
        predicates:
        - Cookie=mycookie,mycookievalue

4.2. 完全展开的参数

spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: https://example.org
        
        predicates:
        - name: Cookie
          args:
            name: mycookie
            regexp: mycookievalue

以上两种方式主要区别在predicates部分

经验分享 程序员 微信小程序 职场和发展