美文网首页
(初级)HDFS中的RPC

(初级)HDFS中的RPC

作者: 呆老板 | 来源:发表于2017-07-24 15:04 被阅读0次

    RPC采用客户机/服务器模式。请求程序就是一个客户机,而服务提供程序就是一个服务器。首先,客户机调用进程发送一个有进程参数的调用信息到服务进程,然后等待应答信息。在服务器端,进程保持睡眠状态直到调用信息的到达为止。当一个调用信息到达,服务器获得进程参数,计算结果,发送答复信息,然后等待下一个调用信息,最后,客户端调用进程接收答复信息,获得进程结果,然后调用执行继续进行。

    hadoop的整个体系结构就是构建在RPC之上的(见org.apache.hadoop.ipc)

    package rpc;

    import org.apache.hadoop.ipc.VersionedProtocol;

    public interface MyBizable extends VersionedProtocol{

    long VERSION = 2345245L;

    public abstract String hello(String name);

    }

    --------------------------------------------------------

    package rpc;

    import java.io.IOException;

    import org.apache.hadoop.ipc.VersionedProtocol;

    public class MyBiz implements VersionedProtocol, MyBizable{

    /* (non-Javadoc)

    * @see rpc.MyBizable#hello(java.lang.String)

    */

    @Override

    public String hello(String name) {

    System.out.println("服务器端,我被调用了");

    return "hello "+name;

    }

    /* (non-Javadoc)

    * @see rpc.MyBizable#getProtocolVersion(java.lang.String, long)

    */

    @Override

    public long getProtocolVersion(String protocol, long clientVersion) throws IOException {

    return VERSION;

    }

    }

    --------------------------------------------------------

    package rpc;

    import org.apache.hadoop.conf.Configuration;

    import org.apache.hadoop.ipc.RPC;

    import org.apache.hadoop.ipc.RPC.Server;

    public class MyServer {

    static final String ADDRESS="localhost";

    static final int PORT=12345;

    public static void main(String[] args) throws Exception {

    /**

    * @param instance the instance whose methods will be called此实例中的方法会被调用

    * @param conf the configuration to use

    * @param bindAddress the address to bind on to listen for connection用于监听链接的地址

    * @param port the port to listen for connections on

    * @param numHandlers the number of method handler threads to run

    * @param verbose whether each call should be logged

    */

    Server server = RPC.getServer(new MyBiz(), ADDRESS, PORT, new Configuration());

    server.start();

    }

    }

    --------------------------------------------------------

    package rpc;

    import java.net.InetSocketAddress;

    import org.apache.hadoop.conf.Configuration;

    import org.apache.hadoop.ipc.RPC;

    public class MyClient {

    public static void main(String[] args) throws Exception{

    /**

    * 构造一个客户端代理对象,该对象实现了命名的协议

    * 代理对象会与指定地址的服务端通话

    */

    MyBizable proxy = (MyBizable)RPC.waitForProxy(

    MyBizable.class,//Class protocol,

    MyBizable.VERSION,

    new InetSocketAddress(MyServer.ADDRESS,MyServer.PORT),

    new Configuration());

    //hello()的调用,在RPC的环境下,是发生在server端,而非client端

    String result = proxy.hello("yanxin");

    System.out.println("客户端:"+result);

    //关闭网络连接

    RPC.stopProxy(proxy);

    }

    }

    相关文章

      网友评论

          本文标题:(初级)HDFS中的RPC

          本文链接:https://www.haomeiwen.com/subject/whebkxtx.html