java rmi 注册,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.

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值