Gateway基础知识

文章目录

  • [Spring Cloud GateWay 用法](#Spring Cloud GateWay 用法)
    • 核心概念
    • 请求流程
    • 两种配置方式
    • 设置日志(建议设置)
    • 路由的各种断言
    • 断言
      • [The After Route Predicate Factory](#The After Route Predicate Factory)
      • [The Before Route Predicate Factory](#The Before Route Predicate Factory)
      • [The Between Route Predicate Factory](#The Between Route Predicate Factory)
      • [The Cookie Route Predicate Factory](#The Cookie Route Predicate Factory)
    • img
      • [The Method Route Predicate Factory](#The Method Route Predicate Factory)
      • [(客户端访问地址)The Path Route Predicate Factory](#(客户端访问地址)The Path Route Predicate Factory)
      • [The Query Route Predicate Factory](#The Query Route Predicate Factory)
      • [The RemoteAddr Route Predicate Factory](#The RemoteAddr Route Predicate Factory)
      • [发布控制 The Weight Route Predicate Factory](#发布控制 The Weight Route Predicate Factory)
      • [The XForwarded Remote Addr Route Predicate Factory](#The XForwarded Remote Addr Route Predicate Factory)
      • [The Header Route Predicate Factory](#The Header Route Predicate Factory)
      • [The Host Route Predicate Factory](#The Host Route Predicate Factory)
    • 过滤器
      • [The AddRequestHeader GatewayFilter Factory](#The AddRequestHeader GatewayFilter Factory)
    • 小demo

Spring Cloud GateWay 用法

网关,类似于所有接口前面的一堵墙,统一来处理用户发起的请求,比如火车站售票前台。

关键是统一,起到路由,安全性等等。

官网:https://spring.io/projects/spring-cloud-gateway

官方文档:https://docs.spring.io/spring-cloud-gateway/docs/current/reference//html/

实例代码:https://spring.io/projects/spring-cloud-gateway#samples

核心概念

  1. 路由 (根据条件,进行url转发)
  2. 断言(一组规则,条件,用来确定如何转发路由)
  3. 过滤器:对请求进行一系列的处理,比如添加请求头,添加请求参数

请求流程

client:客户端发起请求

handler Mapping :根据断言,将请求转发到对应的路由

web handler:处理请求,一层层的经过过滤器:例如可以用于鉴权,限流等等

最后调用实际的服务

两种配置方式

  1. 配置式(推荐,方便,规范)
  • 简化版
  • 全称
  1. 编程式(灵活、自由)

设置日志(建议设置)

设置了日志就可以知道请求什么开始断言转发到哪里了。

yaml 复制代码
logging:
   level:
      org:
        springframework:
          cloud:
             gateway: trace

路由的各种断言

断言

  1. After在x时间之后
  2. Before在x时间之前
  3. Between在x时间之间
  4. 请求类别
  5. 请求头(包含Cookie)
  6. 查询参数
  7. 客户端地址
  8. 权重(用于实现发布控制)

The After Route Predicate Factory

The Before Route Predicate Factory

当前时间在这个时间之前,就会访问当前这个路由

The Between Route Predicate Factory

当前时间在这个时间之间,就会访问当前这个路由

The Method Route Predicate Factory

如果你的请求类别是这个post,get,就会访问当前这个路由

(客户端访问地址)The Path Route Predicate Factory

如果你的访问的地址是以这些/red/{segment},/blue/{segment}路径作为前缀,就会访问当前这个路由

yaml 复制代码
spring:
  cloud:
    gateway:
      routes:
      - id: path_route
        uri: https://example.org
        predicates:
        - Path=/red/{segment},/blue/{segment}

The Query Route Predicate Factory

根据查询条件,比如?green,就会访问当前这个路由

The RemoteAddr Route Predicate Factory

根据远程地址,比如你的用户的ip地址是192.168.1.1/24,就会访问当前这个路由

发布控制 The Weight Route Predicate Factory

根据你设置的权重,给你把同一个访问的地址,重定到不同的服务,轻松实现发布控制

The XForwarded Remote Addr Route Predicate Factory

从请求头中如果拿到XForwarded这个请求头的地址192.168.1.1/24,,就会访问当前这个路由 请求染色

The Header Route Predicate Factory

如果你的请求头包含X-Request-Id这样一个请求头,并且,它的值符合正则表达式的规则,就会访问当前这个路由

The Host Route Predicate Factory

如果你的访问的是这个.somehost.org,.anotherhost.org,域名,就会访问当前这个路由

过滤器

基本功能:对请求头、请求参数、响应头的增删改查

1.添加清求头

2.添加请求参数

3.添加响应头

4.降级

5.限流

6.重试

降级:https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#spring-cloud-circuitbreaker-filter-factory

The AddRequestHeader GatewayFilter Factory

增加请求头

小demo

创建SpringBoot项目

1.引入依赖

gateway

lombok

2.写配置文件

yaml 复制代码
server:
  port: 8090

spring:
  cloud:
    gateway:
      routes:
        - id: path_route
          uri: https://www.codefather.cn/
          predicates:
            - Path=/**


logging:
  level:
    org:
      springframework:
        cloud:
          gateway: trace

运行访问

地址访问 localhost:8090,就会跳转到 codefather.cn

我是小辉,24 届应届毕业生。当下是找工作ing,持续分享,包括不限于技术文章。全网同名...

相关推荐
zzzsde2 分钟前
【C++】stack和queue:优先级队列的使用及底层原理
开发语言·c++
杯莫停丶3 分钟前
设计模式之:外观模式
java·设计模式·外观模式
乐之者v4 分钟前
Mac常用软件
java·1024程序员节
TDengine (老段)23 分钟前
TDengine 数据函数 ROUND 用户手册
java·大数据·数据库·物联网·时序数据库·tdengine·1024程序员节
TDengine (老段)26 分钟前
TDengine 数学函数 RAND 用户手册
java·大数据·数据库·物联网·时序数据库·tdengine·涛思数据
從南走到北26 分钟前
JAVA无人自助共享系统台球室源码自助开台约球交友系统源码小程序
java·微信·微信小程序·小程序·1024程序员节
是苏浙37 分钟前
零基础入门C语言之操作符详解2
c语言·开发语言
野犬寒鸦37 分钟前
从零起步学习MySQL || 第十章:深入了解B+树及B+树的性能优势(结合底层数据结构与数据库设计深度解析)
java·数据库·后端·mysql·1024程序员节
总有刁民想爱朕ha1 小时前
银河麒麟v10批量部署Python Flask项目小白教程
开发语言·python·flask·银河麒麟v10
yi碗汤园2 小时前
【一文了解】八大排序-插入排序、希尔排序
开发语言·算法·unity·c#·1024程序员节