PS: RPC远程调用 Webservice啥的都是远程调用。下面简单介绍其使用过程
Hadoop已经实现了RPC框架,不用我们自己写,不过需要我们注意几点:
1.发布服务端和客户端必须包名相同
1.服务端---发布服务
package cn.itcast.bigdata.hadooprpc.service; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.ipc.RPC;
import org.apache.hadoop.ipc.RPC.Builder;
import org.apache.hadoop.ipc.RPC.Server; import cn.itcast.bigdata.hadooprpc.protocol.ClientNamenodeProtocol;
import cn.itcast.bigdata.hadooprpc.protocol.IUserLoginService; public class PublishServiceUtil { public static void main(String[] args) throws Exception { Builder builder2 = new RPC.Builder(new Configuration());
builder2.setBindAddress("localhost")
.setPort(9999)
.setProtocol(IUserLoginService.class)
.setInstance(new UserLoginServiceImpl()); Server server2 = builder2.build();
server2.start();
}
}
//服务与实现
package cn.itcast.bigdata.hadooprpc.protocol; public interface IUserLoginService { public static final long versionID = 100L;
public String login(String name,String passwd); }
package cn.itcast.bigdata.hadooprpc.service; import cn.itcast.bigdata.hadooprpc.protocol.IUserLoginService; public class UserLoginServiceImpl implements IUserLoginService{ @Override
public String login(String name, String passwd) { return name + "logged in successfully...";
}
}
2.客户端---查找服务
package cn.itcast.bigdata.hadooprpc.client; import java.io.IOException;
import java.net.InetSocketAddress; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.ipc.RPC; import cn.itcast.bigdata.hadooprpc.protocol.IUserLoginService; public class UserLoginActionStudy { public static void main(String[] args) throws IOException {
IUserLoginService userLoginService = RPC.getProxy(IUserLoginService.class, 100L, new InetSocketAddress("localhost", 8888),new Configuration());
String login = userLoginService.login("angelababy", "1314520");
System.out.println(login);
} }