Design and Implementation of a Custom RPC Framework with Spring Boot and Netty
This article presents a comprehensive overview of designing and implementing a custom RPC framework using Spring Boot and Netty, covering concepts, architecture, service registration with Zookeeper, load balancing, custom message protocol, serialization, handling TCP fragmentation, and various client invocation modes.
Remote Procedure Call (RPC) enables developers to invoke remote services as if they were local methods, abstracting network communication.
The framework consists of three core components: client, server, and a registry (Zookeeper). During a call, the server registers its services, the client discovers them, creates a proxy, encodes the request, sends it via Netty, and the server decodes, invokes the target method, encodes the response, and returns it.
Key design decisions include using Spring Boot starters (client‑starter and server‑starter) for minimal configuration, Netty as the I/O layer, and a custom binary message protocol that carries a magic number, version, serialization type, message type, status, request ID, length, and payload.
Serialization is handled by Hessian by default, with support for JSON; load‑balancing strategies (random and round‑robin) are pluggable via the LoadBalance interface. TCP packet fragmentation and sticky‑packet problems are solved by prefixing each message with its length.
The client supports synchronous, future, callback, and one‑way invocation modes, though only synchronous is implemented in this version. The startup flow for providers and consumers is driven by Spring Boot auto‑configuration, bean post‑processors, and annotations @RpcService and @RpcAutowired.
Environment requirements: Windows, IntelliJ IDEA, Spring Boot 2.5.2, JDK 1.8, Netty 4.1.42.Final, Zookeeper 3.7.0, and Maven 4.0.0. Testing involves starting Zookeeper, launching the provider and consumer applications, and accessing http://localhost:9090/hello/world?name=hello to receive the response.
Project source code is available at https://gitee.com/listen_w/rpc . The Spring Boot auto‑configuration entry is declared with:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.rrtv.rpc.client.config.RpcClientAutoConfigurationCode Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
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.