我需要:
一个客户端,与前端通信,前端与3个文件服务器通信。
我应该怎样做呢?它需要使用RMI作为分布式系统。
我还需要监视所有三个文件服务器。
据我了解,我需要建立一个RMI注册表,但是如何在一个注册表中建立三个并发服务器?
最佳答案
Okay, so am I right in thinking i'd have the following: A server interface, a server implementation, and a master server which creates the three servers (with unique names) and finally a client?
- “主服务器”需要在自己的本地主机上创建一个注册表,将自己绑定(bind)到注册表,以便从属服务器可以找到它,并导出一个远程接口(interface),让服务器可以向其注册自己。
- 主服务器必须代表从服务器绑定(bind)到此注册表,因为您无法绑定(bind)到远程注册表。但实际上slave根本不需要绑定(bind)Registry,只需要向master注册即可。
- 主站需要导出第二个远程接口(interface),该接口(interface)向客户端提供 API,该接口(interface)提供上传 API,其实现执行平衡操作。我会将此接口(interface)与从属设备使用的接口(interface)分开,出于安全原因和简单性:您不需要客户端尝试成为从属设备,或担心远程接口(interface)中与从属设备相关的方法是什么。<
- 所有这些服务器和注册表都可以在端口 1099 上运行。
- 从站可能是同一服务的多个实例,因此它们都使用通用的远程接口(interface)。该接口(interface)提供了上传到从站的 API,并且还需要允许每个从站提供有关每个从站有多满的信息,可能作为上传方法的返回值,或者作为查询方法。
快速草图:
public interface UploadMaster extends Remote
{
void upload(String name, byte[] contents) throws IOException, RemoteException;
}
public interface LoadBalancingMaster extends Remote
{
void register(Slave slave) throws RemoteException;
void unregister(Slave slave) throws RemoteException;
}
public interface Slave extends Remote
{
/** @return the number of files now uploaded to this slave. */
int upload(String name, byte[] contents) throws IOException, RemoteException;
int getFileCount() throws RemoteException;
}
我希望这是家庭作业。对于文件传输来说,RMI 是一个糟糕的选择,因为它将整个参数列表捆绑到两端的内存中,而不是提供流接口(interface)。
关于java - 如何使用 Java RMI 创建多个服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49118941/