A Simplified RPC Framework: Core Principles and Implementation
This article introduces a lightweight RPC framework that demonstrates the essential publishing and invocation processes, explains the underlying SPI mechanism, and details service registration, load balancing, fault‑tolerance strategies, and module structure, providing code references and visual diagrams for practical understanding.
The article presents a simplified RPC framework inspired by Dubbo, aiming to help readers grasp the core publishing and invocation workflow without relying on third‑party software. It outlines the classic RPC architecture, including service registration, discovery, routing, load balancing, and optional monitoring.
Implemented features include an SPI mechanism, support for Netty or HTTP protocols via SPI selection, a file‑based service registry, cluster fault‑tolerance (safe‑fail or retry), and weighted random or round‑robin load balancing.
The demo consists of three modules: Provider (service implementation and publishing), Consumer (client startup and invocation), and SDK (registry, protocol, and other shared components). Visual diagrams illustrate the module relationships and runtime behavior.
A detailed explanation of the SPI mechanism shows how extensions are loaded through getExtensionLoader, getAdaptiveExtension, and getExtension, with accompanying sequence diagrams and resource loading logic.
The service publishing flow is described step‑by‑step, from Spring initialization to exporting the service via the selected protocol, ultimately starting a Netty server and registering the service in a local file.
The service invocation flow covers how a consumer creates an invocation object, selects a load‑balancer via SPI, and executes the remote call through Netty, including the role of the cluster component for fault tolerance.
Consumer startup and call processes are illustrated with sequence diagrams, highlighting Spring context loading, reference configuration, dynamic proxy generation, and the final Netty client invocation.
Fault‑tolerance strategies such as Failfast, Failsafe, and Failover are discussed, with an emphasis on the retry mechanism that loops until a successful call or all providers have been tried.
Load‑balancing strategies include a weighted random algorithm and a round‑robin approach, both configurable via weights to influence provider selection.
In summary, the article explains SPI’s role in extending protocol, clustering, and load‑balancing components, walks through the provider and consumer lifecycles, and demonstrates concrete implementations of fault tolerance and load balancing, giving readers a clear understanding of RPC fundamentals.
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.
JD Retail Technology
Official platform of JD Retail Technology, delivering insightful R&D news and a deep look into the lives and work of technologists.
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.
