1、 远程接口调用(必须实现VersionedProtocol接口)
里面有一个方法,IPC通信时会比较客户端和服务端接口的版本号。必须一致才可以
package rpc;
import org.apache.hadoop.ipc.VersionedProtocol;
public interface MyBizable extends VersionedProtocol {
	//必须具有一个版本号
	public static final long VERSION = 100L;
	//实际调用方法
	public abstract String hello(String name);
}
2、 定义远程对象的实现类
package rpc;
import java.io.IOException;
public class MyBiz implements MyBizable{
	//获取版本号
	@Override
	public long getProtocolVersion(String protocol, long clientVersion)
			throws IOException {
		// TODO Auto-generated method stub
		return VERSION;
	}
	@Override
	public String hello(String name){
		System.out.println("我被调用了");
		return "hello:" + name;
	}
}
3、 构建服务器
package rpc;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.ipc.RPC;
import org.apache.hadoop.ipc.RPC.Server;
public class MyServer {
public static final String SERVER_ADDRESS = "localhost";
public static final int SERVER_PORT = 12344;
public static void main(String[] args) throws Exception {
//public static Server getServer(final Object instance, final String bindAddress, final int port, Configuration conf)
/** Construct an RPC server.
* @param instance the instance whose methods will be called客户端调用远程接口
* @param bindAddress the address to bind on to listen for connection监听连接地址
* @param port the port to listen for connections on端口
* @param conf 配置文件
*/
final Server server = RPC.getServer(new MyBiz(), SERVER_ADDRESS, SERVER_PORT, new Configuration());
server.start();
}
}
4、客户端实现
package rpc;
import java.io.IOException;
import java.net.InetSocketAddress;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.ipc.RPC;
import org.apache.hadoop.ipc.VersionedProtocol;
public class MyClient {
	
	public static void main(String[] args) throws Exception {
		/** Construct a client-side proxy object that implements the named protocol,
		   * talking to a server at the named address. */
		final MyBizable proxy = (MyBizable) RPC.waitForProxy(MyBizable.class
              , MyBizable.VERSION
              , new InetSocketAddress(MyServer.SERVER_ADDRESS, MyServer.SERVER_PORT)
              , new Configuration());
		//普通测试
		String result = proxy.hello("hello");
		System.out.println(result);
		
		RPC.stopProxy(proxy);
	}
}
原理:(暂且不关注,在需要的时候可以再复习)

总结:hadoop的namenode,secondarynamenode,datanode,jobtrack等进程都实现了远程调用接口,也就是说他们每一个都是一个服务端Server,等待客户端的调用。相互之间彼此充当客户端和服务端的角色。
启动hadoop实际上就是启动了RPC的Server。
Hadoop技术内幕HDFS-笔记6之RPC,布布扣,bubuko.com
原文:http://www.cnblogs.com/jsunday/p/3817467.html