RMI客戶端調用服務端是報錯:
Could not connect to remote service [rmi://公網ip:port/rmiService]; nested exception is java.rmi.ConnectIOException: Exception creating connection to: 192.168.0.245; nested exception is: java.net.NoRouteToHostException: No route to host (Host unreachable)
發現客戶端沒有通過公網ip找服務端,而是通過服務端的內網ip
解決方案:
@Bean
public RmiServiceExporter rmiServiceExporter() {
//添加下行代碼,註冊爲公網地址,hostname爲公網ip
System.setProperty("java.rmi.server.hostname", hostname);
RmiServiceExporter exporter = new RmiServiceExporter();
exporter.setServiceInterface(RMIServiceInterface.class);
exporter.setServiceName("rmiService");
exporter.setService(rmiServiceInterface);
exporter.setRegistryPort(registryPort);
exporter.setServicePort(serverPort);
return exporter;
}