Spring Cloud Gateway

概述

Spring Cloud Gateway 是 Spring Cloud 的一个全新项目,该项目是基于Netty、Reactor以及WEbFlux构建,它旨在为微服务架构提供一种简单有效的统一的 API 路由管理方式。

Spring Cloud Gateway 作为 Spring Cloud 生态系统中的网关,目标是替代 Netflix Zuul,其不仅提供统一的路由方式,并且基于 Filter 链的方式提供了网关基本的功能,例如:安全、监控、埋点和限流等。

优点

  • 性能强劲,是Zuul的1.6倍
  • 功能强大,内置了很多实用的功能,例如转发、监控、限流等
  • 设计优雅,容易扩展

缺点

  • 依赖Netty与WebFlux,不是传统的Servlet编程模型,有一定的学习成本
  • 不能在Servlet容器下工作,也不能构建成WAR包,即不能将其部署在Tomcat、Jetty等Servlet容器里,只能打成jar包执行
  • 不支持Spring Boot 1.x,需2.0及更高的版本

核心概念

Route(路由)

这是Spring Cloud Gateway的基本构建块,可简单理解成一条转发规则。包含:ID、目标URL、一组断言和一组过滤器

Predicate(断言)

这是一个 Java 8 的 Predicate,即java.util.function.Predicate这个接口,Gateway使用Predicate实现路由的匹配条件。

Filter(过滤器)

这是 org.springframework.cloud.gateway.filter.GatewayFilter 的实例,我们可以使用它修改请求和响应

Gateway路由配置示例

spring:
cloud:
gateway:
routes:
- id: study01 # 唯一标识,通常使用服务id
uri: lb://study01 # 目标URL,lb代表从注册中心获取服务,lb是Load Balance的缩写
predicates:
# Predicate集合
- Path=/findBy # 匹配转发路径
filters:
# Filter集合
- StripPrefix=4 # 从第几级开始转发

工作流程

客户端向 Spring Cloud Gateway 发出请求。然后在 Gateway Handler Mapping 中找到与请求相匹配的路由,将其发送到 Gateway Web Handler。Handler 再通过指定的过滤器链来将请求发送到我们实际的服务执行业务逻辑,然后返回。

过滤器之间用虚线分开是因为过滤器可能会在发送代理请求之前(“pre”)或之后(“post”)执行业务逻辑。

特征

  • 基于 Spring Framework 5,Project Reactor 和 Spring Boot 2.0
  • 动态路由
  • Predicates 和 Filters 作用于特定路由
  • 集成 Hystrix 断路器
  • 集成 Spring Cloud DiscoveryClient
  • 易于编写的 Predicates 和 Filters
  • 限流
  • 路径重写

整合Spring Cloud Gateway

新建一个标准的Spring Boot项目,命名为gateway。

添加依赖

整合Spring Cloud Alibaba,添加Gateway和Nacos的依赖,示例如下:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>gateway</name>
<description>小程序网关</description> <properties>
<java.version>1.8</java.version>
<spring-cloud-alibaba.version>2.1.0.RELEASE</spring-cloud-alibaba.version>
<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency> <!-- 整合spring-cloud-alibaba-->
<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> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> <repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories> </project>

修改配置

添加nacos和gateway的配置,代码如下:

server:
port: 8040
spring:
application:
name: gateway
cloud:
nacos:
discovery:
server-addr: localhost:8848
namespace: 888ac051-ae5f-44f1-940a-30c7824a0e91
cluster-name: HZ
gateway:
discovery:
locator:
# 让gateway通过服务发现组件找到其他的微服务
enabled: true management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always

测试

依次启动之前就写好的项目【study01和study02】,然后启动gateway项目,打开任意浏览器。

PS:上述三个项目的nacos配置要一致。

我在study01项目中有一个findBy请求:http://localhost:8881/findById

在启动了gateway之后,可以这样输入浏览器地址:http://localhost:8040/study01/findById ,我们发现返回的结果是相同的

这就是Spring Cloud Gateway的作用之一 - 转发。

转发规律

  • ${GATEWAY_URL}/{微服务名}/{请求路径}

