public class RPCServer implements Bizable{
//服务端实现sayHi接口,为客户端提供接口
public String sayHi(String name)
{
return "Hi~" + name;
}
public static void main(String[] args) throws HadoopIllegalArgumentException, IOException {
//启动服务器
Configuration conf = new Configuration();
Server server = new RPC.Builder(conf).setProtocol(Bizable.class).setInstance(new RPCServer()).setBindAddress("192.168.8.100").setPort(9527).build();
server.start();
}
2.接口代码如下:
public interface Bizable {
public static final long versionID = 10010;
public String sayHi(String name);
}
3.客户端,需要得到服务端代理对象,通过RPC.getProxy可以得到服务器代码对象
public class RPCClient {
public static void main(String[] args) throws IOException {
Bizable proxyBizable = RPC.getProxy(Bizable.class, 10010, new InetSocketAddress("192.168.8.100", 9527), new Configuration());
String result = proxyBizable.sayHi("Tomcat");
System.out.println(result);
RPC.stopProxy(proxyBizable);
}
}
4.我们先启动服务端,再启动客户端,就可以看到Hi~ Tomcat