• Gateway


    一、如何引入SpringCloud Gateway

    1.1 Gateway简介

    https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.1.3.RELEASE/single/spring-cloud-gateway.html

    每一秒处理请求个数对比--Gateway vs Zuul 1.0 vs Linkerd

    1.2 引入Gateway

    To include Spring Cloud Gateway in your project use the starter with group org.springframework.cloud and artifact id spring-cloud-starter-gateway. See the Spring Cloud Project page for details on setting up your build system with the current Spring Cloud Release Train.

    If you include the starter, but, for some reason, you do not want the gateway to be enabled, set spring.cloud.gateway.enabled=false.

    [Important] Important

    Spring Cloud Gateway is built upon Spring Boot 2.xSpring WebFlux, and Project Reactor. As a consequence many of the familiar synchronous libraries (Spring Data and Spring Security, for example) and patterns you may not apply when using Spring Cloud Gateway. If you are unfamiliar with these projects we suggest you begin by reading their documentation to familiarize yourself with some of the new concepts before working with Spring Cloud Gateway.

    [Important] Important

    Spring Cloud Gateway requires the Netty runtime provided by Spring Boot and Spring Webflux. It does not work in a traditional Servlet Container or built as a WAR.

    二、Gateway术语概念 (路由,断言,过滤器)

    核心流程:当请求到达网关Gateway,网关利用断言Predicate,判定这次请求是否符合某个路由规则Route(id + uri + predicates + filters),符合则根据该路由规则把请求路由到指定地方,期间需要经过一系列过滤器Filter进行过滤。

    2.1 Predicate 断言

    - Query 请求参数断言

    共有两种参数模式

    The Query Route Predicate Factory takes two parameters: a required param and an optional regexp.

    application.yml. 

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

    只包含param的参数时,只要request里有该param就行,至于param的值是什么无所谓。例如 127.0.0.1:80?baz=take , 127.0.0.1:80?baz=test 

    This route would match if the request contained a baz query parameter.

    application.yml. 

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

    有两个参数时,就是对param及其值均有要求。其值可以用正则表达式来匹配

    This route would match if the request contained a foo query parameter whose value matched the ba. regexp, so bar and baz would match.

    - Path 请求路径断言

    The Path Route Predicate Factory takes two parameter: a list of Spring PathMatcher patterns and an optional flag to matchOptionalTrailingSeparator.

    application.yml. 

    spring:
      cloud:
        gateway:
          routes:
          - id: host_route
            uri: https://example.org
            predicates:
            - Path=/foo/{segment},/bar/{segment}

    This route would match if the request path was, for example: /foo/1 or /foo/bar or /bar/baz.

    2.2 Gateway Filter 网关过滤器

    - RewritePath 请求路径重写

    会把127.0.0.1:80/api/test重写为127.0.0.1:80/renren-fast/test

    - id: admin_route
              uri: lb://renren-fast
              predicates:
                - Path=/api/**
              filters:
                - RewritePath=/api/(?<segment>.*), /renren-fast/${segment}

    2.3 Global Filter 全局过滤器

    lb:// 负载均衡

    If the url has a lb scheme (ie lb://myservice), it will use the Spring Cloud LoadBalancerClient to resolve the name (myservice in the previous example) to an actual host and port and replace the URI in the same attribute. 

     - id: admin_route
              uri: lb://renren-fast
              predicates:
                - Path=/api/**
              filters:
                - RewritePath=/api/(?<segment>.*), /renren-fast/${segment}

    GateWay官方文档

    https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.1.3.RELEASE/single/spring-cloud-gateway.html

  • 相关阅读:
    nvidia tx1使用记录--基本环境搭建
    STL hashtable阅读记录
    Linux strace命令
    转一篇:Reactor模式
    C++ 模板特化以及Typelist的相关理解
    C++ 内联函数inline
    迭代器失效的几种情况总结
    C++ Class与Struct的区别
    C++中string.find()函数,string.find_first_of函数与string::npos
    C/C++ 中长度为0的数组
  • 原文地址:https://www.cnblogs.com/frankcui/p/15192040.html
Copyright © 2020-2023  润新知