zoukankan      html  css  js  c++  java
  • 【Hadoop】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);
        }
    }
  • 相关阅读:
    可伸缩的菜单
    Microsoft.AspNet.Identity: UserID用整型数据表示, 而不是GUID
    需要了解的技术
    微信公众号开发一些链接
    SQL Server Connection Strings for ASP.NET Web Applications https://msdn.microsoft.com/en-us/library/jj653752.aspx
    ASP.NET MVC 4 (三) 过滤器
    quick Cocos 2dx 学习网站
    获取SQL server 中的表和说明
    在 fragment 里面调用 findViewById
    gravity 和 layout_gravity
  • 原文地址:https://www.cnblogs.com/junneyang/p/5841135.html
Copyright © 2011-2022 走看看