hadoop上rpc的程序demo及其解释

Fork from Github

package org.apache.hadoop.examples.rpc;

import java.io.IOException;

import java.net.InetSocketAddress;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.ipc.RPC;

import org.apache.log4j.Logger;

public class RPCClient {

private static transient Logger logger = Logger.getLogger(RPCClient.class);

public static void main(String[] args) throws IOException {

Barty proxy = RPC.getProxy(Barty.class, 10010, new InetSocketAddress(

“ip”, 9527), new Configuration());

String sayHi = proxy.sayHi(“tomcat”);

logger.info(sayHi);

System.out.println(sayHi);

}

}

package org.apache.hadoop.examples.rpc;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 RPCServer implements Barty{

public static void main(String[] args) throws HadoopIllegalArgumentException, IOException {
Server server = new RPC.Builder(new Configuration())
.setInstance(new RPCServer())
.setBindAddress(“ip”)
.setPort(9527)
.setProtocol(Barty.class)
.build();
server.start();

}

@Override
public String sayHi(String name) {
// TODO Auto-generated method stub
return “HI~” + name;
}

}


package org.apache.hadoop.examples.rpc;

public interface Barty {

publicstaticfinallongversionID = 10010;

public String sayHi(String name);

}

发表评论

电子邮件地址不会被公开。