首页 > 其他 > 详细

RPC--HDFS节点间的沟通桥梁

时间:2019-04-27 13:54:20      阅读:170      评论:0      收藏:0      [点我收藏+]

 RPC(Remote Procedure Call, )

    RPC使SocketSocket(client stub)(stub)(agent)(agent)RPC便

技术分享图片

  1. (client stub)(encode);

  2. client stub使Socket

  3. (UDP, TCP)

  4. (server stub)(decode)

  5. server stub

  6. Socket

    SocketSocketSocketIP

    Socket/RPC

    HDFSRPCProxyHadoop RPC使

  1. RPCRPC

  2. RPCHadoop RPCJava

  3. RPC Server使getServer()RPC Serverstart()Server

  4. RPC ClientRPC使getProxy()

HDFS RPC

    

package beichen.rpc.server;
import org.apache.hadoop.ipc.VersionedProtocol;
publiinterface MyInterface extends VersionedProtocol{    
  //使
  
  publistatilong versionID = 1;  

  //

  public String sayHello(String name);
}

package beichen.rpc.server;
import java.io.IOException;
import org.apache.hadoop.ipc.ProtocolSignature;
public class MyInterfaceImpl implements MyInterface {
@Override
  public String sayHello(String name) {

  System.out.println("*********Server **********");

  return "Hello "+name;

  }

@Override
public ProtocolSignature getProtocolSignature(String arg0, long arg1, int arg2) throws IOException {

  //

  return new ProtocolSignature(MyInterface.versionID, null);

}

@Override
  public long getProtocolVersion(String arg0, long arg1) throws IOException {

  //

  return MyInterface.versionID;

  }
}
package beichen.rpc.server;
import java.io.IOException;
import org.apache.hadoop.HadoopIllegalArgumentException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.ipc.RPC;
import org.apache.hadoop.ipc.RPC.Server;
public class MyRPCServer {

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

    //Hadoop RPCRPC Server,使RPC Builder

    RPC.Builder builder = new RPC.Builder(new Configuration());

    //Server

    builder.setBindAddress("localhost");

    builder.setPort(7788);

    //

    builder.setProtocol(MyInterface.class);
    //

    builder.setInstance(new MyInterfaceImpl());
    //

    // RPC Server

    Server server = builder.build();

    //Server

    server.start();

  }
}

    

package beichen.rpc.client;
import java.io.IOException;
import java.net.InetSocketAddress;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.ipc.RPC;
import beichen.rpc.server.MyInterface;
public class MyRPCClient {
  // 使Hadoop RPCServer 

  publistatic void main(String[] args) throwException {

    //Server

    MyInterface proxy = RPC.getProxy(MyInterface.class,
  MyInterface.versionID,
  new InetSocketAddress("localhost", 7788),
                   
new Configuration());

    //使Server

    String result = proxy.sayHello("Tom");

    System.out.println(result);

   }
}

RPC--HDFS节点间的沟通桥梁

原文:https://www.cnblogs.com/beichenroot/p/10778437.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!