使用hadoop中的RPC框架实现远程调用

来源:互联网 发布:无创dna数据辨别男女 编辑:程序博客网 时间:2022/10/05 07:37

liunx系统中运行的方法

协议

package cn.itcast.hadoop;public interface UserLoginServiceProtocol {public static final long versionID = 1L;public String login(String username,String password);}

实现

package cn.itcast.hadoop;public class UserLoginServiceImple implements UserLoginServiceProtocol {@Overridepublic String login(String username, String password) {// TODO Auto-generated method stubreturn username + " is logged in!";}}
运行RPC

package cn.itcast.hadoop;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.Builder;import org.apache.hadoop.ipc.RPC.Server;public class RpcRunner {public static void main(String[] args) throws HadoopIllegalArgumentException, IOException {Builder builder = new RPC.Builder(new Configuration());builder.setBindAddress("weekend08").setPort(1234).setInstance(new UserLoginServiceImple()).setProtocol(UserLoginServiceProtocol.class);Server server = builder.build();server.start();}}


window系统的方法

接口协议

package cn.itcast.hadoop;public interface UserLoginServiceProtocol {public static final long versionID = 1L;public String login(String username,String password);}

具体调用

package cn.itcast.hadoop;import java.io.IOException;import java.net.InetSocketAddress;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.ipc.RPC;public class UserLoginController {public static void main(String[] args) throws IOException {UserLoginServiceProtocol userLoginService = RPC.getProxy(UserLoginServiceProtocol.class, 1L, new InetSocketAddress("weekend08",1234), new Configuration());String res = userLoginService.login("angelababy", "123456");System.out.println(res);}}



0 0