如何构建分布式文件服务器?

时间:2015-05-07 09:41:43

标签: cdn distributed fileserver

设置分布式文件服务器所需的步骤或工具是什么,这些服务器将用于为网站提供大型媒体文件。该网站将在局域网中,而不是互联网。该系统就像一个CDN。

详细说明是可选的。我只想知道我应该在谷歌搜索什么,如果没有在这里找到。

1 个答案:

答案 0 :(得分:-1)

我使用Java RMI制作了类似的东西(FTP服务器)。如果你习惯了JAVA,这是一个不错的选择。

RMI的关键思想是创建服务器,服务器类的接口(由客户端调用)和客户端类。

这是一个从维基百科中获取并重写的例子。

您的服务器类

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.registry.*; 
public class RmiServer
    extends UnicastRemoteObject 
    implements RmiServerIntf {
    public static final String MESSAGE = "Hello World";

    public RmiServer() throws RemoteException {
        super(0);    // required to avoid the 'rmic' step, see below
    }

    public String getMessage() {
        return MESSAGE;
    }

    public static void main(String args[]) throws Exception {
        System.out.println("RMI server started");

        try { //special exception handler for registry creation
            LocateRegistry.createRegistry(1099); //you need to register an endpoint in the server machine
            System.out.println("java RMI registry created.");
        } catch (RemoteException e) {
            //do nothing, error means registry already exists
            System.out.println("java RMI registry already exists.");
        }

        //Instantiate RmiServer

        RmiServer obj = new RmiServer();

        // Bind this object instance to the name "RmiServer"
        Naming.rebind("//localhost/RmiServer", obj); //localhost you need to change to your server machine address
        System.out.println("PeerServer bound in registry");
    }
}

聚苯乙烯。为了使您的服务器正常工作,您需要使用命令rmic 例如

rmic RmiServer

(注意你可能需要在Linux中使用命令locate rmic找到rmic,在windows中我不知道)。

您的接口,其中包含将由客户端调用的服务器中的所有方法

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface RmiServerIntf extends Remote {
    public String getMessage() throws RemoteException;
}

和客户端类

import java.rmi.Naming;

public class RmiClient { 
    public static void main(String args[]) throws Exception {
        RmiServerIntf obj = (RmiServerIntf)Naming.lookup("//host/RmiServer"); //host is your server machine
        System.out.println(obj.getMessage()); 
    }
}

现在您只需要编写方法来传递您想要的任何文件。请注意,在服务器端使用线程是必需的,以便保持多台机器同时进行交互。

以下是a decent tutorial,其中包含更多示例和更广泛的解释。 干杯