RPC(Remote Peocedure Call):远程进程调用


RPC是Hadoop框架运行的基础。


RPC:一方叫做客户端,一方是服务器端,


    其中被调用对象的调用是在客户端,执行是在服务器端


    1.服务器端提供的对象必须是一个接口,且必须继承VersionedProtocol,

    2.客户端能够调用的对象的方法必须位于对象的接口中。



   Hadoop Rpc的调用


   1.MyBizI(被调用对象接口)

   2.MyBiz(实现类)

   3.MyServer

   4.MyClient

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

//被调用对象必须是一个接口,实现VersionedProtocal

//被调用对象的方法必须位于接口中    

public interface MyBizI extends VersionedProtocol{

   public abstract String hello(String name);

}

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

//实现类,重写getProtocolVersion方法,用来判断Server和Client端调用的是不是一个版本的

public class MyBiz implements MyBizI {

/*

* (non-Javadoc)

*

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

*/

public String hello(String name) {

return "hello " + name;

}


@Override

public long getProtocolVersion(String protocol, long clientVersion)

throws IOException {

// TODO Auto-generated method stub

return 111L;

}


}

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

//Server端

public class MyServer {

   public static final String SERVER_ADDREDD="localhost";

   public static final int SERVER_PORT=123;

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

   Server server = RPC.getServer(new MyBiz(), SERVER_ADDREDD, SERVER_PORT, new Configuration());

   server.start();

}

}

//Client端

public class MyClient {

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

       MyBizI waitForProxy = (MyBizI)RPC.waitForProxy(MyBizI.class, 111L, new InetSocketAddress(MyServer.SERVER_ADDREDD,MyServer.SERVER_PORT), new Configuration());

       String hello = waitForProxy.hello("wk");

       System.out.println(hello);

       RPC.stopProxy(waitForProxy);

       }

}