gateway的官方文档解读

之前公司用了springcloud的gateway.被一个伙伴留下了一堆的坑,没办法只能从头梳理.

第一步就是确定架构, gateway+consul+springboot

第二步就是确定一个flag 要解决哪些问题

1.解决灰度负载均衡策略问题:如何配置,支持哪些配置
2.解决ip名单和限流的问题:如何配置,支持哪些配置
3.解决路由重写的问题: 将msg服务,stats服务和action服务整合到business
4.解决consul摘机后服务列表不更新的问题
第三步就是开始官方文档的解读

我承认这篇博客不是一个好博客,因为不够亲民. 主要写下来就是给自己留个笔记罢了.谢谢!!!!

官方文档地址:https://docs.spring.io/spring-cloud-gateway/docs/2.2.5.RELEASE/reference/html/

1. How to Include Spring Cloud Gateway

这个自己百度吧,一堆堆的

简单来说里面有个警告: 就是用了netty的思想,有些东西吧不好使.悠着点.

2. Glossary

Route: 路由

Predicate:断言

Filter:过滤器

3. How It Works

Spring Cloud Gateway Diagram

Clients make requests to Spring Cloud Gateway. If the Gateway Handler Mapping determines that a request matches a route, it is sent to the Gateway Web Handler. This handler runs the request through a filter chain that is specific to the request. The reason the filters are divided by the dotted line is that filters can run logic both before and after the proxy request is sent. All “pre” filter logic is executed. Then the proxy request is made. After the proxy request is made, the “post” filter logic is run.

以下是一段纯google翻译

客户端向Spring Cloud Gateway发出请求。 如果网关处理程序映射确定请求与路由匹配,则将其发送到网关Web处理程序。 该处理程序通过特定于请求的过滤器链运行请求。 筛选器由虚线分隔的原因是,筛选器可以在发送代理请求之前和之后运行逻辑。 所有“前置”过滤器逻辑均被执行。 然后发出代理请求。 发出代理请求后,将运行“后”过滤器逻辑。

简单来说请求先匹配Handler Mapping,然后交给gate way web Handler,这哥们有一套独立的过滤器链(感觉有点像是netty的selector)后续需要再校正, 经过一顿prefilter的过滤,拿到返回值在进行一通postfilter

4. Configuring Route Predicate Factories and Gateway Filter Factories

配置路由,断言和过滤器

4.1. Shortcut Configuration

短链方式

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

4.2. Fully Expanded Arguments

长链方式

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

官方建议短链方式: 

predicates:

- Cookie=mycookie,mycookievalue

这里面声明了几个问题, 一个gateway区块有id, url, predicates 组成 predicates 可以写成 - 自定义id=name,value的形式

 

5. Route Predicate Factories 路由工厂

5.1. The After Route Predicate Factory

spring: cloud: gateway: routes: - id: after_route uri: https://example.org predicates: - After=2017-01-20T17:42:47.789-

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

This route matches any request made after Jan 20, 2017 17:42 Mountain Time (Denver).

5.2. The Before Route Predicate Factory

Example 2. application.yml

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

This route matches any request made before Jan 20, 2017 17:42 Mountain Time (Denver).

5.3. The Between Route Predicate Factory

Example 3. application.yml

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

This route matches any request made after Jan 20, 2017 17:42 Mountain Time (Denver) and before Jan 21, 2017 17:42 Mountain Time (Denver). This could be useful for maintenance windows.

Example 4. application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: cookie_route
        uri: https://example.org
        predicates:
        - Cookie=chocolate, ch.p

This route matches requests that have a cookie named chocolate whose value matches the ch.p regular expression.

5.5. The Header Route Predicate Factory

Example 5. application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: header_route
        uri: https://example.org
        predicates:
        - Header=X-Request-Id, \d+

This route matches if the request has a header named X-Request-Id whose value matches the \d+ regular expression (that is, it has a value of one or more digits)

5.6. The Host Route Predicate Factory

Example 6. application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: host_route
        uri: https://example.org
        predicates:
        - Host=**.somehost.org,**.anotherhost.org

URI template variables (such as {sub}.myhost.org) are supported as well.

This route matches if the request has a Host header with a value of www.somehost.org or beta.somehost.org or www.anotherhost.org.

5.7. The Method Route Predicate Factory

Example 7. application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: method_route
        uri: https://example.org
        predicates:
        - Method=GET,POST

This route matches if the request method was a GET or a POST.

5.8. The Path Route Predicate Factory

5.8. The Path Route Predicate Factory

The Path Route Predicate Factory takes two parameters: a list of Spring PathMatcher patterns and an optional flag called matchOptionalTrailingSeparator. The following example configures a path route predicate:

Example 8. application.yml

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

This route matches if the request path was, for example: /red/1 or /red/blue or /blue/green.

This predicate extracts the URI template variables (such as segment, defined in the preceding example) as a map of names and values and places it in the ServerWebExchange.getAttributes() with a key defined in ServerWebExchangeUtils.URI_TEMPLATE_VARIABLES_ATTRIBUTE. Those values are then available for use by GatewayFilter factories

5.9. The Query Route Predicate Factory

Example 9. application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: https://example.org
        predicates:
        - Query=green

The preceding route matches if the request contained a green query parameter.

5.10. The RemoteAddr Route Predicate Factory

Example 10. application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: remoteaddr_route
        uri: https://example.org
        predicates:
        - RemoteAddr=192.168.1.1/24

This route matches if the remote address of the request was, for example, 192.168.1.10.

5.11. The Weight Route Predicate Factory

Example 11. application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: weight_high
        uri: https://weighthigh.org
        predicates:
        - Weight=group1, 8
      - id: weight_low
        uri: https://weightlow.org
        predicates:
        - Weight=group1, 2

This route would forward ~80% of traffic to weighthigh.org and ~20% of traffic to weighlow.org

综上所述感觉gateway很牛逼吧,相当于拿着七层协议, header, paramter,cookie url随意玩耍,而且是显示配置.

猜你喜欢

转载自blog.csdn.net/habazhu1110/article/details/108476347