hadoop中RPC的使用方法

public class UseRPC { //hadoop配置信息
private static Configuration conf=new Configuration(); //定义接口
public interface Serverif{
public String method(String args); public static class ServerImpl implements Serverif{ //业务逻辑的实现
public String method(String args){
return args;
} } public static void main(String args[]) throws Exception {
ServerImpl si = new ServerImpl();
int port=8668;
org.apache.hadoop.ipc.Server server = RPC.getServer(si, port, 10, true,conf);
server.start();
server.join(); public static class Client { private static final Method GETTASK; public String method_proxy(String args) throws Exception{
InetSocketAddress sa=new InetSocketAddress("192.168.1.1",8668);
Serverif si=(Serverif) RPC.getProxy(Serverif.class, sa, conf);
return si.method(args); //参数为一个二维数据,相对应每个服务器的方法
public String method_reflected(String args) throws Exception{
InetSocketAddress[] sa=new InetSocketAddress[]{
new InetSocketAddress("192.168.1.1",8668),
new InetSocketAddress("192.168.1.2",8668)};
Object[][] params = new Object[2][1];
params[0][0]=String.class;
params[1][0]=String.class; METHOD = Serverif.class.getMethod("method", new Class[] {String.class});
return (String)RPC.call(METHOD, params, sa, conf); 
public static void main(String args[]) throws Exception {
String remoteIP="192.168.1.1";
int port=8668;
Client c=new Client();
System.out.println(c.method_proxy("hello world")); 
}
 

更多信息请查看 java进阶网 http://www.javady.com

你可能感兴趣的:(hadoop,Hadoop入门,hadoop教程)