简体   繁体   English

java rmi 服务器主机名

[英]java rmi server hostname

I have two RMI java applications:我有两个 RMI java 应用程序:

server服务器

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

client客户

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

Communication between the two is done with the help of Ngrok:两者之间的通信是在 Ngrok 的帮助下完成的:

ngrok tcp 1099

result结果

   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

My problem is :我的问题是:

 RemoteException
 java.rmi.ConnectException: Connection refused to host: 0.tcp.ngrok.io; nested exception is: 
 java.net.ConnectException: Connection timed out: connect

I think the problem is at我认为问题出在

System.setProperty("java.rmi.server.hostname","0.tcp.ngrok.io");

what should I put on the server and client at "java.rmi.server.hostname" ?我应该在“java.rmi.server.hostname”的服务器和客户端上放什么?

(Sorry for my bad english) (对不起,我的英语不好)

In general it's only required on server side to show on what hostname exported stubs will be available to the world.一般来说,只需要在服务器端显示哪些主机名导出的存根将可供世界使用。

It is useful for example, when your server is in the intranet and listens on some interface lets say 192.168.2.1 .例如,当您的服务器位于 Intranet 中并在某个接口上侦听时,它很有用,比如说192.168.2.1 NAT proxy can have ports forwarded to that host from internet. NAT 代理可以将端口从 Internet 转发到该主机。 Normally RMI registry would report stubs being exported on address 192.168.2.1:someport .通常 RMI 注册表会报告在地址192.168.2.1:someport上导出的存根。 But obviously you cannot hit that address directly java.rmi.server.hostname comes in action.但显然你不能直接点击那个地址java.rmi.server.hostname By setting that property, RMI registry will announce stubs being exported on given hostname insteed on actual intranet address, allowing clients to get to the stubs from outside world trough forwarded ports on NAT gateway.通过设置该属性,RMI 注册中心将宣布在给定hostname上导出存根,而不是在实际内部网地址上,允许客户端通过 NAT 网关上的转发端口从外部访问存根。

So if your server is behind NAT, it should specify the domain that points to your NAT gateway from the Internet.因此,如果您的服务器位于 NAT 之后,它应该指定从 Internet 指向您的 NAT 网关的域。 If your server has external IP and its listening on that IP, then this property is not needed.如果您的服务器具有外部 IP 并且它正在侦听该 IP,则不需要此属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM