zoukankan      html  css  js  c++  java
  • windows配置thrift开发环境

     

      1)安装thrift:到thrift官网下载exe文件,然后将文件重命名为thrift.exe,拷贝到c:windows目录下(或者任何目录下),然后就可以在dos环境下使用了

    c:windows>thrift -gen java D:myworkjavaProject hriftTest est.thrift ,输出的java文件默认输出到当前目录下c:windows,也可以使用-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 接口文件

    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)编写接口实现代码

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

        5)编写server代码

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

        6)编写client 代码

    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. public class Client {  
    12.     public void startClient() {    
    13.         TTransport transport;    
    14.         try {    
    15.             System.out.println("thrift client connext server at 1234 port ");  
    16.             transport = new TSocket("localhost", 1234);    
    17.             TProtocol protocol = new TBinaryProtocol(transport);    
    18.             Hello.Client client = new Hello.Client(protocol);    
    19.             transport.open();    
    20.             System.out.println(client.helloString("panguso"));    
    21.             transport.close();    
    22.             System.out.println("thrift client close connextion");  
    23.         } catch (TTransportException e) {    
    24.             e.printStackTrace();    
    25.         } catch (TException e) {    
    26.             e.printStackTrace();    
    27.         }    
    28.     }    
    29.     
    30.     public static void main(String[] args) {    
    31.         System.out.println("thrift client init ");  
    32.         Client client = new Client();    
    33.         System.out.println("thrift client start ");  
    34.         client.startClient();    
    35.         System.out.println("thrift client end ");  
    36.     }    
    37. }  

     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 

    下载地址:http://download.csdn.net/download/liyonghui123/5742211

  • 相关阅读:
    转:request.getSession(true)和request.getSession(false)的区别
    转:Linux中文显示乱码?如何设置centos显示中文
    Linux Centos 6.5_x86安装Nginx
    Java反射详解
    Java泛型
    泛型的通配符扩展
    文本分类:survey
    CRF++官方文档
    Paper: Bidirectional LSTM-CRF Models for Sequence Tagging
    PCA数学推导及原理(转)
  • 原文地址:https://www.cnblogs.com/liyonghui/p/3186140.html
Copyright © 2011-2022 走看看