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主要有一下这些类型:

  1. bool --简单类型,true or false
  2. byte --简单类型,单字符
  3. i16 --简单类型,16位整数
  4. i32 --简单类型,32位整数
  5. i64 --简单类型,64位整数
  6. double --简单类型,双精度浮点型
  7. string --简单类型,utf-8编码字符串
  8. binary --二进制,未编码的字符序列
  9. struct --结构体,对应结构体、类等对象类型
  10. list --list容器
  11. set --set容器
  12. map --map容器
  13. 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简单实践的更多相关文章

  1. Java 异步处理简单实践

    Java 异步处理简单实践 http://www.cnblogs.com/fangfan/p/4047932.html 同步与异步 通常同步意味着一个任务的某个处理过程会对多个线程在用串行化处理,而异 ...

  2. Android 设计随便说说之简单实践(合理组合)

    上一篇(Android 设计随便说说之简单实践(模块划分))例举了应用商店设计来说明怎么做模块划分.模块划分主要依赖于第一是业务需求,具体是怎么样的业务.应用商店则包括两个业务,就是向用户展示appl ...

  3. c#中,委托Func的简单实践

    c# 委托Func的简单实践最近才真正的接触委托,所以针对Func类型的委托,做一个实践练习. 首先说一些我对委托的初级理解:"就是把方法当做参数,传进委托方法里". 我平时用到的 ...

  4. kafka原理和实践(二)spring-kafka简单实践

    系列目录 kafka原理和实践(一)原理:10分钟入门 kafka原理和实践(二)spring-kafka简单实践 kafka原理和实践(三)spring-kafka生产者源码 kafka原理和实践( ...

  5. SQL知识以及SQL语句简单实践

    综述 大家都知道SQL是结构化查询语言,是关系数据库的标准语言,是一个综合的,功能极强的同时又简洁易学的,它集级数据查询(Data Quest),数据操纵(Data Manipulation),数据定 ...

  6. Python Thrift 简单示例

    本文基于Thrift-0.10,使用Python实现服务器端,使用Java实现客户端,演示了Thrift RPC调用示例.Java客户端提供两个字符串参数,Python服务器端计算这两个字符串的相似度 ...

  7. ZooKeeper分布式锁简单实践

    ZooKeeper分布式锁简单实践 在分布式解决方案中,Zookeeper是一个分布式协调工具.当多个JVM客户端,同时在ZooKeeper上创建相同的一个临时节点,因为临时节点路径是保证唯一,只要谁 ...

  8. Spring 学习二-----AOP的原理与简单实践

    一.Spring  AOP的原理 AOP全名Aspect-Oriented Programming,中文直译为面向切面(方面)编程.何为切面,就比如说我们系统中的权限管理,日志,事务等我们都可以将其看 ...

  9. VueRouter爬坑第一篇-简单实践

    VueRouter系列的文章示例编写时,项目是使用vue-cli脚手架搭建. 项目搭建的步骤和项目目录专门写了一篇文章:点击这里进行传送 后续VueRouter系列的文章的示例编写均基于该项目环境. ...

随机推荐

  1. 使用SQL Server 2008 维护计划(图解)

    使用Sql Server 2008的维护计划可以实现自动备份数据库,并自动删除过期备份的功能. 一.环境 OS: Microsoft Windows Server 2003 R2 soft:Micro ...

  2. 如何在select下拉列表中添加复选框?

    近来在给一个公司做考试系统的项目,遇到的问题不少,但其中的几个让我对表单的使用颇为感兴趣,前端程序员都知道,下拉列表有select标签,复选框有checkbox,但是两者合在一起却少有人去研究,当时接 ...

  3. Oracle中的CHR()函数与ASCII()函数

    工作中经常会处理一些因特殊字符而导致的错误,如上周我就遇到了因为换行符和回车符导致的数据上报的错误,这种错误比较难以发现,通常是由于用户的输入习惯导致的,有可能数据极少,就那么几行错误从而导致整个数据 ...

  4. nodejs学习之加密

    Nodejs中的加密是Crypto模块, 1.md5的使用 var crypto = require("crypto"); //创建 var md5 = crypto.create ...

  5. NGINX实现反向代理

    一.安装NGINX 略,请自行百度,GOOGEL 二.配置文件1.由上面的步骤,我们看到配置文件放置在/etc/nginx/目录下:主要配置文件:/etc/nginx/nginx.conf 扩展配置文 ...

  6. linux命令初识

    一.查看当前的目录文件 ls  demo   查看demo目录下的所有文件 ls  -l  demo/test.txt   --查看指定目录(test.txt)的详细内容 二.复制文件 cp   or ...

  7. 【转】成为Java顶尖程序员 ,看这10本书就够了

    “学习的最好途径就是看书“,这是我自己学习并且小有了一定的积累之后的第一体会.个人认为看书有两点好处: 1.能出版出来的书一定是经过反复的思考.雕琢和审核的,因此从专业性的角度来说,一本好书的价值远超 ...

  8. dev GridControl 根据鼠标坐标 选中行

    if (e.Button == System.Windows.Forms.MouseButtons.Right) { DevExpress.XtraGrid.Views.Grid.ViewInfo.G ...

  9. ExtJS扩展:扩展grid

    ExtJs的grid功能很强大,但是有时候觉得总是少那么一点点功能,我们就来扩展它,让它用起来更方便. 今天我们要扩展的是:根据记录的选择数量来禁用或启用grid toolbar上的某些按钮. 本文所 ...

  10. 《深入理解Java虚拟机》垃圾收集器

    说起垃圾收集(Garbage Collection,GC),大部分人都把这项技术当做Java语言的伴生产物.事实上,GC的历史远比Java久远,1960年诞生于MIT的Lisp是第一门真正使用内存动态 ...