Understanding Java RPC Frameworks: RMI, Hessian, and Dubbo with Code Examples
This article explains the concepts and usage of three Java RPC frameworks—RMI, Hessian, and Dubbo—detailing their serialization methods, transport protocols, and service registration mechanisms, and provides complete code samples for interface definition, service implementation, client, and server setup.
RPC (Remote Procedure Call) is widely used in large‑scale distributed applications to enable vertical splitting and easier scalability. In Java, several RPC frameworks exist, such as RMI, Hessian, and Dubbo, each with its own serialization and transport mechanisms.
RMI (Remote Method Invocation) is the built‑in Java RPC tool. It defines a remote interface and its implementation, then registers the service in an RMI registry. Example code:
public interface IService extends Remote {
public String queryName(String no) throws RemoteException;
} public class ServiceImpl extends UnicastRemoteObject implements IService {
private static final long serialVersionUID = 682805210518738166L;
protected ServiceImpl() throws RemoteException { super(); }
@Override
public String queryName(String no) throws RemoteException {
System.out.println("hello " + no);
return String.valueOf(System.currentTimeMillis());
}
} public class Client {
public static void main(String[] args) {
try {
Registry registry = LocateRegistry.getRegistry("127.0.0.1", 8088);
String[] list = registry.list();
for (String s : list) { System.out.println(s); }
IService server = (IService) registry.lookup("vince");
String result = server.queryName("ha ha ha ha");
System.out.println("result from remote : " + result);
} catch (Exception e) { e.printStackTrace(); }
}
} public class Server {
public static void main(String[] args) {
try {
Registry registry = LocateRegistry.createRegistry(8088);
ServiceImpl server = new ServiceImpl();
registry.rebind("vince", server);
System.out.println("bind server");
} catch (Exception e) { e.printStackTrace(); }
}
}Hessian is an HTTP‑based RPC framework that hides the registry concept and creates a proxy via HessianProxyFactory . It requires adding the Hessian JAR and uses its own serialization format.
Dubbo, an open‑source RPC framework from Alibaba, operates over TCP using Netty for high‑performance communication. It relies on Zookeeper for service registration and discovery, and supports multiple serialization protocols.
All three frameworks illustrate different trade‑offs in serialization, transport protocol, and service registry design, providing developers with options for building scalable Java distributed systems.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.