Thrift使用总结篇
2012-08-21 21:21:58
分类: LINUX
工程目录如下:
可以给它传递一个thrift配置文件然后编译输出相应的代码.
简单介绍一下如何编写thrift配置文件
对以上代码的简单分析:
project
idl/
gen.bat
thrift.exe
main.thrift
res/
a.jpg
lib/
a.py
其中idl里面存放的是thrift相关的配置文件内容。gen.bat的内容如下:
点击(此处)折叠或打开
- @ECHO OFF
- set THRIFT_CODEDEFINE=main.thrift
- if "%1"=="" goto genDefault
-
- :genDefault
- call thrift-0.7.0.exe --gen py main.thrift
- goto end
-
- :genByArg
- call thrift-0.7.0.exe --gen java:hashcode %1
- call thrift-0.7.0.exe --gen php %1
- call thrift-0.7.0.exe --gen py %1
- goto end
-
- :exit
- exit /b 1
-
- :end
- exit /b 0
ps:如果是py的话中文是个问题的.需要手工置#coding:utf-8
main.thrift的内容如下
点击(此处)折叠或打开
- namespace py thriftlib.adspub.thrift
- exception ThrfitException {
- 1: i32 errCode,
- 2: string msg
- }
- struct ResultInfo{
- 1: i32 err,
- 2: string value,
- 3: string msg
- }
- struct A {
- 1: string type,
- 2: i32 age
- }
- struct B {
- 1: i32 id,
- 2: list a
- }
-
service InfoService { - ResultInfo deployTemplateCreate(1:B deployObject,2:string username);
- list deployPlanList(1:string planName);
- }
- handler = DeployInfoHandler() # 这个就是实现了接口方法的实现类
- processor = DeployInfoService.Processor(handler)
- transport = TSocket.TServerSocket("0.0.0.0", 10001)
- tfactory = TTransport.TBufferedTransportFactory()
- pfactory = TBinaryProtocol.TBinaryProtocolFactory()
- server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
- server.serve()