windows配置thrift开发环境

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 ,下载地址:http://repo1.maven.org/maven2/org/apache/thrift/libthrift/0.9.0/

          2.2)slf4j-api.jar

          2.3)slf4j-simple.jar

     3)编写thrift 接口文件

[java]  view plain copy
  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]  view plain copy
  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]  view plain copy
  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(truetrue);    
  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]  view plain copy
  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端

[html]  view plain copy
  1. thrift server init  
  2. thrift server start  
  3. thrift server open port 1234  
         8.2)启动client端

[html]  view plain copy
  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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值