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. POJ 3233Matrix Power Series

    妈妈呀....这简直是目前死得最惨的一次. 贴题目: http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS Mem ...

  2. weex append

    append有两个值:其中的一个是tree, 另外一个是node. 不会像数据绑定一样对最后的渲染结果有影响.但它决定是否会影响整个节点的重绘还是只是某一个地方的内容会重绘. append=" ...

  3. 关于ARC下需要dealloc的相关内容

    今天在项目中使用KVO添加观察者模式的时候,在返回上一级的时候竟然崩了.可是,看了很久,代码没有问题.最后,终于知道了需要添加dealloc 防止以后再出错,所以,便纪录下来.关于ARC下需要手动释放 ...

  4. Python 爬虫5——爬取并下载网页指定规格的图片

    看完上篇文档之后,我们对于正则表达式已经有了基本的了解,其实学习最有效的办法就是带着问题和目的,这里我们假设有一个目标:获取某个网页上指定规格的图片的链接地址,并下载到本地. 一.实现步骤: 1.在浏 ...

  5. 解析文件+AcitonBar展示:

    //项目效果:

  6. 安装OS X虚拟机错误vcpu-0:VERIFY vmcore/vmm/main/physMem_monitor.c:1123

    新建一个虚拟机, 选择客户机操作系统为Apple MacOS X 10.10, 其余参数可以默认. 注意建好之后不要急着打开客户机, 因为直接打开你会发现新建的客户机将会无法启动. 仔细阅读Mac O ...

  7. AndroidStudio错误总结及解决(待续)

    AndroidStudio错误总结及解决 一. 当安装好AndroidStudio开启的时候出现如下错误: 百度的解决方法: 1)进入刚安装的Android Studio目录下的bin目录.找到ide ...

  8. pyserial 16进制显示与发送

    pyserial 16进制显示与发送 http://www.centoscn.com/python/2013/0817/1320.html 十六进制显示的实质是把接收到的字符诸葛转换成其对应的ASCI ...

  9. xcodebuild编译ipa

    #!/bin/sh # autoBuild.sh # CTest # # Created by Ethan on 14-11-3. # Copyright (c) 2014年 Ethan. All r ...

  10. SQL基础--同义词

    同义词的概念: 同义词是Oracle对象的别名,使用同义词访问相同的对象 可以为表.视图.存储过程.函数或另一同义词等对象创建同义词 方便访问其它用户的对象,隐藏了对象的身份 缩短对象名字的长度 同义 ...