Thrift lua example

欢迎转载,带上链接即可http://blog.csdn.net/superye1983/article/details/51190166

最近做个项目,想法是nginx+lua+thrift

thrift是一个比较流行的rpc框架,很多公司都有大规模使用的经验,不过网上很少有关于thrift-lua的介绍和example

apache的thrift-lua也是刚刚从fbthrift项目引入,在安装过程中踩了一些坑,记录一下


首先是版本问题,apache-thrift的最新版本是0.9.3,这个版本支持的lua版本是5.2

不过由于nginx-lua模块用的是luajit,luajit用的lua版本是5.1.4,所以我 用的thrift版本是0.9.2


准备工作,首先安装lua5.1,默认的安装会把lua的头文件放在/usr/local/include

自己建一个目录名字为:lua5.1,然后把头文件拷贝进去


第一个坑是make时报错/usr/bin/ld: cannot find -llua5.2

在lib/lua下的Makefile.am中可以看到,这个编译选项是写死的,修改后为

libluasocket_la_CPPFLAGS = $(AM_CPPFLAGS) -I/usr/local/include/lua5.1 -DLUA_COMPAT_MODULE
libluasocket_la_LDFLAGS = $(AM_LDFLAGS) ${LUA_LIB} -lm

所有写着lua5.2的地方都按照上面的写法改掉,然后就可以通过make了


第二个坑是在make install的时候找不到lualongnumber,这个库不是Lua自带的,是thrift中的,所以不应该会报错啊,而且有个朋友可以顺利编译安装,而我不行

看了报错信息,在生成的libluabpack.la文件中发现了线索

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

但是看日志在编译libluabpack.so之前并没有先生成liblualongnumber.so,怀疑还是Makefile问题

于是把Makefile.am中的内容修改为:

lib_LTLIBRARIES = \
                        libluasocket.la \
                        liblualongnumber.la \
                        libluabitwise.la \
                        libluabpack.la

其实就是把libluabpack.la和liblualongnumber.la换个位置,修改Makefile.am后重新configure一下,然后就能顺利通过安装了


最后写一个example,原来thrift已经有一个了在tutorial文件夹下,不过不巧没有Lua的例子,只能自己写

先是生成文件,然后写个client,代码如下

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(5000)

  local protocol = TBinaryProtocol:new{
    trans = socket
  }
  assert(protocol, 'Failed to create binary protocol')

  client = CalculatorClient:new{
    protocol = protocol
  }

  result = client:add(1,1)
  print(result)

end

testBasicClient()
teardown()


运行后报错找不到lua_rawlen模块,由于lua_rawlen是Lua5.2才有的,所以要把src/luabpack.c这个文件的代码修改为size_t len = lua_objlen(L, 2);然后重新编译安装

还有Thrift.lua中有一句版本号的代码也是 运行时报错直接注释掉


再运行还是报错

./gen-lua/tutorial_Calculator.lua:125: malformed number near '0x2233d70Processor'

很奇怪生成的代码居然以数字开头,且只有一处是这样的,修改为_TProcessor



然后起一个Python的Server,运行Client,得到返回结果执行成功

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Thrift is a framework for building cross-language services. It allows you to define data types and service interfaces in a simple definition file, and then generate code in various languages to implement those interfaces. In Python, you can use the Thrift library to create both client and server applications. To use Thrift in Python, you first need to install the Thrift library. You can do this using pip: ``` pip install thrift ``` Once installed, you can start using Thrift in your Python code. Here's a simple example to get you started: 1. Define your data types and service interface in a Thrift IDL file (e.g., `example.thrift`): ``` namespace py example struct MyStruct { 1: required string name 2: optional i32 age } service MyService { MyStruct getStruct(1: string id) } ``` 2. Generate Python code from the IDL file using the `thrift` compiler: ``` thrift --gen py example.thrift ``` 3. Implement the service interface in Python: ```python from example import MyService, ttypes class MyHandler: def getStruct(self, id): # Implementation code goes here return ttypes.MyStruct(name="John", age=25) handler = MyHandler() processor = MyService.Processor(handler) # Run the server transport = TSocket.TServerSocket(port=9090) tfactory = TTransport.TBufferedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() server = TServer.TSimpleServer(processor, transport, tfactory, pfactory) server.serve() ``` 4. Create a client to interact with the server: ```python from example import MyService, ttypes transport = TSocket.TSocket("localhost", 9090) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = MyService.Client(protocol) transport.open() struct = client.getStruct("123") print(struct.name) print(struct.age) transport.close() ``` This is just a basic example to give you an idea of how to use Thrift with Python. You can find more details and advanced usage in the Thrift documentation.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值