Thrift Lua Example

Source: Internet
Author: User
Tags assert lua require socket

Welcome reprint, with the link can be http://blog.csdn.net/superye1983/article/details/51190166

A recent project, the idea is Nginx+lua+thrift

Thrift is a popular RPC framework, and many companies have experience in large-scale use, but there is little on-line information about Thrift-lua and example

Apache Thrift-lua is also just introduced from the Fbthrift project, in the installation process stepped on some pits, record


The first is the version issue, the latest version of Apache-thrift is 0.9.3, and this version supports the LUA version 5.2

However, since the Nginx-lua module uses the Luajit,luajit version of Lua is 5.1.4, so I use the thrift version is 0.9.2


To get started, install lua5.1 First, and the default installation will put Lua's header files in/usr/local/include

Build a directory of your own name: lua5.1, and then copy your head file in.


The first pit is make times wrong/usr/bin/ld:cannot find-llua5.2

As can be seen in the makefile.am under Lib/lua, this compilation option is written dead and modified to

Libluasocket_la_cppflags = $ (am_cppflags)-i/usr/local/include/lua5.1-dlua_compat_module
Libluasocket_la_ldflags = $ (am_ldflags) ${lua_lib}-lm

All the places where lua5.2 are written are changed according to the above, and then they can be made.


The second pit is in make install the time can not find Lualongnumber, this library is not Lua comes with, is the thrift, so should not be error Ah, and a friend can be successfully compiled installation, and I can't

Read the error message, found a clue in the generated libluabpack.la file

dependency_libs= '/home/kira/src/thrift/thrift-0.9.2/lib/lua/liblualongnumber.la-lm-lssl-lcrypto-lrt-lpthread '

But look at the log before compiling libluabpack.so before the liblualongnumber.so, doubt or makefile problem

The contents of the makefile.am are then modified to:

Lib_ltlibraries = \
libluasocket.la \
liblualongnumber.la \
libluabitwise.la \
Libluabpack.la

In fact, libluabpack.la and liblualongnumber.la change a position, modify the makefile.am after configure again, and then can smoothly through the installation of


Finally write a example, originally thrift already had a in tutorial folder, but unfortunately there is no LUA example, can only write their own

First generate the file, then write a client, the code is as follows

Package.path = Package.path. ";. /gen-lua/?. Lua "
Package.cpath = Package.cpath". ";/usr/local/lib/?" So "
require (' Tsocket ')
require (' Tbinaryprotocol ')
require (' Liblualongnumber ')
require (' Ttransport ')
require (' tutorial_calculator ')

local client

function teardown ()
  If client then
    -- Shuts down the server
--    client:testvoid ()-

    -Close the connection
    client:close ()
  End
End

function testbasicclient ()
  local socket = tsocket:new{
    port = 9090
  }
  Socket:open ()
  assert (socket, ' Failed to create client socket ')
  socket:settimeout ()

  Local protocol = tbinaryprotocol:new{
    trans = socket
  }
  assert (protocol, ' Failed to create Binary protocol ')

  client = calculatorclient:new{
    protocol = Protocol
  }

  result = Client:add (All)
  print (result)

end

testbasicclient ()
teardown ()


After running the error can not find the Lua_rawlen module, because Lua_rawlen is Lua5.2, so the code to src/luabpack.c this file is modified to size_t len = Lua_objlen (L, 2); then recompile the installation

There is a Thrift.lua in the version number of the code is also run the times wrong directly commented out


Run again or an error

./gen-lua/tutorial_calculator.lua:125:malformed number near ' 0x2233d70processor '

It's strange that the generated code starts with a number, and only one of these is changed to _tprocessor



Then start a python server, run the client, get the results back to the successful execution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.