Build a Mini RPC Framework from Scratch: A Step‑by‑Step Guide

This tutorial walks through creating a lightweight RPC system, illustrating how an order service can call a product service via dynamic proxies, socket communication, serialization, and service registration, while showing Maven module setup, API design, and concrete code examples for each component.

Senior Brother's Insights
Senior Brother's Insights
Senior Brother's Insights
Build a Mini RPC Framework from Scratch: A Step‑by‑Step Guide

Preface

In typical backend development, an order service (owned by developer A) calls a product service (owned by developer B) via an agreed API. Usually B publishes the API to a Maven repository, implements it, and A consumes the API as a dependency.

The diagram shows the RPC usage scenario, and existing frameworks such as Baidu‑RPC, Dubbo, and TAF can be used, but this article builds a miniature RPC to explore its underlying mechanisms.

Order service calls product service diagram
Order service calls product service diagram

Product Service Project

The product service is split into two Maven modules: one for the API definition and one for the implementation. The API provides a method that, given a product ID, returns a product object.

Product service project diagram
Product service project diagram

Product Object

The Product class must be serializable because the order system receives a product instance over the network, requiring serialization and deserialization.

Product object diagram
Product object diagram

Product Query API

The API interface defines a method Product queryById(Long id). The order service will depend on this interface.

Product query API diagram
Product query API diagram

Order System Calls Product Service

When the order service invokes the product service, the call is routed through a dynamic proxy created with Proxy.newProxyInstance. The proxy intercepts method calls and forwards them to an invoke implementation.

Order service calling product service diagram
Order service calling product service diagram

The invoke method performs socket communication: it packages the target class name, method name, parameter types, and arguments, sends them to the product service, receives the serialized result, and deserializes it back into a Java object.

To locate the actual implementation of the product service API, a service‑registration mechanism is introduced, mapping the API to its concrete provider.

Service registration diagram
Service registration diagram

RPC Implementation Details

The RPC framework consists of three main steps:

Generate a dynamic proxy for the API interface.

In the proxy’s invoke method, open a socket, serialize the invocation data, and transmit it to the server.

On the server side, deserialize the request, use reflection to locate and invoke the target method, serialize the result, and write it back to the socket.

RPC implementation diagram
RPC implementation diagram

Running the Demo

After starting the product service, the order service can call queryById and receive a populated Product object. The article includes screenshots of the runtime output confirming successful remote invocation.

Runtime result
Runtime result
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.

BackendJavaMicroservicesDynamic ProxySocket
Senior Brother's Insights
Written by

Senior Brother's Insights

A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.

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.