Hadoop里的rpc框架可以单独拿出来使用。jar包全在hadoop-common工程里。
导入hadoop-common工程里(hadoop-2.7.3为例):
hadoop-common-2.7.3.jar
\hadoop-2.7.3\share\hadoop\common\lib下的全部jar包
实例
rpc.client 客户端
rpc.protocol (数据传递的)协议层
rpc.service 服务端
package rpc.client;import java.net.InetSocketAddress;import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.ipc.RPC;import rpc.protocol.IClientNamenodeProtocol;public class MyHdfsClient {public static void main(String[] args) throws Exception {IClientNamenodeProtocol proxy = RPC.getProxy(IClientNamenodeProtocol.class, 1L, new InetSocketAddress("localhost", 8888), new Configuration());String metaData = proxy.getMetaData("/hellohellohelloxxxxxxx.xxx");System.out.println("获取到结果:"+metaData);}}
package rpc.protocol;/*** 通信接口* * @author zengmiaogen**/
public interface IClientNamenodeProtocol {//hadoop-rpc框架要求有的变量versionID,没有报错public final long versionID=1L;public String getMetaData(String path);
}
package rpc.service;import rpc.protocol.IClientNamenodeProtocol;public class MyNameNode implements IClientNamenodeProtocol{/*** 模拟NameNode的业务方法之一,查询元数据* @param path* @return*/@Overridepublic String getMetaData(String path){//假的,模拟的System.out.println("传入的路径:"+path);return path+": 副本数量3-{BLK-1,BLK-2}-NameNode.......";}}
package rpc.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 rpc.protocol.IClientNamenodeProtocol;/*** 启动服务器* @author zengmiaogen**/
public class PublishServiceUtil {public static void main(String[] args) throws Exception {Builder builder = new RPC.Builder(new Configuration());builder.setBindAddress("localhost").setPort(8888).setProtocol(IClientNamenodeProtocol.class).setInstance(new MyNameNode());Server server = builder.build();server.start();}}
步骤:
1、运行PublishServiceUtil.java 启动服务端
2、运行MyHdfsClient.java 访问服务端
结果:
客户端
log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
获取到结果:/hellohellohelloxxxxxxx.xxx: 副本数量3-{BLK-1,BLK-2}-NameNode.......
服务端
log4j:WARN No appenders could be found for logger (org.apache.hadoop.ipc.Server).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
传入的路径:/hellohellohelloxxxxxxx.xxx
-------------
更多的Java,Angular,Android,大数据,J2EE,Python,数据库,Linux,Java架构师,:
http://www.cnblogs.com/zengmiaogen/p/7083694.html