package com.liang;
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 RPCRunner {
public static void main(String[] args) throws HadoopIllegalArgumentException, IOException {
RPC.Builder builder = new RPC.Builder(new Configuration());
builder.setBindAddress("localhost")//
.setPort(1234)//
.setInstance(new UserServiceImpl())//
.setProtocol(UserServiceProtocol.class);
Server server = builder.build();
server.start();
}
}
package com.liang;
public class UserServiceImpl implements UserServiceProtocol {
@Override
public boolean exists(String userName, String pwd) {
System.out.println("some one invoked....");
return false;
}
}
package com.liang;
public interface UserServiceProtocol {
public static final long versionID = 1l;
boolean exists(String userName, String pwd);
}
以上为server端,下面为client端
package com.liang;
public interface UserServiceProtocol {
public static final long versionID = 1l;
boolean exists(String userName, String pwd);
}
package com.liang;
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 Exception {
UserServiceProtocol proxy = RPC.getProxy(UserServiceProtocol.class, 1l, new InetSocketAddress("localhost", 1234), new Configuration());
proxy.exists("123", "455");
}
}