erlang + thrift with the development

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

To use the thrift + erlang to develop, to go through the following steps.

 

1. understanding of thrift.

thrift function is indeed powerful, but thrift lack of documentation it is indeed flawed, especially the specific language of the API documentation is missing, the Internet is basically java api documentation. Here are some of my collection of documents, the definition and use of thrift has specific presentation, data entry required it can be said of thrift.

Thrift: The Missing Guide

There pdf documents can be downloaded, 15, dapper, I just read this document to understand thrift.

thrift whitepaper

thrift white paper introduces the thrift's history and mission.

Apache Thrift - scalable cross-language services development framework

Use java developers can see.

Thrift Practice

Some of the features introduced thrift

Apache Thrift-quick tutorial

Content includes a series of installation, use and so on.

After reading this series of documents, the definition of thrift, use, and characteristics will have a more in-depth understanding of the problems encountered can be reduced.

 

2.thrift development of the general steps

Thrift Erlang used  which referred to the general procedure used in erlang + thrift:

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

 

3.thrift and combat erlang

First, understand the erlang code for the thrift, following Bowen is reading experiences code

Thrift Erlang source code to read

After reading this blog, we started thrift + development of the erlang.

thrift + erlang use cases have thrift source inside Tutorial, understand this example, may be used basically to develop thrift erlang server program.

Here, I use is another example, it is another example of the Internet:

The first test Thrift  (owned ladder)

thrift Erlang adhesive  (self ladder)

Hello.thrift definitions file:

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

Generate erl files:

thrift --gen erl hello.thrift

gen-erl directory there erlang code that we need.

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

Open hello_thrift.erl, which is:

%%
%% 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.

The method can be seen say defined above, say there is a method of the type parameters hello_thrift.erl inside, return type information, abnormal.

It does not define any type hello.thrift inside, so there is no substance in hello_types.erl.

View from the above examples, the idea is to produce erlang file named according to type and service.

 

In gen-erl directory, create a new file 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.

If this reference hello_server2.erl and thrift-0.9.1 / tutorial / erl / server.erl, can be found almost their structure.

General idea of ​​the code is to use thrift_socket_server: start / 1 function to start the service, export handle_function / 2 function to handle RPC access.

It can be seen, the general idea is thrift file, which adds the function definition to be exported, and / additional function code 2 at handle_function. thrift framework really save development time programmers.

Use erlang do client development thrift, but also relatively easy, directly on the code:

-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.

 

The demo map:

 

 

Reproduced in: https: //my.oschina.net/u/191928/blog/618617

Guess you like

Origin blog.csdn.net/weixin_34411563/article/details/91986996