基于 Vert.x 的 API 网关 Agate

背景
关注Vert.x已有4年,在多个项目中见证了威力,也踩了很多坑,然而对它的喜爱有增无减。Vert.x基于Netty的全异步编程模型很像Nginx,所以就想是否可以在JVM上开发一个类似Kong的高性能API网关呢?虽然JVM上的zuul、spring cloud gateway已经很优秀,但是手撕API网关(重复造轮子)的心一直没有死。
在新冠疫情爆发期间,花了2周时间写了一个基于Vert.x的基本可用的API网关Agate,借此正好填补空虚失落的焦虑,抚慰一下多愁善感的心灵。下面介绍一下微服务架构下分布式API网关的架构和实现效果,更多细节可以参看源码:Agate。
架构

运行架构
Agate由2部分组成:
Agate-Manager:
管理API的定义、配置信息,将这些信息持久化到数据库中,同时将要发布的API配置推送到Consul中。
为了快速原型,当前使用spring mvc + jsp页面来实现的UI页面,功能实现比较糙,有待改进。
Agate-Gateway:
监听Consul中API配置的变更,执行API的部署和启停,同时接受API客户端的请求,并根据API定义做路由处理,期间会执行日志、限流、熔断、认证、缓存等增强处理。
Gateway使用了Vert.x框架,全异步流程处理,抽象了BeforeHandler、RouteHandler、AfterHandler、FailureHandler处理过程。

Gateway组件图
系统依赖
Vert.x 4.0 和 Consul 1.7
特性列表
BlackWhiteList 待实现
RateLimit 已实现
Logging 已实现
Tracing 已实现
Metrics 已实现
Security 待实现
CircuitBreaker 待实现
GrayDeployment 待实现
Routing 已实现
实现效果

登录Manager

管理APP

管理API

链路追踪Zipkin

链路追踪Zipkin
结语
限于时间和水平,很多地方实现的不是太好,后续还要持续完善。对于正在学习网络编程的同学来说,Vert.x是个不可多得好工具;对于研究高性能网关实现的同学来说,Agate是个不(言)可(过)多(期)得(实)的参考。
评论