版权声明:本文为博主原创文章,版权归烟台华东数据科技有限公司与博主本人共同所有。烟台华东数据科技有限公司及其下属机构毋须博主授权即可在任意用途下转载、使用!
前言
其实很多项目都是使用的zuul来做网关,我们公司由于历史原因,一直没有使用网关,这一次由于一些业务调整和框架升级,决定在Spring Cloud环境中加入网关模块,后续基于网关可以实现统一日志、统一转发、对外暴露唯一IP地址,统一授权、统一认证、统一熔断等各种需求。
有关Spring Cloud Gateway和zuul的比较,现在网上已经有很多了,我这里不再赘述,详细的对比可以参考下面的博文:
网关zuul与springcloudgateway 对比——来自yanchunling1981
我个人的想法是,既然已经升级到了Finchley版本,那么应该直接使用官方推荐的Spring Cloud Gateway了,而且听说效率也比zuul高很多。
Environment
- Intellij Idea version : 2019.1.2
- JDK version : 1.8
- Maven version : 3.6.1
- Spring Cloud version : Finchley.SR4
- SpringBoot version : 2.0.9.RELEASE
- Spring Cloud Gateway 2.0.4.RELEASE
其实Finchley版本的Spring Cloud Gateway算是初级版本,还有很多功能并没有完善,但是直接升级到Greenwich版本的Spring Cloud风险有点高,因此还是在Finchley版本下进行Spring Cloud Gateway的配置,后续升级的话,Spring Cloud Gateway的配置应该不会有太大变化。
不过需要注意的是,升级到Greenwich版本后,Spring Cloud Gateway中会集成Oath2的授权验证功能,可以简便很多开发,不过这是后续升级的后话了。
Spring Cloud Gateway简介
有关Spring Cloud Gateway的介绍和基本使用,网上有很多,在此我不再赘述,请各位自行参考学习。
Spring Gateway配置使用(一)——by 指间砂的宿命
Spring Cloud Gateway初体验——by 方志朋
尤其是上面的第二个博客,里面有一系列介绍Spring Cloud Gateway的基本用法和后面的进阶介绍,很适合初学者学习。而本篇博文主要不是介绍Spring Cloud Gateway的基本介绍,而且针对特定的业务和特定的需求,对Spring Cloud Gateway当前不支持的相关模式进行升级改造。包括后面的Oauth2授权认证的情况(当前版本的Spring Cloud Gateway是不支持Oauth2的集成的,需要后续版本才会集成使用,但是我们当前需要此功能,因此才有这一篇和后续的博文介绍)。
在开始本篇博文的介绍之前,要求已学习的技术前提如下:
- 已初步了解Spring Cloud Gateway的基本知识和原理
- 已可以初步搭建Spring Cloud Gateway,使用配置文件配置相关转发和限流
Spring Cloud Gateway升级用法
本篇不会介绍全局过滤器,主要介绍Filter的相关用法。在前面的博文介绍里面,大家可以知道,Spring Cloud Gateway里面已经内嵌了很多种不同的Filter来实现不同的功能,其中比较感兴趣的是限流功能,Spring Cloud Gateway里面有关限流使用的下面的类
org.springframework.cloud.gateway.filter.ratelimit.RedisRateLimiter
这个类在前面的学习中可能有的人会遇到,特别是在配置文件里面可以直接配置使用,但是这个类只能单个关键字限流(比如对IP限流),而可能我们在实际业务中,可能会要求,即对IP限流,同时也对访问的微服务限流,甚是多个其他限制情况。因此我们需要对这个限流类进行改造。
本篇博文我们需要实现的功能如下:
- Spring Cloud Gateway框架搭建
- Spring Cloud Gateway增加熔断控制
- Spring Cloud Gateway使用Redis进行限流控制,同时使用新的Lettuce连接池
- Spring Cloud Gateway中的路由控制使用代码控制
- Spring Cloud Gateway中限流的参数可以实现动态配置
- Spring Cloud Gateway中限流可以使用多个关键字限流
下面直接上代码,为了验证网关效果,因为我先新建了一个数据查询模块,用来接收Spring Cloud Gateway的转发,相关配置不再赘述,只简单介绍,此数据查询模块的详细介绍,在后面的授权验证中会另外详细介绍。
数据查询模块的基本结构如下
DataIndexController
package net.huadong.index.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.huadong.base.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
/**
* create by yangzj 2019-04-25 11:16:14
*/
@Api(value = "首页接口", description = "首页接口")
@Controller
@RequestMapping("/index")
public class DataIndexController extends BaseController {
@ApiOperation(value = "测试路由", notes = "测试路由")
@RequestMapping(value = "/hello",method = {RequestMethod.GET, RequestMethod.POST})
@ResponseBody
public String hello(@RequestParam String key) {
return "hello" + key;
}
}
可以看到,就是一个简单的返回,测试效果如下:
HDD_GATEWAY_SERVICE模块
此模块基于之前第一篇博文的Spring Cloud环境继续搭建,有什么不明白的可以查看此系列的第一篇博文。
pom文件
<parent> <artifactId>HDD_SHJJHY_PLATFORM</artifactId> <groupId>net.huadong</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>HDD_GATEWAY_SERVICE</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> <version>2.0.9.RELEASE</version> </dependency> <!-- 熔断、降级 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <!-- 限流 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis-reactive</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.51</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency> </dependencies>
配置中心的配置文件
由于此模块需要基于Redis运行, 因此需要配置Redis的相关信息,还是继续放在Config配置中心里面
spring: redis: # Redis数据库索引 database: 7 # Redis服务器地址 host: 127.0.0.1 port: 6379 # Redis服务器连接密码(默认为空) password: 123456 pool: # 连接池最大连接数(使用负值表示没有限制) max-active: 500 # 连接池最大阻塞等待时间(使用负值表示没有限制) max-wait: -1 # 连接池中的最大空闲连接 max-idle: 50 # 连接池中的最小空闲连接 min-idle: 10 # 连接超时时间(毫秒) timeout: 1000
启动类
package net.huadong;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.Enable