hadoop RPC(一)

在了解hadoop的rpc框架之前,先尝试怎么使用它。
(请务必注意:这里使用的是hadoop2.6版本)
第一步:定义RPC协议并实现它

//定义我们的接口  实现VersionedProtocol接口
//add和sub是两个自定义方法,与当前主题无关,我们根据自己的需要去编码即
//可
//注意:这里需要使用注解 表明版本号,服务端版本号必须和客户端版本号一致
//才可以实现通信
@ProtocolInfo(protocolName = "",protocolVersion = 1L )
public interface MyProtocol extends VersionedProtocol {
     public static final long versionID = 1L;
     public IntWritable add(IntWritable arg1, IntWritable arg2);
     public IntWritable sub(IntWritable arg1, IntWritable arg2);
}
//定义一个实现类
public class MyServerImp implements MyProtocol 

第二步:服务端启动


 Server server = new RPC.Builder(new Configuration())
                        .setProtocol(MyProtocol .class)
                        .setBindAddress("127.0.0.1")
                        .setPort(IPC_PORT)
                        .setInstance(new MyServerImp())
                        .build();
  server.start();

第三步:客户端

InetSocketAddress addr = new InetSocketAddress("127.0.0.1", 
                    IPC_PORT);
MyProtocol service = (MyProtocol)RPC.getProxy(
                        MyProtocol.class,
                        MyProtocol.versionID, 
                        addr,
                        new Configuration());

从上面的例子可以看到

  1. RPC.Builder封装各种参数,并提供返回server对象的方法build
  2. org.apache.hadoop.ipc.VersionedProtocol 接口是RPC框架的顶层接口
  3. RPC.getProxy返回代理对象实例
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值