Thrift使用總結篇

工程目錄如下:
project 
    idl/
gen.bat
thrift.exe
main.thrift
    res/
a.jpg
    lib/
a.py
其中idl裏面存放的是thrift相關的配置文件內容。gen.bat的內容如下:

點擊(此處)摺疊或打開

  1. @ECHO OFF
  2. set THRIFT_CODEDEFINE=main.thrift
  3. if "%1"=="" goto genDefault

  4. :genDefault
  5. call thrift-0.7.0.exe --gen py main.thrift
  6. goto end

  7. :genByArg
  8. call thrift-0.7.0.exe --gen java:hashcode %1
  9. call thrift-0.7.0.exe --gen php %1
  10. call thrift-0.7.0.exe --gen py %1
  11. goto end

  12. :exit
  13. exit /b 1

  14. :end
  15. exit /b 0
可以給它傳遞一個thrift配置文件然後編譯輸出相應的代碼.
ps:如果是py的話中文是個問題的.需要手工置#coding:utf-8

main.thrift的內容如下

點擊(此處)摺疊或打開

  1. namespace py thriftlib.adspub.thrift
  2. exception ThrfitException {
  3. 1: i32 errCode,
  4. 2: string msg
  5. }
  6. struct ResultInfo{
  7. 1: i32 err,
  8. 2: string value,
  9. 3: string msg
  10. }
  11. struct A {
  12. 1: string type,
  13. 2: i32 age
  14. }
  15. struct B {
  16. 1: i32 id,
  17. 2: list a
  18. }

  19. service InfoService {
  20. ResultInfo deployTemplateCreate(1:B deployObject,2:string username);
  21. list deployPlanList(1:string planName);
  22. }
簡單介紹一下如何編寫thrift配置文件

tskt = TSocket.TSocket(ip,port)
tskt.setTimeout(self.timeout)
self.transport = TTransport.TBufferedTransport(tskt)
 protocol = TBinaryProtocol.TBinaryProtocol(self.transport)
client = service_class.Client(protocol)
注意:這裏面的服務類名就是生成的thrift的接口類

實際工作中要注意的事項:
1、
2、
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章