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); } }