Backend Development 6 min read

Understanding Java RPC: RMI, Hessian, and Dubbo with Code Examples

This article explains the concept of RPC in Java, compares three popular frameworks—RMI, Hessian, and Dubbo—describes their architectures, and provides complete code samples for interfaces, service implementations, clients, and servers to help developers build scalable distributed applications.

Java Captain
Java Captain
Java Captain
Understanding Java RPC: RMI, Hessian, and Dubbo with Code Examples

RPC (Remote Procedure Call) is a widely used technique in large‑scale distributed systems that enables vertical splitting of applications and cross‑language communication; the article focuses on Java‑based RPC frameworks such as RMI, Hessian, and Dubbo.

1. RMI (Remote Method Invocation) – Java’s built‑in RPC mechanism. The remote interface is defined as:

public interface IService extends Remote {
    public String queryName(String no) throws RemoteException;
}

The service implementation uses UnicastRemoteObject and overrides the method:

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

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());
    }
}

The RMI client obtains a registry, looks up the service by name, and invokes the remote method:

import java.rmi.AccessException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class Client {
    public static void main(String[] args) {
        Registry registry = null;
        try {
            registry = LocateRegistry.getRegistry("127.0.0.1", 8088);
            String[] list = registry.list();
            for (String s : list) { System.out.println(s); }
        } catch (RemoteException e) { }
        try {
            IService server = (IService) registry.lookup("vince");
            String result = server.queryName("ha ha ha ha");
            System.out.println("result from remote : " + result);
        } catch (AccessException | RemoteException | NotBoundException e) { }
    }
}

The RMI server creates a registry, registers the service implementation, and starts listening:

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class Server {
    public static void main(String[] args) {
        Registry registry = null;
        try {
            registry = LocateRegistry.createRegistry(8088);
        } catch (RemoteException e) { }
        try {
            ServiceImpl server = new ServiceImpl();
            registry.rebind("vince", server);
            System.out.println("bind server");
        } catch (RemoteException e) { }
    }
}

2. Hessian – An HTTP‑based RPC framework. It uses HessianProxyFactory to create a client proxy from a URL and requires adding the Hessian JAR to the project. The architecture removes the explicit registry step and communicates directly over HTTP.

3. Dubbo – Alibaba’s high‑performance TCP‑based RPC framework built on Netty. It relies on Zookeeper for service registration and discovery, supports multiple serialization protocols, and offers extensive configuration options for load balancing, failover, and clustering.

The article concludes that understanding the underlying registration mechanism (often Zookeeper) is essential for mastering Dubbo, and that each framework has its own trade‑offs in terms of protocol, performance, and ecosystem support.

backenddistributed systemsJavarpcDubboHessianRMI
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

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.