Backend Development 5 min read

Refactoring an IM System with Dynamic Proxy and RPC Architecture

This article explains how a Java‑based instant‑messaging system was restructured by replacing raw HTTP calls with API packages, introducing dynamic proxies for transparent RPC, and simplifying the code to resemble local method invocations while improving maintainability and scalability.

Sohu Tech Products
Sohu Tech Products
Sohu Tech Products
Refactoring an IM System with Dynamic Proxy and RPC Architecture

The author introduces the cim project, an instant‑messaging (IM) system composed of an IM‑server, IM‑client, and a Route service that handles authentication and message forwarding. The original implementation relied on direct okhttp HTTP requests, which made the code verbose and tightly coupled to low‑level details.

To improve the design, the author proposes encapsulating remote calls behind API packages, similar to how Dubbo or Spring Cloud expose services as local method calls. This abstraction hides the underlying protocol (HTTP or custom) and allows compile‑time checks for interface changes.

After refactoring, the architecture uses a dynamic proxy for the RouteApi interface. The proxy is created with:

RouteApi routeApi = new ProxyManager<>().getInstance();

The ProxyInvocation class implements InvocationHandler and performs encoding, remote invocation, and decoding using JDK dynamic‑proxy mechanisms. Whenever a method on the proxied interface is called, the invoke() method handles the entire RPC workflow.

Benefits of this approach include complete abstraction of low‑level details, easier maintenance, and early detection of interface changes during compilation. The article also provides a flow diagram of message transmission between two clients, a route service, and two servers, illustrating how the RPC calls are routed.

In conclusion, understanding the dynamic‑proxy based RPC implementation helps developers grasp the core of frameworks like Dubbo and Spring Cloud, and the provided source code (available on GitHub) serves as a practical reference for building a custom RPC hello‑world service.

backendJavaRPCIM systemrefactoringDynamic Proxy
Sohu Tech Products
Written by

Sohu Tech Products

A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.

0 followers
Reader feedback

How this landed with the community

login 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.