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. day 13 内置函数

    内置函数思维导图:https://www.processon.com/view/link/5c13ad2de4b0ed122da75668 内置函数 作用域相关:   locals() 返回当前作用域 ...

  2. Linux下Bash shell学习笔记

    原文地址: http://www.cnblogs.com/NickQ/p/8870423.html 1.shell下没有变量类型和定义的概念. 变量直接使用不用定义 所有值都视为字符串. 在对变量取值 ...

  3. java语言描述 用抽象类模拟咖啡机的工作

    import java.util.Scanner; class Test { public static void main(String[] args) { coffee per = new cof ...

  4. C语言,初次见面~

    C语言是一门介于低级语言(如汇编语言)和高级语言(如Java,Python)之间的一门编程语言,所以它兼有两类语言的一些优点,并且具有自身的一些特点. 1.c语言的高效性.c语言具有通常是汇编语言才具 ...

  5. LeetCode二叉树实现

    LeetCode二叉树实现 # 定义二叉树 class TreeNode: def __init__(self, x): self.val = x self.left = None self.righ ...

  6. 日期插件Mobiscroll

    http://mobiscroll.com/ http://www.cnblogs.com/hxling/archive/2012/12/12/2814207.html http://www.wglo ...

  7. URL传值中文乱码的解决

    使用 tomcat 时,相信大家都回遇到中文乱码的问题,具体表现为通过表单取得的中文数据为乱码. 一.初级解决方法 通过一番检索后,许多人采用了如下办法,首先对取得字符串按照 iso8859-1 进行 ...

  8. sublime安装php_beautifier来格式化PHP代码

    注:如果你使用sublime3,php版本是5.6以上,推荐使用这个插件phpfmt 环境 操作系统:windows7 sublime版本:2.0.2 PHP安装路径: D:\wamp\bin\php ...

  9. SQL注入篇二------利用burp盲注,post注入,http头注入,利用burpsuit找注入点,宽字节注入

    1.布尔盲注burpsuit的使用 先自己构造好注入语句,利用burpsuit抓包,设置变量,查出想要的信息. 比如----查数据库名的ascii码得到数据库构造好语句 http://123.206. ...

  10. linux基础——文件挂载,lamp安装

    一. 文件挂载 lsblk -f 显示文件系统信息 mount -t vfat UUID="ffffffffff" /mnt   挂载到/mnt目录 Linux针对于各式U盘挂载方 ...