【Hadoop-IPC】

一、WritableRpcEngine

  1. Interface IHello 服务接口类
    package com.harry.hadoop_eco.hadoop.examples.common.rpc.writable_;
    
    import org.apache.hadoop.ipc.ProtocolInfo;
    import org.apache.hadoop.ipc.VersionedProtocol;
    
    /**
     * Created by harry on 2/2/18.
     */
    @ProtocolInfo(protocolVersion = IPCServer.IPC_VER, protocolName = "IHello")
    public interface IHello extends VersionedProtocol{
        public Result sayHi(String name);
    }
    
    1.  
  2. Service HelloImpl 服务实现类
    package com.harry.hadoop_eco.hadoop.examples.common.rpc.writable_;
    
    import org.apache.hadoop.ipc.ProtocolSignature;
    
    import java.io.IOException;
    
    /**
     * Created by harry on 2/2/18.
     */
    public class HelloImpl implements IHello {
        public Result sayHi(String name) {
            return new Result("hello " + name);
        }
    
        public long getProtocolVersion(String protocol, long clientVersion) throws IOException {
            return IPCServer.IPC_VER;
        }
    
        public ProtocolSignature getProtocolSignature(String protocol, long clientVersion, int clientMethodsHash) throws IOException {
            return null;
        }
    }
    
    1.  
  3. Result
    1. package com.harry.hadoop_eco.hadoop.examples.common.rpc.writable_;
      
      import org.apache.hadoop.io.Writable;
      
      import java.io.DataInput;
      import java.io.DataOutput;
      import java.io.IOException;
      
      /**
       * Created by harry on 2/2/18.
       */
      public class Result implements Writable {
          private String result;
      
          public Result(){
      
          }
      
          public Result(String result) {
              this.result = result;
          }
      
          public void write(DataOutput out) throws IOException {
              out.writeUTF(result);
          }
      
          public void readFields(DataInput in) throws IOException {
              result = in.readUTF();
          }
      
          @Override
          public String toString() {
              return "Result{" +
                      "result='" + result + '\'' +
                      '}';
          }
      }
      

       

  4. IPCServer
    package com.harry.hadoop_eco.hadoop.examples.common.rpc.writable_;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.ipc.RPC;
    
    import java.io.IOException;
    
    /**
     * Created by harry on 2/2/18.
     */
    public class IPCServer {
        public static final int IPC_PORT = 3331;
        public static final long IPC_VER = 1234L;
    
        public static void main(String[] args) throws IOException {
            HelloImpl hello = new HelloImpl();
    
            RPC.Server server = new RPC.Builder(new Configuration()).setProtocol(IHello.class).setInstance(hello)
                    .setBindAddress("0.0.0.0").setPort(IPC_PORT).setVerbose(true).build();
    
            server.start();
    
            System.out.println("Server ready, press any key to stop");
    
            System.in.read();
    
            server.stop();
        }
    }
    
    1.  
  5. IPCClient
    package com.harry.hadoop_eco.hadoop.examples.common.rpc.writable_;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.ipc.RPC;
    
    import java.io.IOException;
    import java.net.InetSocketAddress;
    
    /**
     * Created by harry on 2/2/18.
     */
    public class IPCClient {
        public static void main(String[] args) throws IOException {
            InetSocketAddress addr = new InetSocketAddress("localhost", IPCServer.IPC_PORT);
            Configuration configuration = new Configuration();
    
            IHello hello = RPC.getProxy(IHello.class,IPCServer.IPC_VER,addr,configuration);
    
            System.out.println(hello.sayHi("world"));
    
            RPC.stopProxy(hello);
        }
    }
    
    1.  
  6.  

 

