标签:hadoop hdfs server mapreduce namenode
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/45922715
2.客户端能够的对象中的方法必须位于对象的接口中。
package com.lyz.hadoop.rpc; import org.apache.hadoop.ipc.VersionedProtocol; /** * 设置代理对象的返回值必须为接口类型 * 此接口用于接收RPC客户端创建代理对象的返回值 * @author liuyazhuang * */ public interface MyBiz extends VersionedProtocol{ /** * 定义的接口 * @param name * @return */ public String hello(String name); /** * 版本号 */ long VERSION = 1001; }
package com.lyz.hadoop.rpc; import java.io.IOException; /** * MyBiz接口的实现类 * @author liuyazhuang * */ public class MyBizImpl implements MyBiz { @Override public long getProtocolVersion(String protocol, long clientVersion) throws IOException { return MyBiz.VERSION; } @Override public String hello(String name){ System.out.println("服务端被调用了"); return "hello "+name; } }
package com.lyz.hadoop.rpc; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.ipc.RPC; import org.apache.hadoop.ipc.RPC.Server; /** * RPC服务端 * @author liuyazhuang * */ public class MyServer { public static final String SERVER_ADDRESS = "localhost"; public static final int SERVER_PORT = 12345; public static void main(String[] args) throws Exception { /** * 构造一个rpc server * @param instance:实例中的方法会被客户端调用 * @param bindAddress:绑定的地址用于监听连接 * @param port:绑定的端口用于监听连接 * @param conf:the Configuration to use */ Server server = RPC.getServer(new MyBizImpl(), SERVER_ADDRESS, SERVER_PORT, new Configuration()); server.start(); } }
package com.lyz.hadoop.rpc; import java.net.InetSocketAddress; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.ipc.RPC; /** * RPC客户端 * @author liuyazhuang * */ public class MyClient { public static void main(String[] args) throws Exception { MyBiz proxy = (MyBiz) RPC.waitForProxy(MyBiz.class, MyBiz.VERSION, new InetSocketAddress(MyServer.SERVER_ADDRESS, MyServer.SERVER_PORT), new Configuration()); String result = proxy.hello("world"); System.out.println("客户端的调用结果为:"+result); RPC.stopProxy(proxy); } }
标签:hadoop hdfs server mapreduce namenode
原文地址:http://blog.csdn.net/l1028386804/article/details/45922715