具有多个主机的 RMI
我正在开发一个程序,该程序使用 RMI 进行 2 个连接,一个从客户端到服务器,另一个用于客户端上的 2 个虚拟机之间的通信。
看来 RMI 注册表必须在服务器上运行(否则我会得到 java.rmi.AccessException:Registry.Registry.rebind disallowed; origin
所以我尝试在服务器和客户端上创建一个注册表。从客户端上的一个虚拟机到另一台虚拟机的通信是使用在客户端上创建的第二个注册表来完成的。第二个注册表的创建没有任何抱怨。但是,因为我之前设置了 java.rmi.server.hostname
属性,所以我收到另一个异常: java.rmi.ConnectException: Connection精神上拒绝主机:
我有一个肮脏的解决方案;我没有调用客户端注册表的每个 Registry.rebind()
,而是调用
System.setProperty("java.rmi.server.hostname", "localhost");
Registry registry = LocateRegistry.
Remote stub = (Remote) UnicastRemoteObject.exportObject(remote, 0);
registry.rebind(name, stub);
System.setProperty("java.rmi.server.hostname", <server ip>);
是否有更好的方法来处理此问题?可以干净地创建和使用两个注册表,或者客户端和服务器可以共享一个注册表吗?
I'm working on a program that uses RMI for 2 connections, one from client to server, and onether for communication between 2 virtual machines on the client.
It seems the RMI registry has to run on the server (otherwise I get java.rmi.AccessException: Registry.Registry.rebind disallowed; origin <client ip> is non-local host
). In addition, the client could not connect to the server without first calling System.setProperty("java.rmi.server.hostname", <server ip>);
.
So I tried creating a registry on both the server and the client. The communication from one virtual machine on the client to the other is done using a second registry created on the client. This second registry is created without complaints. However, because I had set the java.rmi.server.hostname
property before, I get another exception: java.rmi.ConnectException: Connection refused to host: <server ip>
.
I have a dirty solution in place; in stead of every Registry.rebind()
for the client registry, I call
System.setProperty("java.rmi.server.hostname", "localhost");
Registry registry = LocateRegistry.
Remote stub = (Remote) UnicastRemoteObject.exportObject(remote, 0);
registry.rebind(name, stub);
System.setProperty("java.rmi.server.hostname", <server ip>);
Is there a better way to deal with this problem? Can two registries be created and used cleanly, or can client and server share a registry?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要在客户端中编写
System.setProperty("java.rmi.server.hostname",);
,因为 hostname 定义了本地绑定的注册表对象的应用程序的主机名。 参阅此处:java.rmi 属性请 甚至不需要在不同的端口上运行注册表。尽量保持简单。如果有什么不清楚的可以再问。
You don't need to writer
System.setProperty("java.rmi.server.hostname", <server ip>);
in the client because hostname defines the application's hostname for locally binded registry objects. See here: java.rmi PropertiesYou don't even need to run registries on different ports. Just try to keep it simple. If anything is unclear you can ask again.