Hadoop中的远程过程调用

IPC接口

/**
 * Superclass of all protocols that use Hadoop RPC.
 * Subclasses of this interface are also supposed to have
 * a static final long versionID field.
 */
public interface VersionedProtocol 
{
  /**
   * Return protocol version corresponding to protocol interface.
   * @param 协议接口的类名//protocol The classname of the protocol interface
   * @param 客户端的版本号//clientVersion The version of the protocol that the client speaks
   * @return 协议接口的版本
   */
  public long getProtocolVersion(String protocol, 
                                 long clientVersion) throws IOException;
}
public interface IPCQueryStatus extends VersionedProtocol 
{
	IPCFileStatus getFileStatus(String filename);
}

IPC接口的实现类

public class IPCQueryStatusImpl implements IPCQueryStatus 
{	
	public IPCQueryStatusImpl() 
	{
	
	}

	@Override
	public IPCFileStatus getFileStatus(String filename) 
	{
		IPCFileStatus status=new IPCFileStatus(filename);
		System.out.println("Method getFileStatus Called, return: "+status);
		return status;
	}
	/**
	 * 用于服务器与客户端,进行IPC接口版本检查,再服务器返回给客户端时调用,如果服务器端的IPC版本与客户端不一致
	 * 那么就会抛出版本不一致的异常
	 */
	@Override
	public long getProtocolVersion(String protocol, long clientVersion) throws IOException 
	{
		System.out.println("protocol: "+protocol);
		System.out.println("clientVersion: "+clientVersion);
		return IPCQueryServer.IPC_VER;
	}
}

服务端程序

public class IPCQueryServer 
{
	public static final int IPC_PORT = 32121;
	public static final long IPC_VER = 5473L;
	
	public static void main(String[] args) {
		try {
			ConsoleAppender append=new ConsoleAppender(new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN));
			append.setThreshold(Level.DEBUG);
			BasicConfigurator.configure();
			Configuration conf = new Configuration();
			
	        IPCQueryStatusImpl queryService=new IPCQueryStatusImpl();
	        
	        System.out.println(conf);
	        
	        /**
	        * 在对象queryService上获取一个IPC服务器
	        **/
	        Server server = RPC.getServer(queryService,  //接口实现对象
	        		                      "0.0.0.0", //监听地址
	        		                      IPC_PORT,  //端口
	        		                      1, true,
	        		                      conf ); //配置类的实例
	       
	       //启动服务,
			server.start();
			
			System.out.println("Server ready, press any key to stop");
			System.in.read();
			
			//停止服务
			server.stop();
			System.out.println("Server stopped");
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
}
发布了280 篇原创文章 · 获赞 49 · 访问量 31万+

猜你喜欢

转载自blog.csdn.net/kaikai_sk/article/details/88673764