二、ProtobufRpcEngine

  1. ihello.proto
    1. option java_package = "com.harry.hadoop_eco.hadoop.examples.common.rpc.protobuf_";
      option java_outer_classname = "IHelloProtos";
      option java_generate_equals_and_hash = true;
      
      message EmptyRequestProto{
      }
      
      message EmptyResponseProto{
      }
      
      message EchoRequestProto{
          required string message = 1;
      }
      
      message EchoResponseProto{
          required string message = 1;
      }

       

  2. ihello_rpc_server.proto
    1. option java_package = "com.harry.hadoop_eco.hadoop.examples.common.rpc.protobuf_";
      option java_outer_classname = "IHelloRpcServiceProtos";
      option java_generic_services = true;
      option java_generate_equals_and_hash = true;
      
      import "test.proto";
      
      service IHelloProtobufRpcProto{
          rpc ping(EmptyRequestProto) returns (EmptyResponseProto);
          rpc echo(EchoRequestProto) returns (EchoResponseProto);
      }

       

  3. Protoc 生成
    1. cd 到文件所在目录
    2. protoc -I ./ --java_out=${指向xxx/src/main/java,也可以是任何目录,在copy到对应的目录} ./hello.proto
    3. protoc -i ./ --java_out=${} ./hello_rpc_server.proto
  4. IHello
    1. package com.harry.hadoop_eco.hadoop.examples.common.rpc.protobuf_;
      
      import org.apache.hadoop.ipc.ProtocolInfo;
      
      /**
       * Created by harry on 2/2/18.
       */
      @ProtocolInfo(protocolName = "IHelloProto",protocolVersion = 1)
      public interface IHello extends IHelloRpcServiceProtos.IHelloProtobufRpcProto.BlockingInterface {
      }
      
      

       

  5. HelloImpl
    1. package com.harry.hadoop_eco.hadoop.examples.common.rpc.protobuf_;
      
      import com.google.protobuf.RpcController;
      import com.google.protobuf.ServiceException;
      
      /**
       * Created by harry on 2/2/18.
       */
      public class HelloImpl implements IHello {
          public IHelloProtos.EmptyResponseProto ping(RpcController controller, IHelloProtos.EmptyRequestProto request) throws ServiceException {
              return IHelloProtos.EmptyResponseProto.newBuilder().build();
          }
      
          public IHelloProtos.EchoResponseProto echo(RpcController controller, IHelloProtos.EchoRequestProto request) throws ServiceException {
              return IHelloProtos.EchoResponseProto.newBuilder().setMessage(request.getMessage()).build();
          }
      }
      

       

  6. IPCClient
    1. package com.harry.hadoop_eco.hadoop.examples.common.rpc.protobuf_;
      
      import com.google.protobuf.ServiceException;
      import org.apache.hadoop.conf.Configuration;
      import org.apache.hadoop.ipc.ProtobufRpcEngine;
      import org.apache.hadoop.ipc.RPC;
      
      import java.io.IOException;
      import java.net.InetSocketAddress;
      
      /**
       * Created by harry on 2/2/18.
       */
      public class IPCClient {
          public static void main(String[] args) throws IOException, ServiceException {
              Configuration conf = new Configuration();
              RPC.setProtocolEngine(conf, IHello.class, ProtobufRpcEngine.class);
      
              IHello client = RPC.getProxy(IHello.class,0,new InetSocketAddress("127.0.0.1",3331),conf);
      
              IHelloProtos.EmptyRequestProto emptyRequest = IHelloProtos.EmptyRequestProto.newBuilder().build();
              IHelloProtos.EmptyResponseProto emptyResponseProto = client.ping(null,emptyRequest);
              System.out.println(emptyResponseProto);
      
              IHelloProtos.EchoRequestProto echoRequest = IHelloProtos.EchoRequestProto.newBuilder().setMessage("Hello World, Protobuf").build();
      
              IHelloProtos.EchoResponseProto echoResponse = client.echo(null,echoRequest);
              System.out.println(echoResponse);
          }
      }
      

       

  7. IPCServer
    1. package com.harry.hadoop_eco.hadoop.examples.common.rpc.protobuf_;
      
      import com.google.protobuf.BlockingService;
      import org.apache.hadoop.conf.Configuration;
      import org.apache.hadoop.ipc.ProtobufRpcEngine;
      import org.apache.hadoop.ipc.RPC;
      
      import java.io.IOException;
      
      /**
       * Created by harry on 2/2/18.
       */
      public class IPCServer {
          public static void main(String[] args) throws IOException {
              Configuration conf = new Configuration();
              RPC.setProtocolEngine(conf, IHello.class, ProtobufRpcEngine.class);
      
              HelloImpl hello = new HelloImpl();
              BlockingService service = IHelloRpcServiceProtos.IHelloProtobufRpcProto.newReflectiveBlockingService(hello);
      
              RPC.Server server = new RPC.Builder(conf).setProtocol(IHello.class).setInstance(service).setBindAddress("127.0.0.1").setPort(3331).build();
              server.start();
          }
      }
      

       

转载于:https://my.oschina.net/u/204498/blog/1617424

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值