RMI (Remote Method Invocation) 是 Java 原生分布式 RPC 框架,EJB 的基石。雖然 gRPC、Dubbo、Spring Cloud 等現代框架更主流,但理解 RMI 有助於掌握「接口與實現分離」、「存根/骨架」、「註冊中心」等分布式核心概念。本文結合中國移動自動撥測系統實戰案例,完整演示 RMI 服務端、客戶端、接口定義、啟動流程與網絡原理。
所有分布式通信本質:基於傳輸協議 (TCP/HTTP) + 網絡 IO (BIO/NIO/AIO) 將字節流從一台機器傳到另一台。RMI 底層使用 JRMP (Java Remote Method Protocol) 協議,基於 TCP Socket,默認序列化機制為 Java 原生序列化。
Remote,方法拋 RemoteException);UnicastRemoteObject);Naming.bind);Naming.lookup)並調用;流程:設備告警 → RMI Client → RMI Server → 業務處理接口 → 入庫 → Web/GIS 實時展示。
接口契約:int interactive(int funIndex, String param),param 為 key=value&key=value 格式,funIndex=1 表示設備告警。
package nbpt.ts.manager.message.service;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface RmiMonitorService extends Remote {
int interactive(int funIndex, String param) throws RemoteException;
}
package nbpt.ts.manager.message.service.impl;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import nbpt.ts.manager.message.service.RmiMonitorService;
import nbpt.ts.manager.message.service.WarnService;
public class RmiMonitorServiceImpl extends UnicastRemoteObject implements RmiMonitorService {
private static final long serialVersionUID = -3771656108378649574L;
public static final int SUCCESS = 1, FAIL = 0;
private WarnService warnService;
public RmiMonitorServiceImpl() throws RemoteException { super(); }
@Override
public int interactive(int funIndex, String param) throws RemoteException {
int result = FAIL;
if (funIndex == 1) { // 設備告警
warnService = new WarnServiceImpl(); // 實際從 Spring 獲取
warnService.dealWarn(param);
result = SUCCESS;
}
return result;
}
// Getter/Setter for Spring 注入
}
package nbpt.ts.manager.message.service;
import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import nbpt.ts.manager.message.service.impl.RmiMonitorServiceImpl;
public class RmiServer {
private String ip = "localhost";
private int port = 8889;
public void init() {
try {
LocateRegistry.createRegistry(port); // 啟動內嵌 Registry
RmiMonitorService service = new RmiMonitorServiceImpl();
Naming.bind("//" + ip + ":" + port + "/comm", service);
System.out.println("RMI 服務啟動成功:" + ip + ":" + port);
} catch (RemoteException | AlreadyBoundException | MalformedURLException e) {
e.printStackTrace();
}
}
// Getter/Setter for Spring 配置
public static void main(String[] args) { new RmiServer().init(); }
}
package nbpt.ts.manager.message.service;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
public class MonitorClient {
private String ip = "localhost";
private int port = 8889;
private RmiMonitorService monitorService;
public int interactive(int funIndex, String param) {
try {
getMonitorService().interactive(funIndex, param);
return 1;
} catch (RemoteException e) { e.printStackTrace(); return 0; }
}
private RmiMonitorService getMonitorService() {
if (monitorService == null) {
try {
monitorService = (RmiMonitorService) Naming.lookup("rmi://" + ip + ":" + port + "/comm");
} catch (NotBoundException | MalformedURLException | RemoteException e) { e.printStackTrace(); }
}
return monitorService;
}
public static void main(String[] args) {
MonitorClient client = new MonitorClient();
String msg = "tsid=1022&devid=10001027&warnid=102&warntype=01&warnlevel=1&warnmsg=設備出錯,請檢查。";
client.interactive(1, msg);
}
}
// WarnService.java
public interface WarnService { int dealWarn(String message); }
// WarnServiceImpl.java
public class WarnServiceImpl implements WarnService {
@Override public int dealWarn(String message) {
System.out.println("已接收網絡告警:" + message);
// 解析入庫、推送 WebSocket 等
return 1;
}
}
rmiregistry;serialVersionUID 必須顯式聲明,防止反序列化失敗;java.rmi.server.hostname 或使用 HTTP 隧道/代理模式。| 框架 | 協議 | 序列化 | 服務發現 | 適用場景 |
|---|---|---|---|---|
| RMI | JRMP (TCP) | Java 原生 | RMI Registry | 純 Java 內部調用、教學、舊系統維護 |
| Hessian | HTTP | Hessian 二進制 | 無/自建 | 跨語言、防火牆友好、輕量 |
| Web Service (JAX-WS) | HTTP/SOAP | XML | UDDI/WSDL | 企業級異構集成、標準化要求高 |
| gRPC | HTTP/2 | Protobuf | Consul/Etcd/K8s | 微服務、高性能、多語言、流式 |
| Dubbo/Spring Cloud | TCP/HTTP | Hessian/Protobuf/JSON | Zookeeper/Nacos/Eureka | 國產微服務生態、治理豐富 |