Mastering RPC: From Basics to Building Your Own Framework
This article explains what RPC is, why RPC frameworks are needed, the underlying principles and technologies such as dynamic proxies, serialization, NIO communication, service registration, governance and routing, and walks through a simple hand‑crafted RPC implementation with a comparison of popular frameworks.
1. What is RPC
RPC (Remote Procedure Call) is a protocol that allows a client to invoke methods on a remote server as if they were local, abstracting network details, data formats, and language differences. Typical implementations include Dubbo, Thrift, and gRPC.
2. Why Use an RPC Framework
When applications grow and need to be split into multiple services, a lightweight, transparent communication mechanism becomes essential. RPC frameworks enable high cohesion and low coupling, hide low‑level call details, and simplify development.
3. RPC Framework Principles
Client stub assembles method name and parameters into a network message.
Client stub discovers the service address and sends the message.
Server stub receives and decodes the message.
Server stub invokes the local service implementation.
The service returns the result to the server stub.
Server stub serializes the result and sends it back.
Client stub receives and decodes the response.
Client obtains the final result.
The goal is to encapsulate steps 2‑8 so users do not see the details.
4. Core Technologies Used by RPC
4.1 Dynamic Proxy
Java dynamic proxies (JDK proxy, CGLIB, Javassist, ByteBuddy) are used to make remote calls appear as local method invocations.
public class RPCProxyClient implements InvocationHandler {
private Object obj;
public RPCProxyClient(Object obj) { this.obj = obj; }
public static Object getProxy(Object obj) {
return Proxy.newProxyInstance(obj.getClass().getClassLoader(),
obj.getClass().getInterfaces(), new RPCProxyClient(obj));
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// serialize args, send request, receive response, deserialize
Object result = new Object();
return result;
}
}
public class Test {
public static void main(String[] args) {
HelloWorldService helloWorldService = (HelloWorldService) RPCProxyClient.getProxy(HelloWorldService.class);
helloWorldService.sayHello("hello world!");
}
}4.2 Serialization
Objects are converted to byte streams for network transmission and reconstructed on the receiving side. Common choices include Protobuf, Kryo, FastJSON, or custom binary protocols, evaluated on universality, performance, and extensibility.
4.3 NIO Communication
Non‑blocking I/O (Java NIO/NIO.2) or frameworks like Netty/Mina are used to achieve high concurrency.
4.4 Service Registry & Discovery
Registries such as Zookeeper, Consul, Etcd, or Nacos store service addresses and support health‑checking and dynamic discovery.
4.5 Service Governance
Features include circuit breaking, rate limiting, health monitoring, and graceful shutdown/startup strategies.
4.6 Routing & Load Balancing
Various algorithms (Round‑Robin, Random, Weight, Consistent Hash, Least Connection, Fastest Response, etc.) determine how requests are distributed among service instances.
4.7 Observability
Tracing, logging, and metrics are collected to monitor RPC interactions.
5. Simple RPC Implementation Example
A minimal RPC demo (EasyRPCDemo) implements dynamic proxy, serialization, network transport, and server handling with only a few classes, illustrating the core concepts.
6. Comparison of Popular RPC Frameworks
Framework
Language
Service Governance
Serialization
Registry Support
Management Console
Cross‑Language
Performance
Dubbo
Java
Yes
Multiple
Supported
Yes
No
Good
gRPC
Cross‑language
No
Protobuf only
No
No
Yes
Good
Thrift
Cross‑language
No
Thrift only
No
No
Yes
Excellent
Dubbo provides built‑in governance, while gRPC focuses on the transport layer and relies on service‑mesh solutions like Istio for governance.
7. Conclusion
RPC is a remote method invocation protocol that simplifies distributed service development. Understanding its principles—dynamic proxy, serialization, NIO, registry, governance, routing, and observability—helps you build or choose the right framework, and a tiny hand‑crafted example clarifies the underlying mechanics.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architect
Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.
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.
