Spring Cloud Alibaba [Gateway]网关。

1 简介

网关作为流量的入口,常用功能包括路由转发、权限校验、限流控制等。而springcloudgateway 作为SpringCloud 官方推出的第二代网关框架,取代了Zuul网关。

1.1 SpringCloudGateway特点:

(1)基于Spring5,支持响应式编程和SpringBoot2.0

(2)支持使用任何请求属性进行路由匹配

(3)特定于路由的断言和过滤器

(4)集成Hystrix进行断路保护

(5)集成服务发现功能

(6)易于编写Predicates和Filters

(7)支持请求速率限制与路径重写

2 核心概念

2.1 路由

路由是网关最基础的部分,路由信息有一个ID、一个目的URL、一组断言和一组 Filter 组成。如果断言路由为真,则说明请求的URL和配置匹配

2.2 断言

Java8中的断言函数。SpringCloudGateway中的断言函数输入类型是Spring5.0框 架中的ServerWebExchange。Spring Cloud Gateway 中的断言函数允许开发者去定义匹配 来自于httpRequest 中的任何信息,比如请求头和参数等。

2.3 过滤器

一个标准的SpringwebFilter。Springcloudgateway 中的 filter 分为两种类型的 Filter,分别是 Gateway Filter 和 Global Filter。过滤器 Filter 将会对请求和响应进行修改处理

3 工作原理

客户端发送请求给网关,网关收到请求,并HandlerMapping判断是否请求满足路由,满足就发给网关的WebHandler。WebHandler 将请求交给一个过滤器链,请求到达目标服务之前,会执行所有过滤器的pre方法。请求到达目标服务处理之后再依次执行所有过滤器的post方法。

一句话:满足某些断言(predicates)就路由到指定的地址(uri),使用指定的过滤器(filter)

4 Gateway的集成

4.1 pom引入依赖

复制代码
<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

4.2 添加配置文件

复制代码
spring:
  cloud:
    gateway:
      routes:
#        - id: test_route
#          uri: https://www.baidu.com
#          predicates:
#            - Query=url,baidu
#
#        - id: qq_route
#          uri: https://www.qq.com
#          predicates:
#            - Query=url,qq

#        - id: product_route
#          uri: lb://gulimall-product
#          predicates:
#            - Path=/api/product/**
#          filters:
#            - RewritePath=/api/(?<segment>.*),/$\{segment}
#
#        - id: third_party_route
#          uri: lb://gulimall-third-party
#          predicates:
#            - Path=/api/thirdparty/**
#          filters:
#            - RewritePath=/api/thirdparty/(?<segment>.*),/$\{segment}
#
#        - id: member_route
#          uri: lb://gulimall-member
#          predicates:
#            - Path=/api/member/**
#          filters:
#            - RewritePath=/api/(?<segment>.*),/$\{segment}
#
#        - id: ware_route
#          uri: lb://gulimall-ware
#          predicates:
#            - Path=/api/ware/**
#          filters:
#            - RewritePath=/api/(?<segment>.*),/$\{segment}
#
#        - id: admin_route
#          uri: lb://renren-fast
#          predicates:
#            - Path=/api/**
#          filters:
#            - RewritePath=/api/(?<segment>.*),/renren-fast/$\{segment}



  ## 前端项目,/api
## http://localhost:88/api/captcha.jpg   http://localhost:8080/renren-fast/captcha.jpg
## http://localhost:88/api/product/category/list/tree http://localhost:10000/product/category/list/tree

4.3 注意

在gateway中配置uri配置有三种方式,包括

第一种:ws(websocket)方式: uri: ws://localhost:8808

第二种:http方式: uri: http://www.baidu.com

第三种:lb(注册中心中服务名字)方式: uri: lb://microname

4.3.1 规则1

各种Predicates同时存在于同一个路由时,请求必须同时满足所有的条件才被这个路 由匹配。

4.3.2 规则2

一个请求满足多个路由的谓词条件时,请求只会被首个成功匹配的路由转发

4.4 断言(Predicates)

4.5 过滤器(filters)

相关推荐
侠客行031720 小时前
Mybatis连接池实现及池化模式
java·mybatis·源码阅读
蛇皮划水怪20 小时前
深入浅出LangChain4J
java·langchain·llm
老毛肚1 天前
MyBatis体系结构与工作原理 上篇
java·mybatis
风流倜傥唐伯虎1 天前
Spring Boot Jar包生产级启停脚本
java·运维·spring boot
Yvonne爱编码1 天前
JAVA数据结构 DAY6-栈和队列
java·开发语言·数据结构·python
Re.不晚1 天前
JAVA进阶之路——无奖问答挑战1
java·开发语言
你这个代码我看不懂1 天前
@ConditionalOnProperty不直接使用松绑定规则
java·开发语言
fuquxiaoguang1 天前
深入浅出:使用MDC构建SpringBoot全链路请求追踪系统
java·spring boot·后端·调用链分析
琹箐1 天前
最大堆和最小堆 实现思路
java·开发语言·算法
__WanG1 天前
JavaTuples 库分析
java