打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
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(注意最好要和下载的exe版本一样) 

                  下载地址:http://repo1.maven.org/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   

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Apache Thrift 支持异构语言之间的调用
Thrift使用样例代码
使用thrift作为go和C++中间rpc及问题(一) | Go语言中文网 | Golang中文社区 | Golang中国
Apache Thrift学习小记
java socket 实现服务端与客户端
跟着实例学习ZooKeeper的用法: 缓存 | 并发编程网 – ifeve.com
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服