用于远程 ip(主机)的 Java RMI
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35403765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java RMI for remote ip (host)
提问by Mahendra
I am newbie. I cannot understand RMI
correctly. There are tons of tutorials available on the internet ,but all of them are for the local host as I can understand. Both server and client run on the same machine.
我是新手。我无法RMI
正确理解。互联网上有大量教程可用,但据我所知,所有教程都是针对本地主机的。服务器和客户端都运行在同一台机器上。
I want to run client on any machine and the host will be on the one computer lets consider IP - 11.11.11.11
. On the 1099
.
But how can I achieve this, where should I specify my IP on the client side. As I understand naming convertion is used, like DNS but anyway when I need to connect to some machine remotely I need to know at least IP address (+mask) and port.
我想在任何机器上运行客户端,而主机将在一台计算机上,让我们考虑一下IP - 11.11.11.11
。在1099
.
但是我怎样才能做到这一点,我应该在客户端哪里指定我的 IP。据我所知,使用了命名转换,如 DNS,但无论如何,当我需要远程连接到某台机器时,我至少需要知道 IP 地址(+掩码)和端口。
I guess I missed something really important.
我想我错过了一些非常重要的东西。
Please give some example how to configure RMI remotly not on the same host.
请举例说明如何在不同主机上远程配置 RMI。
回答by Mahendra
First you have to setup a server whose method or object can be accessed by any remote client Below is example code for the server.
首先,您必须设置一个服务器,其方法或对象可以被任何远程客户端访问 下面是服务器的示例代码。
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface MyCalc extends Remote{
int add(int a, int b) throws RemoteException;
}
import java.rmi.RemoteException;
public class MyCalcImpl implements MyCalc {
@Override
public int add(int a, int b) throws RemoteException {
return (a + b);
}
}
Start the rmi registry on server machine so you can register your object to this registry and better you run it where you have placed your classes otherwise you will get ClassNotFound.
在服务器机器上启动 rmi 注册表,这样您就可以将您的对象注册到此注册表,最好在放置类的地方运行它,否则您将获得 ClassNotFound。
rmiregistry 1099
Note: you might need to change the port if port is already in use.
注意:如果端口已被使用,您可能需要更改端口。
Register you object to rmi registry with name 'calculator'.
使用名称“计算器”向 rmi 注册表注册您的对象。
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
public class MyRMIServer {
public static void main(String[] args) throws Exception {
System.setProperty("java.security.policy","file:///tmp/test.policy");
if (System.getSecurityManager() == null) {
System.setSecurityManager(new SecurityManager());
}
try {
String name = "Calculator";
MyCalc engine = new MyCalcImpl();
MyCalc stub = (MyCalc) UnicastRemoteObject.exportObject(engine, 0);
Registry registry = LocateRegistry.getRegistry(1099);
System.out.println("Registering Calculator Object");
registry.rebind(name, stub);
} catch (Exception e) {
System.err.println("Exception:" + e);
e.printStackTrace();
}
}
}
Note: To run the program you have to setup a security policy file and for that creat a file e.g. test.policy and copy below content.
注意:要运行该程序,您必须设置一个安全策略文件并为此创建一个文件,例如 test.policy 并复制以下内容。
grant {
permission java.security.AllPermission;
permission java.net.SocketPermission "localhost:1099", "connect, resolve";
permission java.net.SocketPermission "127.0.0.1:1099", "connect, resolve";
permission java.net.SocketPermission "localhost:80", "connect, resolve";
};
You can change IP and port as per your case.
您可以根据自己的情况更改 IP 和端口。
After starting the server, suppose your server's IP address is 11.11.11.11 then you can invoke the MyCalc's add() on the server. So on your client machine your client code would be like:
启动服务器后,假设您的服务器的 IP 地址是 11.11.11.11,那么您可以在服务器上调用 MyCalc 的 add()。因此,在您的客户端机器上,您的客户端代码将如下所示:
Copy the MyCalc class from server to client machine so you can set it to the classpath while compiling client's code.
将 MyCalc 类从服务器复制到客户端计算机,以便您可以在编译客户端代码时将其设置为类路径。
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class MyRMIClient {
public static void main(String args[]) {
System.setProperty("java.security.policy","file:///tmp/test.policy");
if (System.getSecurityManager() == null) {
System.setSecurityManager(new SecurityManager());
}
try {
String name = "Calculator";
String serverIP = "11.11.11.11"; // or localhost if client and server on same machine.
int serverPort = 1099;
Registry registry = LocateRegistry.getRegistry(serverIP, serverPort);
MyCalc mycalc = (MyCalc) registry.lookup(name);
int result = mycalc.add(10, 20);
System.out.println("Result:" + result);
} catch (Exception e) {
System.err.println("ComputePi exception:");
e.printStackTrace();
}
}
}
compile and test the client's code.
编译和测试客户端的代码。
EDIT: edited to remove dependency on rmi compiler (rmic)
编辑:编辑以删除对 rmi 编译器 (rmic) 的依赖
回答by user207421
You only have to specify the server's IP address in one place: the lookup string supplied to Naming.lookup().
您只需在一处指定服务器的 IP 地址:提供给的查找字符串 Naming.lookup().
[Unless you have the Linux problem referred to in the RMI FAQ item A.1.]
[除非您遇到 RMI 常见问题 A.1 项中提到的 Linux 问题。]