Server 实现:
import java.net.InetSocketAddress;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.ipc.RPC;
import org.apache.hadoop.ipc.Server;
import org.apache.hadoop.net.NetUtils;
public class rpctestServer {
public static final int DEFAULT_PORT = 8020;
/** RPC server */
private Server server;
/** RPC server address */
private InetSocketAddress serverAddress = null;
public void TestServer(){
String address = "127.0.0.1";
InetSocketAddress socAddr = NetUtils.createSocketAddr(address, DEFAULT_PORT);
// create rpc server
TestProtocolImpl testProtocolImpl1 = new TestProtocolImpl();
int handlerCount = 2;
Configuration conf = new Configuration();
try{
server = RPC.getServer(testProtocolImpl1, socAddr.getHostName(), socAddr.getPort(), handlerCount, false, conf);
this.serverAddress = this.server.getListenerAddress();
this.server.start(); //start RPC server
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args){
new rpctestServer().TestServer();
while(true){
}
// System.out.println("server close!");
}
}
Client 实现:
import java.io.IOException;
import java.net.InetSocketAddress;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.ipc.RPC;
import org.apache.hadoop.net.NetUtils;
public class rpctestClient {
public static final int DEFAULT_PORT = 8020;
public TestProtocol namenode = null;
public void TestClient(){
// connect to name node
String address = "127.0.0.1";
InetSocketAddress socAddr = NetUtils.createSocketAddr(address, DEFAULT_PORT);
Configuration conf = new Configuration();
try {
namenode = (TestProtocol)RPC.waitForProxy(TestProtocol.class, TestProtocol.versionID,socAddr,conf);
namenode.sayHello();
System.out.println(namenode.getName());
namenode.sendMessage("I like play basketball!!! ");
RPC.stopProxy(namenode);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args){
new rpctestClient().TestClient();
}
}
TestProtocol 接口:
import org.apache.hadoop.ipc.VersionedProtocol;
public interface TestProtocol extends VersionedProtocol {
public static final long versionID = 19L;
public void sayHello();
public String getName();
public void sendMessage(String message);
}
TestProtocol 接口实现:
import java.io.IOException;
public class TestProtocolImpl implements TestProtocol{
String serverName = "success Test";
public long getProtocolVersion(String protocol, long clientVersion) throws IOException {
if (protocol.equals(TestProtocol.class.getName())) {
return TestProtocol.versionID;
}else {
throw new IOException("Unknown protocol to name node: " + protocol);
}
}
public void sayHello(){
System.out.println("hello server!!");
}
public String getName(){
System.out.println("client get server Name");
return serverName;
}
public void sendMessage(String message){
System.out.println("get message: "+message);
}
}
import java.net.InetSocketAddress;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.ipc.RPC;
import org.apache.hadoop.ipc.Server;
import org.apache.hadoop.net.NetUtils;
public class rpctestServer {
public static final int DEFAULT_PORT = 8020;
/** RPC server */
private Server server;
/** RPC server address */
private InetSocketAddress serverAddress = null;
public void TestServer(){
String address = "127.0.0.1";
InetSocketAddress socAddr = NetUtils.createSocketAddr(address, DEFAULT_PORT);
// create rpc server
TestProtocolImpl testProtocolImpl1 = new TestProtocolImpl();
int handlerCount = 2;
Configuration conf = new Configuration();
try{
server = RPC.getServer(testProtocolImpl1, socAddr.getHostName(), socAddr.getPort(), handlerCount, false, conf);
this.serverAddress = this.server.getListenerAddress();
this.server.start(); //start RPC server
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args){
new rpctestServer().TestServer();
while(true){
}
// System.out.println("server close!");
}
}
Client 实现:
import java.io.IOException;
import java.net.InetSocketAddress;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.ipc.RPC;
import org.apache.hadoop.net.NetUtils;
public class rpctestClient {
public static final int DEFAULT_PORT = 8020;
public TestProtocol namenode = null;
public void TestClient(){
// connect to name node
String address = "127.0.0.1";
InetSocketAddress socAddr = NetUtils.createSocketAddr(address, DEFAULT_PORT);
Configuration conf = new Configuration();
try {
namenode = (TestProtocol)RPC.waitForProxy(TestProtocol.class, TestProtocol.versionID,socAddr,conf);
namenode.sayHello();
System.out.println(namenode.getName());
namenode.sendMessage("I like play basketball!!! ");
RPC.stopProxy(namenode);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args){
new rpctestClient().TestClient();
}
}
TestProtocol 接口:
import org.apache.hadoop.ipc.VersionedProtocol;
public interface TestProtocol extends VersionedProtocol {
public static final long versionID = 19L;
public void sayHello();
public String getName();
public void sendMessage(String message);
}
TestProtocol 接口实现:
import java.io.IOException;
public class TestProtocolImpl implements TestProtocol{
String serverName = "success Test";
public long getProtocolVersion(String protocol, long clientVersion) throws IOException {
if (protocol.equals(TestProtocol.class.getName())) {
return TestProtocol.versionID;
}else {
throw new IOException("Unknown protocol to name node: " + protocol);
}
}
public void sayHello(){
System.out.println("hello server!!");
}
public String getName(){
System.out.println("client get server Name");
return serverName;
}
public void sendMessage(String message){
System.out.println("get message: "+message);
}
}