Thrift简单实践
0、什么是RPC
RPC(Remote Procedure Call - 远程过程调用),是通过网络从远程计算机上请求服务,而不需要了解底层网路技术的细节。简单点说,就是像调用本地服务(方法)一样调用远端的服务(方法)。
RPC与REST的区别
RPC是一种协议,REST是一种架构风格。
RPC以行为为中心,REST以资源为中心。当加入新功能时,RPC需要增加更多的行为,并进行调用。REST的话,调用方法基本不变。
RPC可以不基于HTTP协议,因此在后端语言调用中,可以采用RPC获得更好的性能。REST一般是基于HTTP协议。
1、RPC框架Thrift(0.9.3)
Thrift是一种开源的高效的、支持多种编程语言的远程服务调用框架。支持C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, OCaml 和 Delphi等诸多语言,能够很好的进行跨语言调用。
Thrift官网: https://thrift.apache.org/
2、Thrift的简单实践(Windows)
2.1 安装Thrift
在http://www.apache.org/dyn/closer.cgi?path=/thrift/0.9.3/thrift-0.9.3.exe这里可以下载Thrift的windows系统编译版本。
该文件是一个绿色文件,可以放置在目录中,进入该目录的cmd,就可以直接使用thrift。输入thrift -version
可以查看当前Thrift的版本。
至此,Thrift已完成安装
2.2 编写接口定义文件
在安装好Thrift之后,需要我们编写接口定义文件,用来约定服务和thrift类型的接口定义。
Thrift主要有一下这些类型:
- bool --简单类型,true or false
- byte --简单类型,单字符
- i16 --简单类型,16位整数
- i32 --简单类型,32位整数
- i64 --简单类型,64位整数
- double --简单类型,双精度浮点型
- string --简单类型,utf-8编码字符串
- binary --二进制,未编码的字符序列
- struct --结构体,对应结构体、类等对象类型
- list --list容器
- set --set容器
- map --map容器
- enum --枚举类型
接下来,利用这些类型,编写一个简单的.thrift接口定义文件。
/* 1.thrift file content */
namespace js ThriftTest
namespace csharp ThriftTest
service ThriftTest{
double plus(1:double num1, 2:double num2)
}
更复杂的案例: https://git-wip-us.apache.org/repos/asf?p=thrift.git;a=blob_plain;f=test/ThriftTest.thrift;hb=HEAD
在利用thrift --gen js:node --gen js 1.thrift
来生成好客户端代码和服务端代码。可以跟多个--gen 参数,来实现一次性生成多个语言的代码。
2.3 利用Thrift实现nodeJS服务端
var thrift = require('thrift');
var ThriftTest = require("./gen-nodejs/ThriftTest");
var ttypes = require("./gen-nodejs/1_types");
var nodeServer = thrift.createServer(ThriftTest, {
//完成具体的事情
plus: function(n1, n2, callback){
console.log(`server request, n1 = ${n1}, n2 = ${n2}.`);
callback(null, n1 + n2);
}
});
//处理错误,假设不处理,如果客户端强制断开连接,会导致后端程序挂掉
nodeServer.on('error', function(err){
console.log(err);
});
nodeServer.listen(7410);
console.log('node server started... port: 7410');
//如果client的浏览器,通信采用http的时候,需要创建http server
var httpServer = thrift.createWebServer({
cors: {'*': true}, //配置跨域访问
services: {
'/thrift': { //配置路径映射
transport: thrift.TBufferedTransport,
protocol: thrift.TJSONProtocol,
processor: ThriftTest,
handler: { //具体的处理对象
plus: function(n1, n2, callback) {
console.log(`http request, n1 = ${n1}, n2 = ${n2}.`);
callback(null, n1 + n2);
}
}
}
}
});
httpServer.on('error', function(err) {
console.log(err);
});
httpServer.listen(7411);
console.log('http server started... port: 7411');
2.4 Node Client 调用
var thrift = require('thrift');
var ThriftTest = require('./gen-nodejs/ThriftTest');
var ttypes = require('./gen-nodejs/1_types');
transport = thrift.TBufferedTransport()
protocol = thrift.TBinaryProtocol()
var connection = thrift.createConnection("localhost", 7410, {
transport : transport,
protocol : protocol
});
connection.on('error', function(err) {
console.log(false, err);
});
var client = thrift.createClient(ThriftTest, connection);
var sum = client.plus(1, 1, function(err, result){
//connection.end(); //如果不关闭连接,那么强制断开连接,将会导致后端出现error
if(err){
console.log(err);
return;
}
console.log(result);
});
2.5、Http Client 调用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Thrift Test Client</title>
</head>
<body>
<input type="text" id="num1"> + <input type="text" id="num2"> <button onclick="call()">=</button> <span id="result">?</span>
<!-- <script src="jquery.js"></script> -->
<script src="thrift.js"></script>
<script src="gen-js/1_types.js"></script>
<script src="gen-js/ThriftTest.js"></script>
<script>
var transport = new Thrift.Transport("http://127.0.0.1:7411/thrift");
var protocol = new Thrift.TJSONProtocol(transport);
var client = new ThriftTest.ThriftTestClient(protocol);
var el_result = document.getElementById('result');
function call(){
var num1 = +document.getElementById('num1').value,
num2 = +document.getElementById('num2').value;
client.plus(num1, num2, function(result) {
el_result.innerText = result;
alert('调用成功!');
});
}
</script>
<script>
</script>
</body>
</html>
注意:如果在thrift生成代码时,使用了--gen js:jquery参数,那么在浏览器调用的时候,就必须依赖jquery。
3、demo地址
https://github.com/hstarorg/HstarDemoProject/tree/master/thrift_demo
*:first-child {
margin-top: 0 !important;
}
body>*:last-child {
margin-bottom: 0 !important;
}
/* BLOCKS
=============================================================================*/
p, blockquote, ul, ol, dl, table, pre {
margin: 15px 0;
}
/* HEADERS
=============================================================================*/
h1, h2, h3, h4, h5, h6 {
margin: 20px 0 10px;
padding: 0;
font-weight: bold;
-webkit-font-smoothing: antialiased;
}
h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code {
font-size: inherit;
}
h1 {
font-size: 28px;
color: #000;
}
h2 {
font-size: 24px;
border-bottom: 1px solid #ccc;
color: #000;
}
h3 {
font-size: 18px;
}
h4 {
font-size: 16px;
}
h5 {
font-size: 14px;
}
h6 {
color: #777;
font-size: 14px;
}
body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child {
margin-top: 0;
padding-top: 0;
}
a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
margin-top: 0;
padding-top: 0;
}
h1+p, h2+p, h3+p, h4+p, h5+p, h6+p {
margin-top: 10px;
}
/* LINKS
=============================================================================*/
a {
color: #4183C4;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
/* LISTS
=============================================================================*/
ul, ol {
padding-left: 30px;
}
ul li > :first-child,
ol li > :first-child,
ul li ul:first-of-type,
ol li ol:first-of-type,
ul li ol:first-of-type,
ol li ul:first-of-type {
margin-top: 0px;
}
ul ul, ul ol, ol ol, ol ul {
margin-bottom: 0;
}
dl {
padding: 0;
}
dl dt {
font-size: 14px;
font-weight: bold;
font-style: italic;
padding: 0;
margin: 15px 0 5px;
}
dl dt:first-child {
padding: 0;
}
dl dt>:first-child {
margin-top: 0px;
}
dl dt>:last-child {
margin-bottom: 0px;
}
dl dd {
margin: 0 0 15px;
padding: 0 15px;
}
dl dd>:first-child {
margin-top: 0px;
}
dl dd>:last-child {
margin-bottom: 0px;
}
/* CODE
=============================================================================*/
pre, code, tt {
font-size: 12px;
font-family: Consolas, "Liberation Mono", Courier, monospace;
}
code, tt {
margin: 0 0px;
padding: 0px 0px;
white-space: nowrap;
border: 1px solid #eaeaea;
background-color: #f8f8f8;
border-radius: 3px;
}
pre>code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
background: transparent;
}
pre {
background-color: #f8f8f8;
border: 1px solid #ccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px;
}
pre code, pre tt {
background-color: transparent;
border: none;
}
kbd {
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
background-color: #DDDDDD;
background-image: linear-gradient(#F1F1F1, #DDDDDD);
background-repeat: repeat-x;
border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD;
border-image: none;
border-radius: 2px 2px 2px 2px;
border-style: solid;
border-width: 1px;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
line-height: 10px;
padding: 1px 4px;
}
/* QUOTES
=============================================================================*/
blockquote {
border-left: 4px solid #DDD;
padding: 0 15px;
color: #777;
}
blockquote>:first-child {
margin-top: 0px;
}
blockquote>:last-child {
margin-bottom: 0px;
}
/* HORIZONTAL RULES
=============================================================================*/
hr {
clear: both;
margin: 15px 0;
height: 0px;
overflow: hidden;
border: none;
background: transparent;
border-bottom: 4px solid #ddd;
padding: 0;
}
/* IMAGES
=============================================================================*/
img {
max-width: 100%
}
-->
Thrift简单实践的更多相关文章
- Java 异步处理简单实践
Java 异步处理简单实践 http://www.cnblogs.com/fangfan/p/4047932.html 同步与异步 通常同步意味着一个任务的某个处理过程会对多个线程在用串行化处理,而异 ...
- Android 设计随便说说之简单实践(合理组合)
上一篇(Android 设计随便说说之简单实践(模块划分))例举了应用商店设计来说明怎么做模块划分.模块划分主要依赖于第一是业务需求,具体是怎么样的业务.应用商店则包括两个业务,就是向用户展示appl ...
- c#中,委托Func的简单实践
c# 委托Func的简单实践最近才真正的接触委托,所以针对Func类型的委托,做一个实践练习. 首先说一些我对委托的初级理解:"就是把方法当做参数,传进委托方法里". 我平时用到的 ...
- kafka原理和实践(二)spring-kafka简单实践
系列目录 kafka原理和实践(一)原理:10分钟入门 kafka原理和实践(二)spring-kafka简单实践 kafka原理和实践(三)spring-kafka生产者源码 kafka原理和实践( ...
- SQL知识以及SQL语句简单实践
综述 大家都知道SQL是结构化查询语言,是关系数据库的标准语言,是一个综合的,功能极强的同时又简洁易学的,它集级数据查询(Data Quest),数据操纵(Data Manipulation),数据定 ...
- Python Thrift 简单示例
本文基于Thrift-0.10,使用Python实现服务器端,使用Java实现客户端,演示了Thrift RPC调用示例.Java客户端提供两个字符串参数,Python服务器端计算这两个字符串的相似度 ...
- ZooKeeper分布式锁简单实践
ZooKeeper分布式锁简单实践 在分布式解决方案中,Zookeeper是一个分布式协调工具.当多个JVM客户端,同时在ZooKeeper上创建相同的一个临时节点,因为临时节点路径是保证唯一,只要谁 ...
- Spring 学习二-----AOP的原理与简单实践
一.Spring AOP的原理 AOP全名Aspect-Oriented Programming,中文直译为面向切面(方面)编程.何为切面,就比如说我们系统中的权限管理,日志,事务等我们都可以将其看 ...
- VueRouter爬坑第一篇-简单实践
VueRouter系列的文章示例编写时,项目是使用vue-cli脚手架搭建. 项目搭建的步骤和项目目录专门写了一篇文章:点击这里进行传送 后续VueRouter系列的文章的示例编写均基于该项目环境. ...
随机推荐
- db2基础
DB2知识文档 一.db2 基础 基本语法 注释:"--"(两个减号) 字符串连接:"||" 如set msg='aaaa'||'bbbb',则msg为'aaa ...
- FastDateFormat
1 public static final FastDateFormat ISO_DATE_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd&q ...
- Ubuntu安装Oracle SQLDeveloper
1、下载Oracle安装文件 这里我下载的是Linux RPM版本,文件名为sqldeveloper-4.0.3.16.84-1.noarch.rpm http://www.oracle.com/te ...
- mark
*求数根公式:a的数根b = (a-1) % 9 + 1; *约瑟环问题:f1 = 0; 第i个(i>1),f = (f+m) %i;
- wamp2.5 局域网无法访问问题
1.打开http.conf文件,在对应处修改为如下内容(通常经过步骤一之后就能访问了,若不行则再执行后面步骤) <Directory /> Options FollowSymLinks A ...
- CI框架,双层弹出框的样式实现
在弹出的主页面上,写一个隐藏的悬浮的div 通过标记使他显示,通过计数器使他关闭 部分代码: <div id="common_msg"></div>//主页 ...
- mysql解决其他服务器不可连接问题
在安装mysql的机器上运行: 1.d:\mysql\bin\>mysql -h localhost -u root //这样应该可以进入MySQL服务器 2.mysql> ...
- 如何设置mysql远程访问及防火墙设置
笔者在一个实际的项目中需要MYSQL远程访问. 情景: 安装好Mysql, 本地访问正常,很奇怪局域的机器都无法访问该服务器上的MYSQL数据库. 经过资料查找 原来Mysql默认是不可以通过远程机器 ...
- 通过修改i8042prt端口驱动中类驱动Kbdclass的回调函数地址,达到过滤键盘操作的例子
同样也是寒江独钓的例子,但只给了思路,现贴出实现代码 原理是通过改变端口驱动中本该调用类驱动回调函数的地方下手 //替换分发函数 来实现过滤 #include <wdm.h> #inclu ...
- [.net 面向对象程序设计进阶] (14) 缓存(Cache) (一) 认识缓存技术
[.net 面向对象程序设计进阶] (14) 缓存(Cache)(一) 认识缓存技术 本节导读: 缓存(Cache)是一种用空间换时间的技术,在.NET程序设计中合理利用,可以极大的提高程序的运行效率 ...