Hadoop 中 RPC框架原理、代码示例

0、内容

 

 

1、hadoop中的RPC框架封装思想

2、Hadoop RPC 实现方法

3、服务调用动态转发和负载均衡的实现思考

 

4、协议代码:

 

package com.ares.hadoop.rpc;

public interface HelloProtocol {
    public static final long versionID = 1L;
    
    public String helloMethod(String name);
}

 

 

5、服务端代码:

 

package com.ares.hadoop.rpc;

import java.io.IOException;

import org.apache.hadoop.HadoopIllegalArgumentException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.ipc.RPC;
import org.apache.hadoop.ipc.RPC.Builder;
import org.apache.hadoop.ipc.RPC.Server;
import org.apache.log4j.Logger;

public class HelloServer implements HelloProtocol {
    
    private static final Logger LOGGER = Logger.getLogger(HelloServer.class);

    @Override
    public String helloMethod(String name) {
        // TODO Auto-generated method stub
        LOGGER.debug("name: " + name);
        return "Hello, " + name;
    }
    
    public static void main(String[] args) throws HadoopIllegalArgumentException, IOException {
        Builder builder = new RPC.Builder(new Configuration());
        String bindAddress = "HADOOP-NODE1";
        int port = 8888;
        builder.setBindAddress(bindAddress)
            .setPort(8888)
            .setProtocol(HelloProtocol.class)
            .setInstance(new HelloServer());
        Server server = builder.build();
        LOGGER.debug("Server start to listen on " + port);
        server.start();
    }

}

 


6、客户端代码:

 

package com.ares.hadoop.rpc;

import java.io.IOException;
import java.net.InetSocketAddress;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.ipc.RPC;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class HelloClientTest {
    private static final Logger LOGGER = Logger.getLogger(HelloClientTest.class);
    
    @Before
    public void setUp() {
    }
    @After
    public void tearDown() {
    }
    
    @Test
    public void testHello() throws IOException {
        // TODO Auto-generated method stub 
        LOGGER.debug("RPCClientTest: testHello");
        String bindAddress = "HADOOP-NODE1";
        int port = 8888;
        InetSocketAddress addr = new InetSocketAddress(bindAddress, port);
        HelloProtocol proxy = RPC.getProxy(
                HelloProtocol.class, HelloProtocol.versionID, 
                addr, new Configuration());
        String resp = proxy.helloMethod("JunneYang");
        LOGGER.debug(resp);
        resp = proxy.helloMethod("Ares");
        LOGGER.debug(resp);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值