import java.io.IOException;
import java.net.InetSocketAddress;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.ipc.RPC;
public class RPCClient {
public static void main(String[] args) throws IOException {
Barty proxy = RPC.getProxy(Barty.class, 10010,
new InetSocketAddress("192.168.8.100", 9527), new Configuration());
String sayHi = proxy.sayHi("tomcat");
System.out.println(sayHi);
}
}
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.Server;
public class RPCServer implements Barty{
public static void main(String[] args) throws HadoopIllegalArgumentException, IOException {
Server server = new RPC.Builder(new Configuration())
.setInstance(new RPCServer())
.setBindAddress("192.168.8.100")
.setPort(9527)
.setProtocol(Barty.class)
.build();
server.start();
}
@Override
public String sayHi(String name) {
// TODO Auto-generated method stub
return "HI~" + name;
}
}
public interface Barty {
public static final long versionID = 10010;
public String sayHi(String name);
}
RPC是不同进程之间的方法调用。在hadoop上创建一个接口,可以在Service和client之间通信。底层的实现也用的是socket通信机制。