RMI服务器与RMI注册表 [英] RMI Server vs. RMI Registry

查看:314
本文介绍了RMI服务器与RMI注册表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Oracle常见问题解答页面上关于Java RMI,它说:


理论上,Java远程方法调用(Java RMI)服务器可以在任何主机上将
,它通常与运行注册表的
上的主机相同,并且在不同的端口上。即使
服务器误认为其主机名或IP地址(或主机名
根本无法被客户端解析),它仍会使用该错误的名称导出所有
对象主机名,但是每次尝试接收其中一个对象时,您都会看到
异常。


我不了解RMI服务器和RMI注册表之间的区别。我以为他们是同一回事。如果RMI注册表不是某种类型的服务器,怎么能工作?

解决方案

什么是RMI注册表:



RMI注册表是一项服务,其中注册了远程对象,并由RMI客户端查找已注册的远程对象。如果您希望对象可以远程访问(可能有很多原因,例如您不断更新逻辑,并且每次都不可行将其交付给实现,因此允许通过RMI进行远程调用),然后在RMI注册表中注册该对象,然后在RMI中进行注册。客户端将查找远程对象(使用对象的远程引用),然后可以调用远程对象上的方法。



下面是从Oracle Javadoc定义注册表


注册表是将名称映射到远程对象的远程对象。
服务器在注册表中注册其远程对象,以便可以查找它们。当对象想要在远程
对象上调用方法时,它必须首先使用其名称查找远程对象。
注册表将对远程
对象的引用返回到调用对象,使用该引用可以调用远程方法。




什么是RMI服务器:



RMI服务器是运行JVM且对象(远程对象)正在运行的实际服务器。 RMI客户端最终需要此对象。



根据您的担心,是的,此服务器(RMI服务器)可能与运行RMI注册表的服务器不同。您可以理解为什么!我可以在同一RMI注册表中注册来自不同服务器的对象,并且可以在完全不同的服务器上运行该注册表。请阅读下面的更多内容以获得更多解释。



Java RMI客户端如何与远程Java RMI服务器联系?



要使Java RMI客户端与远程Java RMI服务器联系,客户端必须首先持有对该服务器的引用(这是RMI注册表出现的地方,以便为您提供对RMI服务器的引用 )。 Naming.lookup方法调用是客户端最初获取对远程服务器的引用的最常用机制。



每个远程引用都包含一个服务器主机名和端口号,允许客户端执行以下操作:找到为特定远程对象提供服务的VM(这是RMI服务器所在的位置)。 Java RMI客户端具有远程引用后,客户端将使用引用中提供的主机名和端口打开与远程服务器的套接字连接。



请务必阅读,来自相同的Oracle常见问题解答。 / p>

您可以很好地与RMI注册表连接,但是您可能无法获取远程对象,因此人们会报告 java.net.UnknownHostException ,这意味着找不到RMI注册表,但可以找到远程对象的引用,但找不到RMI服务器,该RMI服务器实际上是在托管远程对象或运行对象所在的JVM,或者客户端无法连接。



因此,RMI注册表和RMI服务器是两个不同的东西。



可以将HTTP服务器用于提供对服务器上可用的HTTP资源(超文本文档)的访问。但是,通常超文本文档将与HTTP服务器位于同一物理设备上,但是RMI注册表可以提供对位于不同服务器(RMI服务器)上的远程对象的引用的访问权限。


On Oracle's FAQ page about Java RMI, it says:

While the Java Remote Method Invocation (Java RMI) server can theoretically be on any host, it is usually the same host as that on which the registry is running, and on a different port. Even if the server is mistaken about its hostname or IP address (or has a hostname that simply isn't resolvable by clients), it will still export all of its objects using that mistaken hostname, but you will see an exception every time you try to receive one of those objects.

I don't understand the difference between the RMI Server and the RMI Registry. I thought they were the same thing. How can the RMI Registry work if it wasn't a server of some sorts?

解决方案

What is RMI registry:

RMI registry is a service where remote objects are registered and registered remote objects are looked up by RMI clients. If you want your object to be remotely accessible (could be many reason like you keep on updating the logic and not feasible to ship to the implementation each time, so allow remote invocation through RMI) then register it in a RMI registry and then a RMI client will look up the remote object (using remote reference of the object) and then can invoke the methods on the remote object.

Below is definition of registry from Oracle Javadoc

A registry is a remote object that maps names to remote objects. A server registers its remote objects with the registry so that they can be looked up. When an object wants to invoke a method on a remote object, it must first lookup the remote object using its name. The registry returns to the calling object a reference to the remote object, using which a remote method can be invoked.

What is RMI server:

RMI server is that actual server where the JVM is running and the object (remote object) is living. RMI client ultimately wants this object.

As per your concern, yes this server (RMI server) could be different from the server where RMI registry is running. And you could understand why! I could register object from different servers in same RMI registry and I can have that registry running on a totally different server. Please read more below for more explanation on this.

How do Java RMI clients contact remote Java RMI servers?

For an Java RMI client to contact a remote Java RMI server, the client must first hold a reference to the server (this is where RMI registry is coming into picture, to give you reference to the RMI server). The Naming.lookup method call is the most common mechanism by which clients initially obtain references to remote servers.

Every remote reference contains a server hostname and port number that allow clients to locate the VM that is serving a particular remote object (this is where RMI server is coming into picture). Once a Java RMI client has a remote reference, the client will use the hostname and port provided in the reference to open a socket connection to the remote server.

Please do read this from same Oracle FAQs.

You can very well connect with the RMI registry but you may not be able to get the remote object and that's when people report java.net.UnknownHostException, which means that RMI registry is able to give the reference of remote object BUT RMI server which is actually hosting the remote object or running the JVM where object is living, is not found or client is not able to connect.

So, RMI registry and RMI server are 2 different things.

An analogy could be that HTTP server is used to provide access to HTTP resources (hyper text documents) which are available on a server. However, typically hypertext docs will be on same physical box as HTTP server but RMI registry can provide access to reference of remote objects which are on different server (RMI server).

这篇关于RMI服务器与RMI注册表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