• 【SpringCloud】Gateway网关入门(十六)


    Gateway介绍

      Spring Cloud Gateway是Spring Cloud的一个全新项目,基于Spring 5,Spring Boot 2和 Project Reactor等技术开发的网关,它旨在为微服务框架提供一种简单有效的统一的API路由管理方式,以及基于Filter方式提供一些强大的过滤功能,例如:熔断、限流、重试等

      Spring Cloud Gateway作为spring Cloud 生态系统中的网关,目标是替代Zuul,在Spring Cloud 2.0以上版本中,没有对新版本Zuul 2.0以上最新高性能版本进行集成,仍然还是Zuul 1.x非Reactor模式的老版本,而为了提升网关的性能,Spring Cloud Gateway是基于WebFlux框架实现的,而WebFlux框架底层则使用了高性能的Reactor模式通信框架Netty。

      Spring Cloud Gateway功能:

      1、基于Spring Framework 5,Project Reactor和Spring Boot 2.0构建

      2、能够匹配任何请求属性上的路由。

      3、断言和过滤器特定于路由。

      4、Hystrix断路器集成。

      5、Spring Cloud DiscoveryClient集成

      6、易于编写的断言和过滤器

      7、请求速率限制

      8、路径改写

      Spring Cloud 架构图

      

      由此可以知道网关在整个SpringCloud 中的位置

    Gateway工作流程

      工作流程图

      

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

      Filter,在“pre”类型的过滤器可以做参数校验、权限校验、流量监控、日志输出、协议转换等,在“post”类型的过滤器中可以做响应内容、响应头的修改,日志的输出、流量监控等,有非常重要的作用

    Gateway使用

      springcloud gateway案例演示,需要搭建一个springcloud项目,参考:【SpringCloud】快速入门(一)

      springcloud项目,包含一个Eureka注册中心,一个服务提供者,一个服务消费者

      

      1、在以上springcloud的项目基础上,新建模块Spring Cloud Gateway(springcloud-gateway-gateway9527)

      2、在Gateway模块中引入Gateway依赖和Eureka依赖

     1 <!-- Spring Cloud Gateway -->
     2 <dependency>
     3     <groupId>org.springframework.cloud</groupId>
     4     <artifactId>spring-cloud-starter-gateway</artifactId>
     5 </dependency>
     6 
     7 <!-- eureka client -->
     8 <dependency>
     9     <groupId>org.springframework.cloud</groupId>
    10     <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    11 </dependency>

        完整依赖如下:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <project xmlns="http://maven.apache.org/POM/4.0.0"
     3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     5     <parent>
     6         <artifactId>test-springcloud</artifactId>
     7         <groupId>com.test</groupId>
     8         <version>1.0-SNAPSHOT</version>
     9     </parent>
    10     <modelVersion>4.0.0</modelVersion>
    11 
    12     <artifactId>springcloud-gateway-gateway9527</artifactId>
    13     <build>
    14         <plugins>
    15             <plugin>
    16                 <groupId>org.apache.maven.plugins</groupId>
    17                 <artifactId>maven-compiler-plugin</artifactId>
    18                 <configuration>
    19                     <source>8</source>
    20                     <target>8</target>
    21                 </configuration>
    22             </plugin>
    23         </plugins>
    24     </build>
    25 
    26     <dependencies>
    27 
    28         <!-- Spring Cloud Gateway -->
    29         <dependency>
    30             <groupId>org.springframework.cloud</groupId>
    31             <artifactId>spring-cloud-starter-gateway</artifactId>
    32         </dependency>
    33 
    34         <!-- eureka client -->
    35         <dependency>
    36             <groupId>org.springframework.cloud</groupId>
    37             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    38         </dependency>
    39 
    40         <!-- spring boot -->
    41         <!-- gateway 不需要web模块 -->
    42 <!--        <dependency>-->
    43 <!--            <groupId>org.springframework.boot</groupId>-->
    44 <!--            <artifactId>spring-boot-starter-web</artifactId>-->
    45 <!--        </dependency>-->
    46         <dependency>
    47             <groupId>org.springframework.boot</groupId>
    48             <artifactId>spring-boot-starter-actuator</artifactId>
    49         </dependency>
    50         <dependency>
    51             <groupId>org.springframework.boot</groupId>
    52             <artifactId>spring-boot-devtools</artifactId>
    53             <scope>runtime</scope>
    54             <optional>true</optional>
    55         </dependency>
    56 
    57         <dependency>
    58             <groupId>org.projectlombok</groupId>
    59             <artifactId>lombok</artifactId>
    60             <optional>true</optional>
    61         </dependency>
    62         <dependency>
    63             <groupId>org.springframework.boot</groupId>
    64             <artifactId>spring-boot-starter-test</artifactId>
    65             <scope>test</scope>
    66         </dependency>
    67 
    68     </dependencies>
    69 
    70 </project>
    pom.xml

        ⚠️注意:不能引入模块SpringBoot的Web模块(spring-boot-starter-web),否则启动报错,如下:

        

      3、编辑appliction.yml文件

     1 # 端口
     2 server:
     3   port: 9527
     4 
     5 spring:
     6   application:
     7     name: cloud-gateway-gateway
     8   cloud:
     9     gateway:
    10       routes:
    11         # 路由的ID,没有固定规则,但要求唯一,建议配合服务名
    12         - id: payment_routh
    13           # 匹配后提供服务的路由地址
    14           uri: http://localhost:8001
    15           # 断言,路径相匹配的进行路由
    16           predicates:
    17             - Path=/payment/get/**
    18 
    19 eureka:
    20   client:
    21     register-with-eureka: true
    22     fetch-registry: true
    23     service-url:
    24       defaultZone: http://localhost:8761/eureka

      4、测试

        1)启动项(springcloud-gateway-gateway9527),注册到Eureka注册中心

        2)访问地址:http://localhost:8001/payment/get/1,证明支付模块服务正常

        3)访问地址:http://localhost:9527/payment/get/1,验证网关已生效

          

  • 相关阅读:
    线上项目部署
    day26 面向对象 单例模式总结
    阿里云服务器,更换操作系统步骤总结
    后端for循环补充
    前端,css
    django-admin 仿写stark组件action,filter筛选过滤,search查询
    7.20晚作业
    用户和群组管理
    7.19 晚作业
    目录和文件管理
  • 原文地址:https://www.cnblogs.com/h--d/p/12733498.html
Copyright © 2020-2023  润新知