谷粒商城17-SpringCloud Gateway网关介绍

SpringCloud Gateway网关介绍


发送的请求,都先到达网关, 网关动态的路由到各个服务, 网关可以实时的感知某一个服务是上线还是下线, 总是能够把请求正确的路由到某一个位置,
没有网关的时候, 每一个微服务需要自己做鉴权, 限流和日志输出


有了网关之后, 在网关层进行鉴权,转发 限流 和日志输出的操作.

Springcloud gateway 是第二代网关框架 , 取代了Zuul网关.
每秒的请求速率, Gateway 最高

Gateway的三大概念
https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.3.RELEASE/reference/html/#glossary

  • Route: The basic building block of the gateway. It is defined by an ID, a destination URI, a collection of predicates, and a collection of filters. A route is matched if the aggregate predicate is true.
    Route为路由, 发一个请求个网关, 网关把请求路由到指定的服务中去. 路由之间的区分采用的是ID. 当次请求的路径为URI. 只要路由匹配了断言, 那么该次请求就能到达指定的路径.
  • Predicate: This is a Java 8 Function Predicate. The input type is a Spring Framework ServerWebExchange. This lets you match on anything from the HTTP request, such as headers or parameters.
    请求发送给网关, 是否需要到达某一个服务, 是需要进行条件判断的, 该条件就是Predicate 断言. 让开发者匹配当次请求的任何信息, 比如请求头或者请求参数. 网关根据请求参数, 或者请求头的不同 ,路由到不同的服务, 判断路由到不同的服务, 就是进行断言.
  • Filter: These are instances of Spring Framework GatewayFilter that have been constructed with a specific factory. Here, you can modify requests and responses before or after sending the downstream request.
    Filter为Spring提供的网关过滤器, 过滤器就是请求到后台服务的时候,进行一次过滤, 后台给前端响应, 再次进行一次过滤. 在请求到后台之前, 或者响应之前都能进行修改过滤.

SpringCloud Gateway工作流程


网关的工作流程, 请求到达网关, 先利用Predicate 断言, 判定是否符合某个路由Route规则, 如果符合了, 就让路由转发到指定的后台服务中, 在到达后台之前, 需要进行一系列的filter ,进行过滤 .
在Gateway的官方文档中, 如下的两节, 写明了有哪些路由断言的工厂 ,和 哪些过滤器工厂.


官网的路由规则示例

spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: https://example.org
        predicates:
        - After=2017-01-20T17:42:47.789-07:00[America/Denver]

此处为-id为其中的数组的一个元素. 每一个路由规则,都会有一个id , 代表,当前路由的名称.
uri为想要到达的后台的接口路径
predicates 为断言.

- After 为一个数组, 代表可以满足很多的断言.  只有断言成功了, 才会路由到指定的地方. 
- id: after_route
        uri: https://example.org
        predicates:
        - After=2017-01-20T17:42:47.789-07:00[America/Denver]

The After route predicate factory takes one parameter, a datetime (which is a java ZonedDateTime). This predicate matches requests that happen after the specified datetime. The following example configures an after route predicate
该断言的作用是, 满足指定的时间之后的请求, 才会进行路由到指定的地方.

官方示例的filter

此网关是没有写断言的, 代表所有的请求都会进入到指定的uri,但是进入到uri之前, 会先进行指定的filters

spring:
  cloud:
    gateway:
      routes:
      - id: add_request_header_route
        uri: https://example.org
        filters:
        - AddRequestHeader=X-Request-red, blue

其中filters 下面的配置为数组, 代表可以配置多个filters
filter的配置, 首先为官方指定的名称为 AddRequestHeader.
The AddRequestHeader GatewayFilter factory takes a name and value parameter.
该过滤器为键值对的过滤器, 会给请求加上请求头,
This listing adds X-Request-red:blue header to the downstream request’s headers for all matching requests.
最终形成的效果就是给请求头添加上了键为 X-Request-red 值 blue

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值