RMI公網訪問

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

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章