下载APP

学习Spring Cloud 技术第六篇-服务网关


theme: cyanosis highlight: a11y-dark

开启掘金成长之旅!这是我参与「掘金日新计划 · 2 月更文挑战」的第 2 天,点击查看活动详情

相关阅读

  1. 带你学习Spring Cloud 技术第一篇-总览
  2. 带你学习Spring Cloud 技术第二篇-技术介绍
  3. 带你学习Spring Cloud 技术第三篇-服务注册中心
  4. 学习Spring Cloud 技术第四篇-服务调用
  5. 学习Spring Cloud 技术第五篇-服务降级

一、服务网关(Spring-Cloud-GateWay)简介

服务网关 是整个服务的入口,核心功能是为整个的服务提供统一的路由、断言以及过滤的能力。下面是常见的网络架构图,可以看到我们的网关实际上就是对外提供的API接口服务。它接受了物联网、移动端以及PC端的请求流量。

image.png

1.1 核心特性

Spring Cloud GateWay 底层使用了webflux中的 reactor-netty 响应式编程组件,底层使用了netty的通信组件,异步非阻塞模型实现的技术。Spring-Cloud-GateWay 这个项目提供了一个在 Spring MVC 之上构建API网关的库。Spring Cloud Gateway旨在提供一种简单而有效的方法来路由到api,并为它们提供交叉关注点,例如:安全性、监视/度量和弹性等等。它的核心特性如下:

  1. 基于Spring Framework 5、Project Reactor和Spring Boot 2.0构建
  2. 能够匹配任何请求属性的路由。
  3. 谓词和过滤器是特定于路由的。
  4. Hystrix断路器集成。
  5. Spring Cloud Discovery Client集成
  6. 易于编写谓词和过滤器
  7. 请求速率限制
  8. 路径重写

1.2 三大基本概念

路由(Router):网关的基本构件。它由一个ID、一个目标URI、一组谓词和一组过滤器定义。如果聚合谓词为真,则匹配路由。

谓词(Predicate):这是一个Java 8函数谓词。输入类型是Spring Framework ServerWeb Exchange。这允许您匹配来自HTTP请求的任何内容,例如头或参数。

Filter(过滤器):这些是Spring Framework Gateway Filter的实例,是用特定的工厂构造的。在这里,您可以在发送下游请求之前或之后修改请求和响应。

1.3 Spring Cloud GateWay 服务网关功能

Spring Cloud GateWay 服务网关提供了反向代理、鉴权、流量控制、熔断、日志监控等等核心功能。

1.4 springcloud GateWay 与zuul的区别

Zuul 1.x 底层是一个阻塞的 IO API的gateway。

Zuul 1.x 基于servlet2.5,使用阻塞的架构,不支任何的长连接。

1.5 GateWay 模型

基于 spring-webflux模型实现的,相关文档链接: 文档链接

1.6 GATEWAY 工作流程

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

二、代码实操

2.1 新建项目

新建立我们的项目:spring-cloud-gateway-gateway9527 模块,其中9527是端口号。

2.2 配置项目

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-cloud-learn</artifactId>
        <groupId>com.breakpoint</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <packaging>jar</packaging>
    <artifactId>spring-cloud-gateway-gateway9527</artifactId>
    <dependencies>
        <!--  gateway 的配置  -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <!-- erureka-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

application.yml:

server:
  port: 9527
spring:
  application:
    name: cloud-gateway-gate
  cloud:
    gateway:
      enabled: true # 是否开启gateway的功能,默认是你开启的状态
      routes:
      - id: consumer_routh # 路由的ID ,没有固定的要求,但是唯一 建议使用服务名
        #uri:  http://localhost:80/
        uri:  lb://CLOUD-EUREKA-CLIENT-CONSUMER # 根据服务名动态的配置 实现 动态配置路由的目的
        predicates:
        - Path=/consumer/feign/**
      discovery:
        locator:
          # 如果不是动态的路由,这里可以不需要进行开启的
          enabled: true # Flag that enables DiscoveryClient gateway integration

eureka:
  instance:
    instance-id: ${spring.application.name}:${server.port}
    prefer-ip-address: true
  client:
    service-url:
      #defaultZone: http://peer1:7001/eureka/,http://peer2:7002/eureka/ # 服务的注册中心 高可用的模式
      defaultZone: http://peer1:7001/eureka/  # 服务的注册中心 单机版模式
    register-with-eureka: true
    fetch-registry: true

核心启动类

/**
 * @author :breakpoint/赵立刚
 * @date : 2020/07/27
 */
@SpringBootApplication
@EnableDiscoveryClient
public class GateWayMain9527 {

    public static void main(String[] args) {
        SpringApplication.run(GateWayMain9527.class, args);
    }
}

2.3 测试结果

image.png

image.png

成功调用我们的后台服务,起到了一定的拦截作用。

三、参考资料

https://cloud.spring.io/spring-cloud-static/Hoxton.SR1/reference/htmlsingle/#spring-cloud-gateway

代码小结:https://github.com/zhaoligang594/spring-cloud-learn/releases/tag/7.0.0

四、相关阅读

  1. 带你学习Spring Cloud 技术第一篇-总览
  2. 带你学习Spring Cloud 技术第二篇-技术介绍
  3. 带你学习Spring Cloud 技术第三篇-服务注册中心
  4. 学习Spring Cloud 技术第四篇-服务调用
  5. 学习Spring Cloud 技术第五篇-服务降级

开启掘金成长之旅!这是我参与「掘金日新计划 · 2 月更文挑战」的第 2 天,点击查看活动详情

在线举报