0, content,

 

 

1. Encapsulation idea of RPC framework in Hadoop

2. Implementation method of Hadoop RPC

3. Thinking about dynamic forwarding and load balancing of service invocation

 

4. Protocol Code:

 

package com.ares.hadoop.rpc;

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

 

 

5. Server code:

 

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(); }}Copy the code

 

6. Client code:

 

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); }}Copy the code