Hadoop2源码分析-RPC机制初识
1.概述
上一篇博客,讲述Hadoop V2的序列化机制,这为我们学习Hadoop V2的RPC机制奠定了基础。RPC的内容涵盖的信息有点多,包含Hadoop的序列化机制,RPC,代理,NIO等。若对Hadoop序列化不了解的同学,可以参考《Hadoop2源码分析-序列化篇》。今天这篇博客为大家介绍的内容目录如下:
- RPC概述
- 第三方RPC
- Hadoop V2的RPC简述
那么,下面开始今天的学习之路。
2.RPC概述
首先,我们要弄明白,什么是RPC?RPC能用来做什么?
2.1什么是RPC
RPC的全程是Remote Procedure Call,中文释为远程过程调用。也就是说,调用的过程代码(业务服务代码)并不在调用者本地运行,而是要实现调用着和被调用着之间的连接通信,有同学可能已经发现,这个和C/S模式很像。没错,RPC的基础通信模式是基于C/S进程间相互通信的模式来实现的,它对Client端提供远程接口服务,其RPC原理图如下所示:
2.2RPC的功能
我们都知道,在过去的编程概念中,过程是由开发人员在本地编译完成的,并且只能局限在本地运行的某一段代码,即主程序和过程程序是一种本地调用关系。因此,这种结构在如今网络飞速发展的情况下已无法适应实际的业务需求。而且,传统过程调用模式无法充分利用网络上其他主机的资源,如CPU,内存等,也无法提高代码在Bean之间的共享,使得资源浪费较大。
而RPC的出现,正好有效的解决了传统过程中存在的这些不足。通过RPC,我们可以充分利用非共享内存的机器,可以简便的将应用分布在多台机器上,类似集群分布。这样方便的实现过程代码共享,提高系统资源的利用率。减少单个集群的压力,实现负载均衡。
3.第三方RPC
在学习Hadoop V2的RPC机制之前,我们先来熟悉第三方的RPC机制是如何工作的,下面我以Thrift框架为例子。
Thrift是一个软件框架,用来进行可扩展且跨语言的服务开发协议。它拥有强大的代码生成引擎,支持C++,Java,Python,PHP,Ruby等编程语言。Thrift允许定义一个简单的定义文件(以.thirft结尾),文件中包含数据类型和服务接口。用以作为输入文件,编译器生成代码用来方便的生成RPC客户端和服务端通信的编程语言。具体Thrift安装过程请参考《Mac OS X 下搭建thrift环境》。
3.1Thrift原理图
下面给出Thrift的原理图,如下所示:
下面为大家解释一下上面的原理图,首先,我们编译完thrift定义文件后(这里我使用的是Java语言),会生成对应的Java类文件,该类的Iface接口定义了我们所规范的接口函数。在服务端,实现Iface接口,编写对应函数下的业务逻辑,启动服务。客户端同样需要生成的Java类文件,以供Client端调用相应的接口函数,监听服务端的IP和PORT来获取连接对象。
3.2代码示例
- Server端代码:
package cn.rpc.main; import org.apache.thrift.TProcessorFactory;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.server.THsHaServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import cn.rpc.service.StatQueryService;
import cn.rpc.service.impl.StatQueryServiceImpl; /**
* @Date Mar 23, 2015
*
* @Author dengjie
*/
public class StatsServer { private static Logger logger = LoggerFactory.getLogger(StatsServer.class); private final int PORT = 9090; @SuppressWarnings({ "rawtypes", "unchecked" })
private void start() {
try {
TNonblockingServerSocket socket = new TNonblockingServerSocket(PORT);
final StatQueryService.Processor processor = new StatQueryService.Processor(new StatQueryServiceImpl());
THsHaServer.Args arg = new THsHaServer.Args(socket);
/*
* Binary coded format efficient, intensive data transmission, The
* use of non blocking mode of transmission, according to the size
* of the block, similar to the Java of NIO
*/
arg.protocolFactory(new TCompactProtocol.Factory());
arg.transportFactory(new TFramedTransport.Factory());
arg.processorFactory(new TProcessorFactory(processor));
TServer server = new THsHaServer(arg);
server.serve();
} catch (Exception ex) {
ex.printStackTrace();
}
} public static void main(String[] args) {
try {
logger.info("start thrift server...");
StatsServer stats = new StatsServer();
stats.start();
} catch (Exception ex) {
ex.printStackTrace();
logger.error(String.format("run thrift server has error,msg is %s", ex.getMessage()));
}
} }
- Client端代码:
package cn.rpc.test; import java.util.Map; import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport; import cn.rpc.service.StatQueryService; /**
* @Date Mar 23, 2015
*
* @Author dengjie
*
* @Note Test thrift client
*/
public class StatsClient { public static final String ADDRESS = "127.0.0.1";
public static final int PORT = 9090;
public static final int TIMEOUT = 30000; public static void main(String[] args) {
if (args.length < 4) {
System.out.println("args length must >= 4,current length is " + args.length);
System.out.println("<info>****************</info>");
System.out.println("ADDRESS,beginDate,endDate,kpiCode,...");
System.out.println("<info>****************</info>");
return;
}
TTransport transport = new TFramedTransport(new TSocket(args[0], PORT, TIMEOUT));
TProtocol protocol = new TCompactProtocol(transport);
StatQueryService.Client client = new StatQueryService.Client(protocol);
String beginDate = args[1]; // "20150308"
String endDate = args[2]; // "20150312"
String kpiCode = args[3]; // "login_times"
String userName = "";
int areaId = 0;
String type = "";
String fashion = ""; try {
transport.open();
Map<String, String> map = client.queryConditionDayKPI(beginDate, endDate, kpiCode, userName, areaId, type,
fashion);
System.out.println(map.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
transport.close();
}
} }
- StatQueryService类:
这个类的代码量太大,暂不贴出。需要的同学请到以下地址下载。
下载地址:git@gitlab.com:dengjie/Resource.git
- StatQueryServiceImpl类:
下面实现其中一个函数的内容,代码如下所示:
package cn.rpc.service.impl; import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.apache.thrift.TException; import cn.rpc.conf.ConfigureAPI;
import cn.rpc.dao.KpiDao;
import cn.rpc.domain.ReportParam;
import cn.rpc.domain.ReportResult;
import cn.rpc.service.StatQueryService;
import cn.rpc.util.MapperFactory; /**
* @Date Mar 23, 2015
*
* @Author dengjie
*/
public class StatQueryServiceImpl implements StatQueryService.Iface { public Map<String, String> queryDayKPI(String beginDate, String endDate, String kpiCode) throws TException {
return null;
} public Map<String, String> queryConditionDayKPI(String beginDate, String endDate, String kpiCode, String userName,
int areaId, String type, String fashion) throws TException {
Map<String, String> res = new HashMap<String, String>();
ReportParam param = new ReportParam();
param.setBeginDate(beginDate + "");
param.setEndDate(endDate + "");
param.setKpiCode(kpiCode);
param.setUserName(userName == "" ? null : userName);
param.setDistrictId(areaId < 0 ? 0 : areaId);
param.setProductStyle(fashion == "" ? null : fashion);
param.setCustomerProperty(type == "" ? null : type);
List<ReportResult> chart = ((KpiDao) MapperFactory.createMapper(KpiDao.class)).getChartAmount(param);
Map<String, Integer> title = ((KpiDao) MapperFactory.createMapper(KpiDao.class)).getTitleAmount(param);
List<Map<String, Integer>> tableAmount = ((KpiDao) MapperFactory.createMapper(KpiDao.class))
.getTableAmount(param);
String avgTime = kpiCode.split("_")[0];
param.setKpiCode(avgTime + "_avg_time");
List<Map<String, Integer>> tableAvgTime = ((KpiDao) MapperFactory.createMapper(KpiDao.class))
.getTableAmount(param);
res.put(ConfigureAPI.RESMAPKEY.CHART, chart.toString());
res.put(ConfigureAPI.RESMAPKEY.TITLE, title.toString());
res.put(ConfigureAPI.RESMAPKEY.TABLEAMOUNT, tableAmount.toString());
res.put(ConfigureAPI.RESMAPKEY.TABLEAVG, tableAvgTime.toString());
return res;
} public Map<String, String> queryDetail(String beginDate, String endDate, String userName) throws TException {
// TODO Auto-generated method stub
return null;
} }
4.Hadoop V2的RPC简述
Hadoop V2中的RPC采用的是自己独立开发的协议,其核心内容包含服务端,客户端,交互协议。源码内容都在hadoop-common-project项目的org.apache.hadoop.ipc包下面。
- VersionedProtocol类:
package org.apache.hadoop.ipc; import java.io.IOException; /**
* Superclass of all protocols that use Hadoop RPC.
* Subclasses of this interface are also supposed to have
* a static final long versionID field.
*/
public interface VersionedProtocol { /**
* Return protocol version corresponding to protocol interface.
* @param protocol The classname of the protocol interface
* @param clientVersion The version of the protocol that the client speaks
* @return the version that the server will speak
* @throws IOException if any IO error occurs
*/
public long getProtocolVersion(String protocol,
long clientVersion) throws IOException; /**
* Return protocol version corresponding to protocol interface.
* @param protocol The classname of the protocol interface
* @param clientVersion The version of the protocol that the client speaks
* @param clientMethodsHash the hashcode of client protocol methods
* @return the server protocol signature containing its version and
* a list of its supported methods
* @see ProtocolSignature#getProtocolSignature(VersionedProtocol, String,
* long, int) for a default implementation
*/
public ProtocolSignature getProtocolSignature(String protocol,
long clientVersion,
int clientMethodsHash) throws IOException;
}
该类中的两个方法一个是作为版本,另一个作为签名用。
- RPC下的Server类:
/** An RPC Server. */
public abstract static class Server extends org.apache.hadoop.ipc.Server {
boolean verbose;
static String classNameBase(String className) {
String[] names = className.split("\\.", -1);
if (names == null || names.length == 0) {
return className;
}
return names[names.length-1];
}
对外提供服务,处理Client端的请求,并返回处理结果。
至于Client端,监听Server端的IP和PORT,封装请求数据,并接受Response。
5.总结
这篇博客赘述了RPC的相关内容,让大家先熟悉一下RPC的相关机制和流程,并简述了Hadoop V2的RPC机制,关于Hadoop V2的RPC详细内容会在下一篇博客中给大家分享。这里只是让大家先对Hadoop V2的RPC机制有个初步的认识。
6.结束语
这篇博客就和大家分享到这里,如果大家在研究学习的过程当中有什么问题,可以加群进行讨论或发送邮件给我,我会尽我所能为您解答,与君共勉!
Hadoop2源码分析-RPC机制初识的更多相关文章
- Hadoop2源码分析-RPC探索实战
1.概述 在<Hadoop2源码分析-RPC机制初识>博客中,我们对RPC机制有了初步的认识和了解,下面我们对Hadoop V2的RPC机制做进一步探索,在研究Hadoop V2的RPC机 ...
- Hadoop2源码分析-YARN RPC 示例介绍
1.概述 之前在<Hadoop2源码分析-RPC探索实战>一文当中介绍了Hadoop的RPC机制,今天给大家分享关于YARN的RPC的机制.下面是今天的分享目录: YARN的RPC介绍 Y ...
- Hadoop2源码分析-HDFS核心模块分析
1.概述 这篇博客接着<Hadoop2源码分析-RPC机制初识>来讲述,前面我们对MapReduce.序列化.RPC进行了分析和探索,对Hadoop V2的这些模块都有了大致的了解,通过对 ...
- Hadoop2源码分析-YARN 的服务库和事件库
1.概述 在<Hadoop2源码分析-YARN RPC 示例介绍>一文当中,给大家介绍了YARN 的 RPC 机制,以及相关代码的演示,今天我们继续去学习 YARN 的服务库和事件库,分享 ...
- Hadoop2源码分析-MapReduce篇
1.概述 前面我们已经对Hadoop有了一个初步认识,接下来我们开始学习Hadoop的一些核心的功能,其中包含mapreduce,fs,hdfs,ipc,io,yarn,今天为大家分享的是mapred ...
- springMVC源码分析--异常处理机制HandlerExceptionResolver执行原理(二)
上一篇博客springMVC源码分析--异常处理机制HandlerExceptionResolver简单示例(一)中我们简单地实现了一个异常处理实例,接下来我们要介绍一下HandlerExceptio ...
- MyBatis 源码分析 - 插件机制
1.简介 一般情况下,开源框架都会提供插件或其他形式的拓展点,供开发者自行拓展.这样的好处是显而易见的,一是增加了框架的灵活性.二是开发者可以结合实际需求,对框架进行拓展,使其能够更好的工作.以 My ...
- Hadoop2源码分析-准备篇
1.概述 我们已经能够搭建一个高可用的Hadoop平台了,也熟悉并掌握了一个项目在Hadoop平台下的开发流程,基于Hadoop的一些套件我们也能够使用,并且能利用这些套件进行一些任务的开发.在Had ...
- 鸿蒙内核源码分析(调度机制篇) | 任务是如何被调度执行的 | 百篇博客分析OpenHarmony源码 | v7.07
百篇博客系列篇.本篇为: v07.xx 鸿蒙内核源码分析(调度机制篇) | 任务是如何被调度执行的 | 51.c.h .o 任务管理相关篇为: v03.xx 鸿蒙内核源码分析(时钟任务篇) | 触发调 ...
随机推荐
- HP 集群软件 - 不能接收节点的设备查询信息:软件引起的连接失败
问题 # cmcheckconf -v -C /etc/cmcluster/cmclconfig.ascii Begin cluster verification... Checking clust ...
- VHDL基础2
Signals & Variables VHDL 提供了 signal 和 variable 两种对象来处理非静态数据:提供了 constant 和 generic 来处理静态数据. cons ...
- Unicode 字符
Unicode是计算机可以支持这个星球上多种语言的秘密武器.在Unicode之前,用的都是ASCII. ASCII码非常简单,每个英文都是7位二进制的方式存贮在计算机内,其范围是32~126.当用户在 ...
- kepware http接口 javascript开发
读取某变量的值(jquery var settings = { "async": true, "crossDomain": true, "url&qu ...
- Hdu2841 Visible Trees 2017-06-27 22:13 24人阅读 评论(0) 收藏
Visible Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- C语言中:static与extern对变量和函数的作用
1.两者对全局变量 static对全局变量,表示定义一个内部变量 extern对全局变量,表示声明一个外部变量 说明: 1.内部变量:定义的变量只能在本文件中访问,不能被其他文件访问. 2.不同文件中 ...
- 省赛在即!最大流问题回顾学习!!DInic
Dinic是很好的算法,但是我还是从ek算法复习起步 面对最大流问题,印象最深的就是反向边的思想,他给我们提供了反悔的机会,其实现在放到实际上来想,可以相当于两边的水都流了这条边,只是方向不一样,放到 ...
- Python自动化开发 - 进程、线程(一)
本节内容 1.操作系统发展史 2.进程和线程 3.Python threading 模块 一.操系统发展史 手工操作(无操作系统) 1946年第一台计算机诞生--20世纪50年代中期,还未出现操作系统 ...
- Java设计模式详尽资料
设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...
- dialog里屏蔽ESC和回车
重载PreTranslateMessage,在return之前加一句判断,只要是按下ESC和回车的消息,就直接置之不理即可,代码如下: if( pMsg->message == WM_KEYDO ...