zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Pandas学习整理与实践
    数据描述性统计整理
    关于购置硬盘的相关注意点
    福大软工 · 最终作业
    福大软工 · 第十二次作业
    Beta冲刺 (7/7)
    Beta冲刺 (6/7)
    深度剖析Vue中父给子、子给父、兄弟之间传值!
    mysql 增删改插
    前端必学TypeScript之第一弹,st基础类型!
  • 原文地址:https://www.cnblogs.com/frankcui/p/15192040.html
Copyright © 2011-2022 走看看