从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端相应输出如下: