现在的位置: 首页 > 综合 > 正文

hadoop中RPC的使用方法—DEMO

2013年06月17日 ⁄ 综合 ⁄ 共 1832字 ⁄ 字号 评论关闭

 

TEAM : I.S.T.O
AUTHOR :  Jerry

hadoop其实不难 不过部署起来不太容易!毕竟是分布式  这里简单写了一个demo 其实很早就写了 原来文地址

http://hi.baidu.com/zhyhongyuan/blog/item/1906368127ddc6dabd3e1ef0.html

明明是RPC  转的人看都不看就写为IPC     无语...

看看啥时候分布式也出安全隐患了~ 叫上KJ一起研究~呵呵

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"));
       }
      
   }
  
}

抱歉!评论已关闭.