Spring Cloud Alibaba学习笔记(15) - 整合Spring Cloud Gateway的更多相关文章

  1. Spring Cloud Alibaba学习笔记(1) - 整合Spring Cloud Alibaba

    Spring Cloud Alibaba从孵化器版本毕业:https://github.com/alibaba/spring-cloud-alibaba,记录一下自己学习Spring Cloud Al ...

  2. Spring Cloud Alibaba学习笔记(3) - Ribbon

    1.手写一个客户端负载均衡器 在了解什么是Ribbon之前,首先通过代码的方式手写一个负载均衡器 RestTemplate restTemplate = new RestTemplate(); // ...

  3. Spring Cloud Alibaba学习笔记(22) - Nacos配置管理

    目前业界流行的统一配置管理中心组件有Spring Cloud Config.Spring Cloud Alibaba的Nacos及携程开源的Apollo,本文将介绍Nacos作为统一配置管理中心的使用 ...

  4. Spring Cloud Alibaba学习笔记(4) - Feign配置与使用

    什么是Feign Feign是一个声明式Web Service客户端. 使用Feign能让编写Web Service客户端更加简单, 它的使用方法是定义一个接口,然后在上面添加注解,同时也支持JAX- ...

  5. Spring Cloud Alibaba学习笔记

    引自B站楠哥:https://space.bilibili.com/434617924 一.创建父工程 创建父工程hello-spring-cloud-alibaba Spring Cloud Ali ...

  6. Spring Cloud Alibaba学习笔记(14) - Spring Cloud Stream + RocketMQ实现分布式事务

    发送消息 在Spring消息编程模型下,使用RocketMQ收发消息 一文中,发送消息使用的是RocketMQTemplate类. 在集成了Spring Cloud Stream之后,我们可以使用So ...

  7. Spring Cloud Alibaba学习笔记(12) - 使用Spring Cloud Stream 构建消息驱动微服务

    什么是Spring Cloud Stream 一个用于构建消息驱动的微服务的框架 应用程序通过 inputs 或者 outputs 来与 Spring Cloud Stream 中binder 交互, ...

  8. Spring Cloud Alibaba学习笔记(7) - Sentinel规则持久化及生产环境使用

    Sentinel 控制台 需要具备下面几个特性: 规则管理及推送,集中管理和推送规则.sentinel-core 提供 API 和扩展接口来接收信息.开发者需要根据自己的环境,选取一个可靠的推送规则方 ...

  9. Spring Cloud Alibaba学习笔记(2) - Nacos服务发现

    1.什么是Nacos Nacos的官网对这一问题进行了详细的介绍,通俗的来说: Nacos是一个服务发现组件,同时也是一个配置服务器,它解决了两个问题: 1.服务A如何发现服务B 2.管理微服务的配置 ...

随机推荐

  1. 微信小程序之自定义底部弹出框动画

    最近做小程序时,会经常用到各种弹框.直接做显示和隐藏虽然也能达到效果,但是体验性太差,也比较简单粗暴.想要美美地玩,添加点动画还是非常有必要的.下面做一个底部上滑的弹框. wxml <view ...

  2. configure-nginx-how-to-handle-500-error-on-upstream-itself-while-nginx-handle 自定义header 传递header 定义拦截器

    应用程序拦截 nginx 拦截 保证 接口返回 https://serverfault.com/questions/859667/configure-nginx-how-to-handle-500-e ...

  3. MiniDLNA常用操作

    # MiniDLNA常用操作 ## 简介 多媒体共享服务器,类似于FTP,支持DLNA的客户端都可以看视频,听音乐,处于同一局域网就可以了 ## 管理 - 启动`systemctl start min ...

  4. git如何查找某个包含指定字符串的commit hash值?

    答: git shortlog --format='%H|%cn|%s' | grep '需要查找的字符串内容'

  5. PHP使用MongoDB类操作MongoDB数据库总结

    参考:https://www.php.net/manual/zh/class.mongodb-driver-manager.php 参考:https://www.zhaokeli.com/articl ...

  6. C++11 学习笔记 std::function和bind绑定器

    C++11 学习笔记 std::function和bind绑定器 一.std::function C++中的可调用对象虽然具有比较统一操作形式(除了类成员指针之外,都是后面加括号进行调用),但定义方法 ...

  7. pytorch中调整学习率的lr_scheduler机制

    有的时候需要我们通过一定机制来调整学习率,这个时候可以借助于torch.optim.lr_scheduler类来进行调整:一般地有下面两种调整策略:(通过两个例子来展示一下) 两种机制:LambdaL ...

  8. Swift4.0复习循环

    1.for-in循环: 2.while循环: 3.repeat-while循环: repeat { // 循环体中的一条或多条执行语句 } while condition 4.标签语句: if_lab ...

  9. 【minikube & helm】

    minikube是用来快速搭建一个单机kubernetes集群的工具. https://www.cnblogs.com/liyuanhong/p/10143157.html helm是kubernet ...

  10. 使用Spring Boot接受HTTP GET/POST请求的一个SQL并返回结果

    这里说的意思是:我向我的Web服务器发送一个请求(因为GET请求的一些限制,所以最好使用POST请求,不过这里作为测试还是使用GET请求),请求中带一个sql参数,它对应查询的数据.然后我的Sprin ...