什么是 Spring Cloud Gateway?
Spring Cloud Gateway 是 Spring 官方基于 Spring 5.0,Spring Boot 2.0 和 Project Reactor 等技术开发的网关,Spring Cloud Gateway 旨在为微服务架构提供一种简单而有效的统一的 API 路由管理方式。Spring Cloud Gateway 作为 Spring Cloud 生态系中的网关,目标是替代 Netflix ZUUL,其不仅提供统一的路由方式,并且基于 Filter 链的方式提供了网关基本的功能,例如:安全,监控/埋点,和限流等。
Spring Cloud Gateway 功能特征
- 基于 Spring Framework 5,Project Reactor 和 Spring Boot 2.0
- 动态路由
- Predicates 和 Filters 作用于特定路由
- 集成 Hystrix 断路器
- 集成 Spring Cloud DiscoveryClient
- 易于编写的 Predicates 和 Filters
- 限流
- 路径重写
Spring Cloud Gateway 工程流程
客户端向 Spring Cloud Gateway 发出请求。然后在 Gateway Handler Mapping 中找到与请求相匹配的路由,将其发送到 Gateway Web Handler。Handler 再通过指定的过滤器链来将请求发送到我们实际的服务执行业务逻辑,然后返回。
过滤器之间用虚线分开是因为过滤器可能会在发送代理请求之前(pre)或之后(post)执行业务逻辑。
构建路由网关cloud-alibaba
1.在父项目中创建子module项目名字为cloud-alibaba,在pom中引入Gateway依赖
<!--gateway网关 不能引入starter-web-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
完整服务消费者cloud-alibaba的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>cloud-alibaba</artifactId>
<groupId>com.zsy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.zsy</groupId>
<artifactId>cloud-gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cloud-gateway</name>
<dependencies>
<!--gateway网关 不能引入starter-web-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.配置application.yml或者properties文件
server:
port: 7777
spring:
application:
name: api-gateway
cloud:
gateway:
discovery:
locator:
enabled: true #让gateway可以发现nacos中的微服务
routes: # 路由数组 指当请求满足什么样的条件的时候,转发到哪个微服务上
- id: cloud-account #当前路由标识,要求唯一 (默认值uuid,一般不用,需要自定义)
uri: lb://cloud-account #请求最终要被转发的地址 lb指的是从nacos中按照名称获取微服务,并遵循负载均衡策略
order: 1 #路由优先级,数字越小,优先级越高
predicates: #断言 判断条件,返回值是boolean 转发请求要返回的条件 (可以写多个)、
- Path=/cloud-account/** #当请求路径满足path指定的规则时,此路由信息才会正常转发
filters: #过滤器(在请求传递过程中,对请求做一些手脚)
- StripPrefix=1 # 在请求转发之前去掉一层路径
nacos:
discovery:
server-addr: 127.0.0.1:8848
3.配置消费者启动服务的启动类
package com.zsy.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class CloudGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(CloudGatewayApplication.class, args);
}
}
4.启动cloud-account服务项目,启动成功,如下图
5. 验证服务,浏览器访问http://localhost:8081/getAccount,返回如下图
查看cloud-account控制台,如下图
6.启动cloud-gateway项目,启动成功,如下图
7. 验证服务,浏览器访问http://localhost:777/cloud-account/getAccount,返回如下图
注意:请求方式是 http://路由网关IP:路由网关Port/服务名
查看消费者cloud-account控制台,如下图
Spring Cloud Gateway 的路由功能配置成功