I  think, thrift is a  tcp/ip based Client-Server architecture multi-languages supported RPC framework.

要使用thrift+erlang开发,要经过下面几个步骤。

1.对thrift了解。

thrift的功能的确是强劲,不过thrift缺少文档的确是它的硬伤,尤其是具体的语言的API文档更是缺少,网上基本上是java api的文档。下面是我收集的一些文档,对thrift的定义和使用都有具体的介绍,可以说是thrift的入门必备资料。

Thrift: The Missing Guide

有pdf文档可以下载,15页,短小精悍,我就是看了这个文档对thrift有所了解。

thrift whitepaper

thrift的白皮书,介绍了thrift的历史和使命。

Apache Thrift - 可伸缩的跨语言服务开发框架

使用java开发可以看看。

Thrift实践

介绍thrift的一些特性

Apache Thrift-quick tutorial

包括安装、使用等一系列内容。

看完这一系列文档,对thrift的定义、使用、以及特性都会有比较深入的了解,可以减少使用中遇到的问题。

2.thrift开发一般步骤

Erlang中使用Thrift 里面讲到了使用erlang+thrift的一般步骤:

第一步:编写相应的*.thrift  文件
第二步:thrift --gen erl *.thrift,将生成的gen-erl复制到src中
第三步:按照例子代码写一个模块,将*.thrift中的函数全都实现了,并在里面指定服务的端口号和启动thrift的框架
第四步:将上一步写的模块添加到整个程序启动过程的最末处,启动thrift开始对外提供服务。

3.thrift与erlang实战

首先对thrift的erlang代码有所了解,下面这个博文就是阅读代码的心得

Thrift Erlang实现源代码阅读

读了这个博文后,我们就开始thrift+erlang的开发了。

thrift+erlang的用例就有thrift源码里面的tutorial,看懂了这个示例,基本上就可以使用thrift开发erlang服务器程序。

在这里,我使用的是另外一个示例,是在网上另外一个例子:

初试thrift (自备梯子)

thrift粘合erlang (自备梯子)

定义hello.thrift文件:

service Hello{
string say(1:string name)
}

生成erl文件:

thrift --gen erl hello.thrift

gen-erl目录里面就有我们需要的erlang代码。

#ls gen-erl/
hello_constants.hrl hello_thrift.erl hello_thrift.hrl hello_types.erl hello_types.hrl

打开hello_thrift.erl,里面就是:

%%
%% Autogenerated by Thrift Compiler (0.9.1)
%%
%% DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
%% -module(hello_thrift).
-behaviour(thrift_service). -include("hello_thrift.hrl"). -export([struct_info/1, function_info/2]). struct_info('i am a dummy struct') -> undefined.
%%% interface
% say(This, Name)
function_info('say', params_type) ->
{struct, [{1, string}]}
;
function_info('say', reply_type) ->
string;
function_info('say', exceptions) ->
{struct, []}
;
function_info(_Func, _Info) -> no_function.

可以看到上面定义的say方法,在hello_thrift.erl里面有say方法的参数类型、返回类型,异常等信息。

在hello.thrift里面没有定义任何类型,所以在 hello_types.erl没有任何实质内容。

从上面的例子来看,产生erlang文件的思路是根据type以及service来命名。

在gen-erl目录里面,新建一个hello_server2.erl文件,

-module(hello_server2).
-include("hello_thrift.hrl").
-export([start/0, handle_function/2, say/1, stop/1]). debug(Info)->
io:format("Debug info:~s~n",[Info]). say(Name)->
io:format("~n Line:~p~n", [?LINE]),
Sentence = "Hello," ++ Name,
debug(Sentence),
BinSentence = list_to_binary(Sentence),
BinSentence. start()->
start(9090). start(Port)->
Handler = ?MODULE,
thrift_socket_server:start([{handler, Handler},
{service, hello_thrift},
{port, Port},
{name, hello_server}]). stop(Server)->
thrift_socket_server:stop(Server). handle_function(Function, Args) when is_atom(Function), is_tuple(Args) ->
case Function of
say ->
{reply, say(tuple_to_list(Args))};
% add function here
_ ->
error
end.

如果参照这个hello_server2.erl和thrift-0.9.1/tutorial/erl/server.erl,可以发现他们的结构差不多。

一般代码的思路是,使用thrift_socket_server:start/1函数来启动服务,导出handle_function/2函数来处理RPC访问。

所以可以看出,一般的思路就是在thrift文件,里面添加需要导出的函数定义,然后在handle_function/2函数处添加额外的代码。thrift框架真的是节省了程序员的开发时间。

使用erlang来做thrift的客户端开发,也比较容易,直接上代码:

-module(hello_client).
-include("hello_thrift.hrl").
-export([test/0]). p(X)->
io:format("in the p() ~w~n", [X]),
ok. test()->
Port = 9090,
{ok, Client0} = thrift_client_util:new("localhost",
Port,
hello_thrift, []),
io:format("~n Client0 : ~p~n", [Client0]),
{Client1, Res} = thrift_client:call(Client0, say, ["world"]),
io:format(" the Res is ~p~n", [Res]),
io:format("~n Client1 : ~p~n", [Client1]),
p(Res),
io:format("the Client0 == Client1: ~p~n", [Client0 == Client1]),
thrift_client:close(Client1),
ok.

