How Does RPC Work? A Step‑by‑Step Java Implementation with Netty and Zookeeper

This article explains RPC fundamentals, detailing a Java example that uses Spring for bean management, Netty for high‑performance network communication, and Zookeeper for service registration and discovery, and walks through both server and client implementations with code snippets and test results.

Java Backend Technology
Java Backend Technology
Java Backend Technology
How Does RPC Work? A Step‑by‑Step Java Implementation with Netty and Zookeeper

Introduction

This article explains the principles of Remote Procedure Call (RPC) and walks through a simple Java implementation using Spring, Netty, and Zookeeper.

What a basic RPC call involves

Modern RPC frameworks such as Dubbo rely on interface‑based remote method invocation. The client needs only the interface definition; at runtime a dynamic proxy (e.g., JDK Proxy) creates a proxy object that forwards calls to the server. The call traverses network communication, requiring serialization, deserialization, and codec handling. Service registration and discovery are typically managed by a registry (e.g., Zookeeper), which stores service metadata and enables clients to locate providers.

Basic implementation

Server (provider)

Define a service interface

public interface HelloService { String sayHello(String somebody); }

Implement the interface

public class HelloServiceImpl implements HelloService { public String sayHello(String somebody) { return "hello " + somebody + "!"; } }

Register the service in Spring configuration and publish its metadata to Zookeeper using a custom StormServiceNamespaceHandler and ProviderFactoryBean.

Start a Netty server that listens on a configured port, adds a decoder, encoder, and a business‑logic handler ( NettyServerInvokeHandler) to the pipeline.

Client (consumer)

Generate a JDK dynamic proxy for the service interface.

Obtain the list of provider nodes from Zookeeper and select one according to a load‑balancing strategy.

Send a StormRequest containing the target method, arguments, and provider info through a Netty client.

Receive a StormResponse, extract the result, and return it to the caller.

Testing

Server main class loads storm-server.xml and publishes the service. Client main class loads storm-client.xml, retrieves the HelloService bean, and invokes sayHello("World").

Result

Server console shows “service published completed”. Client prints “hello World”. Screenshots of the server, client, and Zookeeper node tree are shown below.

RPC flow diagram
RPC flow diagram
Server output
Server output
Client output
Client output
Zookeeper registry
Zookeeper registry

Conclusion

The article provides a concise overview of the RPC workflow and a runnable example that demonstrates service registration, discovery, network communication, and remote method invocation.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Distributed SystemsRPCspringZooKeeperNetty
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.