目录
服务端使用:Naming.bind
客户端调用
Naming.bind使用:
Naming.bind("rmi://127.0.0.1:1099/aaa",new detailimpl ());
bind函数细节
代码,可以看到最终返回了 registry.bind(parsed.name, obj);
public static void bind(String name, Remote obj)
throws AlreadyBoundException,
java.net.MalformedURLException,
RemoteException
{
ParsedNamingURL parsed = parseURL(name);
Registry registry = getRegistry(parsed);
if (obj == null)
throw new NullPointerException("cannot bind to null");
registry.bind(parsed.name, obj);
}
服务端使用:Registry.bind
客户端对应:
detail obj = (detail) Naming.lookup("hello");
总结:registry.rebind和Naming.rebind绑定的区别。前者是使用RMI注册表绑定,所以不需要写完整的RMI URL了;后者是通过java的名称服务进行绑定,由于名称服务不止为RMI框架提供查询服务,所以在绑定是要书写完成的RMI URL。
其他
1 客户端registry.lookup和Naming.lookup其实也是一样的
2 “Naming.rebind”和“Naming.bind”的区别。前置是指“重绑定”,如果“重绑定”时“RMI 注册表”已经有了这个服务name的存在,则之前所绑定的Remote Object将会被替换;而后者在执行时如果“绑定”时“RMI注册表”已经有这个服务name的存在,则系统会抛出错误。所以除非您有特别的业务要求,那么建议使用rebind方法进行Remote Object绑定。
3 注册中心的代码为如下,可以服务端代码写一起,也可分开写
LocateRegistry.createRegistry(1099);
参考:
https://blog.csdn.net/weixin_33676492/article/details/91594152
https://www.cnblogs.com/liulaolaiu/p/11744328.html