• Spring Cloud Gateway 入门


    Spring Cloud Gateway 是由 WebFlux + Netty + Reactor 实现的响应式的 API 网关。

    Spring Cloud Gateway 旨在为微服务架构提供一种简单且有效的 API 路由的管理方式,并基于 Filter 的方式提供网关的基本功能,例如说安全认证、监控、限流等等。

    Spring Cloud Gateway 定位于取代 Netflix Zuul,成为 Spring Cloud 生态系统的新一代网关。目前看下来非常成功,老的项目的网关逐步从 Zuul 迁移到 Spring Cloud Gateway,新项目的网关直接采用 Spring Cloud Gateway。相比 Zuul 来说,Spring Cloud Gateway 提供更优秀的性能,更强大的有功能。

    Spring Cloud Gateway 的特征如下:

    • 基于 Java 8 编码
    • 基于 Spring Framework 5 + Project Reactor + Spring Boot 2.0 构建
    • 支持动态路由,能够匹配任何请求属性上的路由
    • 支持内置到 Spring Handler 映射中的路由匹配
    • 支持基于 HTTP 请求的路由匹配(Path、Method、Header、Host 等等)
    • 集成了 Hystrix 断路器
    • 过滤器作用于匹配的路由
    • 过滤器可以修改 HTTP 请求和 HTTP 响应(增加/修改 Header、增加/修改请求参数、改写请求 Path 等等)
    • 支持 Spring Cloud DiscoveryClient 配置路由,与服务发现与注册配合使用
    • 支持限流

    快速入门

    GitHub地址

    git@github.com:spring-cloud/spring-cloud-gateway.git
    
    切换分支
    git checkout -b a1 v3.0.0.M1
    
    编译打包
    mvn package -Dmaven.test.skip=true -Dcheckstyle.skip=true

    引入依赖

    <properties>
            <spring.boot.version>2.2.4.RELEASE</spring.boot.version>
            <spring.cloud.version>Hoxton.SR1</spring.cloud.version>
            <spring.cloud.alibaba.version>2.2.0.RELEASE</spring.cloud.alibaba.version>
        </properties>
    
        <!--
            引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
            在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
         -->
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-parent</artifactId>
                    <version>${spring.boot.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>${spring.cloud.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
                <dependency>
                    <groupId>com.alibaba.cloud</groupId>
                    <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                    <version>${spring.cloud.alibaba.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <dependencies>
            <!-- 引入 Spring Cloud Gateway 相关依赖,使用它作为网关,并实现对其的自动配置 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-gateway</artifactId>
            </dependency>
        </dependencies>

    配置文件

    创建application.yml配置文件,添加Spring Cloud Gateway相关配置:

    spring:
      cloud:
        # Spring Cloud Gateway 配置项,对应 GatewayProperties 类
        gateway:
          # 路由配置项,对应 RouteDefinition 数组
          routes:
            - id: baidu # 路由的编号
              uri: https://www.baidu.com # 路由到的目标地址
              predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组
                - Path=/baidu
              filters:
                - StripPrefix=1
            - id: sina # 路由的编号
              uri: https://www.sina.com.cn # 路由的目标地址
              predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组
                - Path=/sina
              filters: # 过滤器,对请求进行拦截,实现自定义的功能,对应 FilterDefinition 数组
                - StripPrefix=1

     spring.cloud.gateway 配置项:Spring Cloud Gateway配置项,对应GatewayProperties类。

    这里主要使用的 routes 路由配置项,对应RouteDefinition数组。路由(Route)是Gateway中最基本的组件之一,由一个IDURI、一组谓语(Predicate)、过滤器(Filter)组成。

    • ID:编号,路由的唯一标识;
    • URI:路由指向的目标 URI,即请求最终被转发的目的地;
    • Predicate:谓语,作为路由的匹配条件。Gateway 内置了多种 Predicate 的实现,提供了多种请求的匹配条件,比如说基于请求的 Path、Method 等等;
      • 这里配置的 Path 匹配请求的 Path 地址。
    • Filter:过滤器,对请求进行拦截,实现自定义的功能。Gateway 内置了多种 Filter 的实现,提供了多种请求的处理逻辑,比如说限流、熔断等等。
      • 这里配置的  StripPrefix  会将请求的 Path 去除掉前缀。

    整体架构

    Gateway的整体工作流程的图:

    图片出处:https://www.javainuse.com/spring/cloud-filter

    注意,一定要好好理解 Route、Predicate、Filter 这三个基础组件,Gateway 绝大多数代码都是围绕它们来运转的,如下图所示:

        

     它们在 Gateway 的整体工作流程中的作用,如下图所示:

     

    ① Gateway 接收客户端请求。

    ② 请求与 Predicate 进行匹配,获得到对应的 Route。匹配成功后,才能继续往下执行。

    ③ 请求经过 Filter 过滤器链,执行前置(prev)处理逻辑。

          例如说,修改请求头信息等。

    ④ 请求被 Proxy Filter 转发至目标 URI,并最终获得响应。

          一般来说,如果是在 Spring Cloud 架构中,目标 URI 是被代理的微服务。

    ⑤ 响应经过 Filter 过滤器链,执行后置(post)处理逻辑。

    ⑥ Gateway 返回响应给客户端。

    整体流程和 SpringMVC 的 DispatcherServlet 差不多,只是说 SpringMVC 最终转发到 JVM 进程内的指定方法,而 Gateway 最终转发到远程的目标 URI。

    基于注册中心实现动态路由

    使用 Gateway 提供的与 Spring Cloud 注册中心的集成,从注册中心获取服务列表,并以服务名作为目标 URI 来自动创建动态路由

  • 相关阅读:
    elasticsearch安装教程
    mysql设置账号密码及授权
    mongodb设置账号密码授权案例
    新安装的centos 6.5,不能上网,外网ping不通,内网可以ping通解决方法
    docker-compose安装教程
    解决github图片不显示问题
    网站宽带计算方式
    thinkphp 如何实现url的rewrite
    nginx Https配置
    Ext.Net导入Excel
  • 原文地址:https://www.cnblogs.com/CodePastry/p/13196508.html
Copyright © 2020-2023  润新知