我有两个RMI Java应用程序:
服务器
System.setProperty("java.rmi.server.hostname", "0.tcp.ngrok.io");
System.setProperty("java.security.policy", "security.policy");
if (System.getSecurityManager() == null) {
System.setSecurityManager(new SecurityManager());
}
Registry r = LocateRegistry.createRegistry(1099);
r.rebind("Calc", new CalculatorImpl());
客户
System.setProperty("java.rmi.server.hostname", "0.tcp.ngrok.io");
System.setProperty("java.security.policy", "security.policy");
Calculator c = (Calculator) Naming.lookup("//0.tcp.ngrok.io:18872"+"/Calc");
int sum = c.add(1, 5);
两者之间的通信是在Ngrok的帮助下完成的:
ngrok tcp 1099
结果
Session Status online
Account nival (Plan: Free)
Version 2.2.8
Region United States (us)
Web Interface http://127.0.0.1:4040
Forwarding tcp://0.tcp.ngrok.io:18872 -> localhost:1099
Connections ttl opn rt1 rt5 p50 p90
5 0 0.00 0.00 5.88 17.03
我的问题是:
RemoteException
java.rmi.ConnectException: Connection refused to host: 0.tcp.ngrok.io; nested exception is:
java.net.ConnectException: Connection timed out: connect
我认为问题出在
System.setProperty("java.rmi.server.hostname","0.tcp.ngrok.io");
我应该在“ java.rmi.server.hostname”上的服务器和客户端上放什么?
(对不起,我的英语不好)
最佳答案
通常,仅需在服务器端显示什么主机名导出的存根将对世界可用。
例如,当您的服务器位于Intranet中并在某些界面上进行监听时,例如说192.168.2.1
很有用。 NAT代理可以将端口从Internet转发到该主机。通常,RMI注册表会报告在地址192.168.2.1:someport
上导出的存根蜂。但是显然您不能直接点击该地址,java.rmi.server.hostname
就会生效。通过设置该属性,RMI注册表将通告在实际Intranet地址上指定给定hostname
上导出的存根的存根,从而使客户端可以通过NAT网关上的转发端口从外部进入存根。
因此,如果您的服务器位于NAT之后,则应指定从Internet指向您的NAT网关的域。如果您的服务器具有外部IP及其在该IP上的侦听,则不需要此属性。
关于java - java rmi服务器主机名,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47270918/