Design and Implementation of a High‑Performance API Gateway Using Spring WebFlux and Nacos
This article describes the end‑to‑end design, implementation, and testing of a high‑throughput, extensible API gateway built with Spring WebFlux, Netty, Nacos service discovery, dynamic routing, load‑balancing, authentication plugins, and WebSocket‑based configuration synchronization.
Background – Inspired by the Soul gateway project, the author built a high‑performance gateway (named ship‑gate ) from scratch, aiming to overcome the lack of a management console due to limited front‑end skills.
Design – Technology Selection – Two main approaches were considered for asynchronous request handling: Tomcat/Jetty + NIO + Servlet 3 (widely used by JD, Zuul) and Netty + NIO (used by VIP.com with higher throughput). The author chose Spring WebFlux (built on Netty) to avoid manual HTTP handling.
Requirements – The gateway must support custom routing rules (by version, header, query), cross‑language HTTP, high performance, high availability, gray (canary) releases, interface authentication, and multiple load‑balancing algorithms.
Architecture – The system is divided into modules such as ship-client-spring-boot-starter , ship-server , and ship-admin . Modules communicate via Nacos for service registration/discovery and share routing/metadata through WebSocket synchronization.
Implementation – Client Starter
public class AutoRegisterListener implements ApplicationListener
{ /* registration logic, shutdown hook, Nacos interaction */ }Implementation – Server Core
public class PluginFilter implements WebFilter { /* parses app name, builds plugin chain */ } public class PluginChain extends AbstractShipPlugin { /* manages ordered plugin execution */ } public class DynamicRoutePlugin extends AbstractShipPlugin { /* selects service instance, forwards request */ }Data Synchronization – NacosSyncListener periodically updates instance weight and enabled plugins to Nacos; DataSyncTaskListener pulls instance data into local caches; WebSocket servers/clients ( WebsocketSyncCacheServer , WebsocketSyncCacheClient ) push route rule changes in real time.
Testing – The author deployed Nacos, ship‑admin, and two ship‑example services (gray_1.0 and prod_1.0). A header‑based routing rule was added, and Postman requests verified that traffic with name=ship was routed to the gray instance. Performance testing with wrk on a MacBook Pro achieved ~9,400 QPS with 20 threads and 500 connections.
Conclusion – Building a gateway is approachable once you start coding; the project demonstrates dynamic routing, load balancing, and seamless configuration sync, with all source code available on GitHub.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.