首页 > 技术文章 > RPC--HDFS节点间的沟通桥梁

beichenroot 2019-04-27 13:20 原文

 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);

   }
}

推荐阅读