hadoop RPC

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);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值