hadoop中RPC的用法 ( by quqi99 )

                                               hadoop中RPC的用法 ( by quqi99 )



作者:张华 发表于:2008-01-25  ( http://blog.csdn.net/quqi99 )

版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明。

 

 

package jcss.search;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.InetSocketAddress;

import jcss.search.web.spider.rpc.Para;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.ipc.RPC;
import org.apache.hadoop.ipc.VersionedProtocol;

/**
 ** @作者 张华
 ** @时间 上午10:35:25
 ** @描述
 **/
public class Test {

    //可以同时调用多台机器上的RPC
    private static final int SERVERPORT1 = 1234;
    private static final String SERVERADDRESS1 = "172.17.1.122";
   
    private static final int SERVERPORT2 = 1235;
    private static final String SERVERADDRESS2 = "172.17.1.122";
  
   
   
    //里面所有传进去的参数与传出的参数均要实现WritableComparable接口
    public class Para implements WritableComparable{
   private Integer taskId;   
   public Integer getTaskId() {
    return taskId;
   }
   public void setTaskId(Integer taskId) {
    this.taskId = taskId;
   }
   public int compareTo(Object o) {
    return 0;
   }
   public void readFields(DataInput in) throws IOException { 
    taskId = in.readInt();
   }
   public void write(DataOutput out) throws IOException {
    out.writeInt(taskId);
   } 
  }
   
    //hadoop配置信息
    private static Configuration conf=new Configuration();
   
    //版本接口
    public interface AsopSpiderProtocol extends VersionedProtocol {
     public static final long versionID = 1L;
    }
   
    //定义接口
    public interface Serverif{
        public String method(String args);
    }
   
    //服务器端的实现
    public static class ServerImpl implements Serverif, AsopSpiderProtocol{
   
      public long getProtocolVersion(String protocol, long clientVersion) {
    return AsopSpiderProtocol.versionID;
   }
    
        //业务逻辑的实现
        public String method(String args){
            return args;
        }

    }
   
    public static void main(String args[]) throws Exception {
        ServerImpl si = new ServerImpl();
        org.apache.hadoop.ipc.Server server = RPC.getServer(si,SERVERADDRESS1,SERVERPORT1, 10, true,conf);
        server.start();
        server.join();
    }

    //客户端的实现
    public static class Client {
        //利用代理的方式调用,如果通过代理方式getProxy,服务器只能有一个
        public String method_proxy(String args) throws Exception{
            InetSocketAddress sa=new InetSocketAddress(SERVERADDRESS1,SERVERPORT1);
            Serverif si=(Serverif) RPC.getProxy(Serverif.class,AsopSpiderProtocol.versionID, sa, conf);
            return si.method(args);
        }
   
        //利用反射的方式调用,如果通过反射方式,服务器可以有多个,
        //参数为一个二维数据,相对应每个服务器的方法
        public String method_reflected(String args) throws Exception{
            InetSocketAddress[] sa=new InetSocketAddress[]{
                new InetSocketAddress(SERVERADDRESS1,SERVERPORT1),
                new InetSocketAddress(SERVERADDRESS2,SERVERPORT2)};
           
           
            Para para = new Para();
            para.setTaskId(111);
            Object[][] params = new Object[1][1];
       params[0][0] = para;
     
            //根据反射找方法
            Method METHOD = Serverif.class.getMethod("method", new Class[] {String.class});
           
            //找到方法后,传入调用参数params,调用方法
            Object[] objs = (Object[]) RPC.call(METHOD, params, sa, conf);
            String resultFormOneMashine = (String)objs[0];
            return resultFormOneMashine;
        }
       
        public static void main(String args[]) throws Exception {
            Client c=new Client();
            System.out.println(c.method_proxy("hello world"));
        }
       
    }

}

你可能感兴趣的:(hadoop,exception,object,String,Integer,import)