thrift0.9.1简单教程(包含Java服务端和Java、python客户端)

一、Thrift Java服务端和客户端

  • 官方网站下载thrift-0.9.1.exe,用于生成代码

  • 编写hello.thrift,数据结构描述文件

1
2
3
4
5
namespace java com.test
 
service  HelloWorldService {
   string sayHello(1:string username)
}
  • 生成java代码:thrift-0.9.1.exe -r -gen java hello.thrift

  • 将当前目录gen-java文件夹下生产代码copy到Eclipse源码目录

  • 服务端实现类HelloWorldImpl,服务器启动类HelloWorldServer,客户端类HelloWorldClient

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package  com.abc;
 
import  org.apache.thrift.TException;
 
public  class  HelloWorldImpl  implements  HelloWorldService.Iface {
 
     public  HelloWorldImpl() {
     }
 
     @Override
     public  String sayHello(String username)  throws  TException {
         return  "hello, "  + username;
     }
 
}

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package  com.abc;
 
import  org.apache.thrift.TProcessor;
import  org.apache.thrift.protocol.TBinaryProtocol;
import  org.apache.thrift.server.TServer;
import  org.apache.thrift.server.TSimpleServer;
import  org.apache.thrift.transport.TServerSocket;
 
public  class  HelloServer {
     public  static  final  int  SERVER_PORT =  8090 ;
 
     public  void  startServer() {
         try  {
             System.out.println( "HelloWorld TSimpleServer start ...." );
 
             TProcessor tprocessor =  new  HelloWorldService.Processor<HelloWorldService.Iface>( new  HelloWorldImpl());
             TServerSocket serverTransport =  new  TServerSocket(SERVER_PORT);
             TServer.Args tArgs =  new  TServer.Args(serverTransport);
             tArgs.processor(tprocessor);
             tArgs.protocolFactory( new  TBinaryProtocol.Factory());
             TServer server =  new  TSimpleServer(tArgs);
             server.serve();
         catch  (Exception e) {
             System.out.println( "Server start error!!!" );
             e.printStackTrace();
         }
     }
 
     public  static  void  main(String[] args) {
         HelloServer server =  new  HelloServer();
         server.startServer();
     }
 
}

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package  com.abc;
 
import  org.apache.thrift.TException;
import  org.apache.thrift.protocol.TBinaryProtocol;
import  org.apache.thrift.protocol.TProtocol;
import  org.apache.thrift.transport.TSocket;
import  org.apache.thrift.transport.TTransport;
import  org.apache.thrift.transport.TTransportException;
 
public  class  HelloClient {
 
     public  static  final  String SERVER_IP =  "localhost" ;
     public  static  final  int  SERVER_PORT =  8090 ;
     public  static  final  int  TIMEOUT =  30000 ;
 
     public  void  startClient(String userName) {
         TTransport transport =  null ;
         try  {
             transport =  new  TSocket(SERVER_IP, SERVER_PORT, TIMEOUT);
             // 协议要和服务端一致
             TProtocol protocol =  new  TBinaryProtocol(transport);
             // TProtocol protocol = new TCompactProtocol(transport);
             // TProtocol protocol = new TJSONProtocol(transport);
             HelloWorldService.Client client =  new  HelloWorldService.Client(protocol);
             transport.open();
             String result = client.sayHello(userName);
             System.out.println( "Thrify client result =: "  + result);
         catch  (TTransportException e) {
             e.printStackTrace();
         catch  (TException e) {
             e.printStackTrace();
         finally  {
             if  ( null  != transport) {
                 transport.close();
             }
         }
     }
 
     public  static  void  main(String[] args) {
         HelloClient client =  new  HelloClient();
         client.startClient( "Michael" );
 
     }
 
}

 

二、python客户端调用刚才创建Java Server示例

  • 创建python客户端依赖文件:thrift-0.9.1.exe -r -gen py hello.thrift

  • 编写客户端文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/usr/bin/env python
 
import  sys
sys.path.append( './gen-py' )
 
from  hello  import  HelloWorldService
 
from  thrift  import  Thrift
from  thrift.transport  import  TSocket
from  thrift.transport  import  TTransport
from  thrift.protocol  import  TBinaryProtocol
 
try :
   transport  =  TSocket.TSocket( 'localhost' 9090 )
   transport  =  TTransport.TBufferedTransport(transport)
   protocol  =  TBinaryProtocol.TBinaryProtocol(transport)
   client  =  HelloWorldService.Client(protocol)
   transport. open ()
 
   msg  =  client.sayHello( "jack" )
   print  "server reponse: "  +  msg
 
except  Thrift.TException, ex:
   print  "%s"  %  (ex.message)
finally :
   transport.close()

 

  • 调用:python client.py

    • 如果在调用过程出现no module named thrift.thrift错误,需要解压缩thrift源码包,进入lib/python(或lib/py)目录下运行python setup.py install

       

参考:http://thrift-tutorial.readthedocs.org/en/latest/index.html

 

--end

你可能感兴趣的:(python)