分享

windows配置thrift开发环境

 李副营长 2014-06-29
       1)安装thrift:到thrift官网下载exe文件,然后将文件重命名为thrift.exe,拷贝到c:\windows目录下,然后就可以在dos环境下使用了

           如:thrift -gen java D:\mywork\javaProject\thriftTest\test.thrift ,输出的java文件默认输出到当前目录下,也可以使用-o参数指定输出路径

     2)下载相关依赖包

          2.1)libthrift.jar(注意最好要和下载的exe版本一样) 

                  下载地址:http://repo1./maven2/org/apache/thrift/libthrift/0.9.0/

          2.2)slf4j-api.jar

          2.3)slf4j-simple.jar

     3)编写thrift 接口文件

  1. namespace cpp zam.thrift.test  
  2. namespace py thriftTest  
  3. namespace java com.zam.thrift.test  
  4. namespace php thriftTest  
  5.   
  6. service Hello {    
  7.     string helloString(1:string word)    
  8. }  
    4)编写接口实现代码

[java] 
  1. package com.zam.server;  
  2.   
  3. import org.apache.thrift.TException;  
  4.   
  5. import com.zam.thrift.test.Hello.Iface;  
  6.   
  7. public class HelloImpl implements Iface{  
  8.     private static int count = 0;  
  9.       
  10.     @Override  
  11.     public String helloString(String word) throws TException {  
  12.         // TODO Auto-generated method stub  
  13.         count += 1;  
  14.         System.out.println("get " + word + " " +count);   
  15.         return "hello " + word + " " + count;  
  16.     }  
  17.   
  18. }  
    5)编写server代码

[java] 
  1. package com.zam.server;  
  2.   
  3. import org.apache.thrift.protocol.TBinaryProtocol;    
  4. import org.apache.thrift.protocol.TBinaryProtocol.Factory;    
  5. import org.apache.thrift.server.TServer;    
  6. import org.apache.thrift.server.TThreadPoolServer;    
  7. import org.apache.thrift.server.TThreadPoolServer.Args;    
  8. import org.apache.thrift.transport.TServerSocket;    
  9. import org.apache.thrift.transport.TTransportException;   
  10.   
  11. import com.zam.thrift.test.Hello;  
  12. import com.zam.thrift.test.Hello.Processor;  
  13.   
  14. public class Server {  
  15.     public void startServer() {    
  16.         try {    
  17.             System.out.println("thrift server open port 1234");  
  18.             TServerSocket serverTransport = new TServerSocket(1234);    
  19.             Hello.Processor process = new Processor(new HelloImpl());    
  20.             Factory portFactory = new TBinaryProtocol.Factory(true, true);    
  21.             Args args = new Args(serverTransport);    
  22.             args.processor(process);    
  23.             args.protocolFactory(portFactory);    
  24.             TServer server = new TThreadPoolServer(args);    
  25.             server.serve();    
  26.         } catch (TTransportException e) {    
  27.             e.printStackTrace();    
  28.         }    
  29.     }    
  30.         
  31.     public static void main(String[] args) {    
  32.         System.out.println("thrift server init");  
  33.         Server server = new Server();    
  34.         System.out.println("thrift server start");  
  35.         server.startServer();    
  36.         System.out.println("thrift server end");  
  37.     }    
  38. }  
    6)编写client 代码

[java] 
  1. package com.zam.server;  
  2.   
  3. import org.apache.thrift.TException;    
  4. import org.apache.thrift.protocol.TBinaryProtocol;    
  5. import org.apache.thrift.protocol.TProtocol;    
  6. import org.apache.thrift.transport.TSocket;    
  7. import org.apache.thrift.transport.TTransport;    
  8. import org.apache.thrift.transport.TTransportException;   
  9.   
  10. import com.zam.thrift.test.Hello;  
  11.   
  12. public class Client {  
  13.     public void startClient() {    
  14.         TTransport transport;    
  15.         try {    
  16.             System.out.println("thrift client connext server at 1234 port ");  
  17.             transport = new TSocket("localhost", 1234);    
  18.             TProtocol protocol = new TBinaryProtocol(transport);    
  19.             Hello.Client client = new Hello.Client(protocol);    
  20.             transport.open();    
  21.             System.out.println(client.helloString("panguso"));    
  22.             transport.close();    
  23.             System.out.println("thrift client close connextion");  
  24.         } catch (TTransportException e) {    
  25.             e.printStackTrace();    
  26.         } catch (TException e) {    
  27.             e.printStackTrace();    
  28.         }    
  29.     }    
  30.     
  31.     public static void main(String[] args) {    
  32.         System.out.println("thrift client init ");  
  33.         Client client = new Client();    
  34.         System.out.println("thrift client start ");  
  35.         client.startClient();    
  36.         System.out.println("thrift client end ");  
  37.     }    
  38. }  
     8)运行server和client代码

           8.1)启动server端

  1. thrift server init  
  2. thrift server start  
  3. thrift server open port 1234  
         8.2)启动client端
  1. thrift client init   
  2. thrift client start   
  3. thrift client connext server at 1234 port   
  4. hello panguso 1  
  5. thrift client close connextion  
  6. thrift client end   

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多