Hadoop RPC实现NIO通信范例

16 篇文章 0 订阅

从hadoop的源码中剥离出了ipc,去掉了安全验证等代码,想了解hadoop的rpc的可以看下打印输出,如果想详细了解,做好debug运行试试。

代码见:http://download.csdn.net/detail/lzlchangqi/8182999

下面简单地列出打印日志,后面文章会结合NIO详细介绍有关代码及流程,

注意:每个类和Hadoop中的是对应的,只是包名改了。

1、单纯启动MainServer.java

Server类启动了三类线程端口都是7888,每隔默认15MINs 会进行“Checking for old call resonses”的清理

Responder:

Listener:

Handler:


Server.java中的代码

/** Starts the service.  Must be called before any calls will be handled. */
  public synchronized void start() {
    responder.start();
    listener.start();
    handlers = new Handler[handlerCount];
    
    for (int i = 0; i < handlerCount; i++) {
      handlers[i] = new Handler(i);
      handlers[i].start();
    }
  }

这些类的初始化在Server的构造函数中进行的

protected Server(String bindAddress, int port, 
                  Class<? extends Writable> paramClass, int handlerCount, 
                  Configuration conf) 
    throws IOException {
    this.bindAddress = bindAddress;
    this.conf = conf;
    this.port = port;
    this.paramClass = paramClass;
    this.handlerCount = handlerCount;
    this.socketSendBufferSize = 0;
    this.maxQueueSize = handlerCount * conf.getInt(
                                IPC_SERVER_HANDLER_QUEUE_SIZE_KEY,
                                IPC_SERVER_HANDLER_QUEUE_SIZE_DEFAULT);
    this.maxRespSize = conf.getInt(IPC_SERVER_RPC_MAX_RESPONSE_SIZE_KEY,
                                   IPC_SERVER_RPC_MAX_RESPONSE_SIZE_DEFAULT);
    this.readThreads = conf.getInt(
        CommonConfigurationKeys.IPC_SERVER_RPC_READ_THREADS_KEY,
        CommonConfigurationKeys.IPC_SERVER_RPC_READ_THREADS_DEFAULT);
    this.callQueue  = new LinkedBlockingQueue<Call>(maxQueueSize); 
    this.maxIdleTime = 2*conf.getInt("ipc.client.connection.maxidletime", 1000);
    this.maxConnectionsToNuke = conf.getInt("ipc.client.kill.max", 10);
    this.thresholdIdleConnections = conf.getInt("ipc.client.idlethreshold", 4000);
    
    // Start the listener here and let it bind to the port
    listener = new Listener();
    this.port = listener.getAddress().getPort();    
    this.tcpNoDelay = conf.getBoolean("ipc.server.tcpnodelay", false);

    // Create the responder here
    responder = new Responder();
    
  }
2、启动客户端MainClient.java
import java.io.IOException;
import java.net.InetSocketAddress;

import com.zhs.conf.Configuration;
import com.zhs.ipc.RPC;

public class MainClient {

	public static void main(String[] args) throws IOException {
		
		
		Configuration conf = new Configuration();
		MyProtocol myprotocol = (MyProtocol)RPC.getProxy(MyProtocol.class, MyProtocol.versionID, new InetSocketAddress(conf.get("client.ip.name"), conf.getInt("name.port",7888)), conf,1000);
		System.out.println("client receive:" + myprotocol.echo("hello world"));
		System.out.println("client receive:" +myprotocol.echo("hello China"));
		System.out.println("100+200 = " + myprotocol.sum(100,200));
		
		Person p = new Person();
		p.setName("lizhanlei");
		p.setAge(25);
		System.out.println("test object Peson's name is:"+myprotocol.print(p));
		RPC.stopProxy(myprotocol);
	}

}

打印输出如下:

Server端相应输出如下:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值