效果演示图:

erlang+thrift配合开发的更多相关文章

  1. C# Thrift 实战开发 从PLC到Thrift再到客户端集成开发

    About Thrift: 本文并不是说明Thrift设计及原理的,直接拿Thrift来开发一个Demo程序,如果想要了解Thrift的细节,可以访问官方网站:https://thrift.apach ...

  2. erlang 游戏服务器开发

    http://blog.csdn.net/slmeng2002/article/details/5532771 最近关注erlang游戏服务器开发  erlang大牛写的游戏服务器值得参考 介绍本文以 ...

  3. 关于一次配合开发工作而产生的服务器内核参数问题(Android 网络问题)

    关于一次配合开发工作而产生的服务器内核参数问题(Android 网络问题) 问题转载(本人与作者遇到了同样的问题) 问题描述 问题描述:在这几年的Android开发中,遇到了一个困扰我好久的问题,有时 ...

  4. Webpack+React配合开发

    前面两篇关于webpack的基础和进阶,请先务必阅读之前的文章. Webpack教程一 Webpack教程二 什么是React React是一个由Facebook开发的library,它的口号是“A ...

  5. 使用Erlang和Yaws开发REST式的服务

    看过那张很出名的“Apache vs. Yaws”图么?是不是在考虑你也应该使用Yaws了?这些图给人的第一印象是,Yaws在可伸缩性上具有难以置信的巨大优势,它可以扩展到80000个并行的连接,而 ...

  6. 转载:如何利用Vim进行Erlang开发

    转自:http://ovalpo.info/how_to_use_vim_for_erlang_dev/ 如何利用Vim进行Erlang开发 by Martin J. Logan on Septemb ...

  7. vsp配合Qt5开发,解决virtual void * __cdecl PopDialogManger::qt_metacast

    Qt错误提示 virtual void * __cdecl PopDialogManger::qt_metacast(char const*)"(?qt_metacast@PopDialog ...

  8. 我们都是IT民工---------流浪人IDE开发札记

    你生命中的有些东西终究会失去,比如我住了6年的陈寨,这个聚集了郑州十几万IT民工的地方,说拆就拆了.再比如我玩了3年的坦克英雄,这个带给我太多快乐的游戏,说停就停了. 编程对我而言是种爱好,我上学6年 ...

  9. 在RHEL上安装Thrift(支持C++)的若干问题 » 编码无悔 / Intent & Focused

    在RHEL上安装Thrift(支持C++)的若干问题 » 编码无悔 / Intent & Focused [原创]在RHEL上安装Thrift(支持C++)的若干问题    2010年12月1 ...

随机推荐

  1. Java : logback简单配置

    需要把logback.xml文件放在类路径下,如果是spring boot项目可以用 logging.config=classpath:log/xxxxxx.xml来指定配置文件 logback la ...

  2. UEditor代码实现高亮显示

    在公司开发一个论坛系统,由于用的是UEditor(百度编辑器),单独使用的话,里面的代码不会高亮,网上找了很多,最后决定使用 highlight.js 实现代码高亮显示.效果如下: 这个是我修改其他的 ...

  3. python拓展应用:运行do文件及其衍生内容

    #-*-coding:utf8-*- import os import subprocess import shutil import psutil import time #查找指定文件名称和查找范 ...

  4. rails应用使用carrierwave和mini_magick上传用户头像

    1. 在Gemfile添加 gem 'carrierwave' gem 'mini_magick' 执行 bundle install 2. 生成uploader rails generate upl ...

  5. while do while switch语句的简要分析

    1 //// while是C语言的一个关键字,其后是使用一个小括号中的条件表达式来做为执行循环的条件, 2 ////也就是说当条件表达式的结果为真时执行大括号里面的的程序内容, 3 ////而当条件表 ...

  6. python类的封装

    Python之类的封装 1. 什么是封装 装:往容器/名称空间里存入名字 封:代表将存放于名称空间中的名字给藏起来,这种隐藏对外不对内(怎么做到的,在下文解释) 2. 为何要封装 封数据属性:不想要给 ...

  7. 慎用静态类static class

    偶然翻到一篇有趣的帖子: class - When to Use Static Classes in C# - Stack Overflowhttp://stackoverflow.com/quest ...

  8. AtCoder Regular Contest 098 F.Donation

    传送门 首先,对于一个点i,进入这个点前必须大于等于Ai,每个点必须捐赠Bi 那么我们可以在每个点最后一次经过的时候再捐赠,这样显然更优 现在我们假设每个点都是最后一次经过的时候捐赠.现在我们把捐赠的 ...

  9. 实验1 查看CPU和内存,用机器指令和汇编指令编程

    ·实验任务 (1)使用Debug,用E命令和A命令以两种方式将指令写入内存 机器码        汇编指令 b8 20 4e    mov ax,4e20h 05 16 14    add ax,14 ...

  10. 【转】Odoo装饰器: one装饰

    one装饰器的作用是对每一条记录都执行对应的方法,相当于traditional-style中的function,无返回值! 应用举例: 定义的columns now = fields.Datetime ...