Yarn核心——RPC(2) Hadoop RPC

本文介绍了Hadoop RPC的基本实现过程,包括定义协议、实现协议、定义服务端和客户端四个步骤,并通过具体代码示例展示了如何使用Hadoop RPC进行简单的加法运算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Hadoop RPC 四步法

(1) 定义协议
(2) 实现协议
(3) 定义服务端
(4) 定义客户端

(1) 定义协议

public interface MethodProtocol extends VersionedProtocol{
    public static final long versionID=1L;
    int calculate(int v1,int v2) throws IOException;
}

(2) 实现协议

public class MethodProtocolImpl implements MethodProtocol
{
    public int calculate(int v1, int v2) throws IOException {
        return  v1+v2;
    }


    public long getProtocolVersion(String protocol, long clientVersion) throws IOException {
        return  MethodProtocol.versionID;
    }

    public ProtocolSignature getProtocolSignature(String protocol, long clientVersion, int clientMethodsHash) throws IOException {
        return new ProtocolSignature(MethodProtocol.versionID,null);
    }
}

(3) 实现服务端

public class MethodRpcServer {
    public static void main(String[] args)  throws Exception{
        Configuration conf = new Configuration();
        RPC.Builder builder =new RPC.Builder(conf);
        builder.setBindAddress("localhost")
                .setPort(8888).setProtocol(MethodProtocol.class)
                .setInstance(new MethodProtocolImpl());
        RPC.Server server=builder.build();
        server.start();
    }
}

(4) 实现客户端

public class MethodRpcClient {
    public static void main(String[] args) throws Exception{
        MethodProtocol proxy = RPC.getProxy(MethodProtocol.class,MethodProtocol.versionID,
                new InetSocketAddress("localhost",8888),new Configuration());
        int result = proxy.calculate(1,2);
        System.out.println(result);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值