Apache Thrift入门(安装、测试与java程序编写)

安装Apache Thrift

ubuntu linux运行:

[plain] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. #!/bin/bash  
  2. #下载  
  3. wget http://mirrors.cnnic.cn/apache/thrift/0.9.1/thrift-0.9.1.tar.gz  
  4. tar zxvf thrift-0.9.1.tar.gz  
  5. cd thrift-0.9.1.tar.gz  
  6. ./configure  
  7. make  
  8. make install  
  9. #编译java依赖包  
  10. cd lib/java  
  11. ant  



安装ubuntu依赖

sudo apt-get install libboost-dev libboost-test-dev libboost-program-options-dev libevent-dev automake libtool flex bison pkg-config g++ libssl-dev 

编写thrift文件hello.thrift

[plain] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. namespace java com.micmiu.thrift.demo  
  2.   
  3. service  HelloWorldService {  
  4.   string sayHello(1:string username)  
  5. }  


运行

thrift -gen java hello.thrift

将在同级目录下生成gen-java/com/micmiu/thrift/demo/HelloWorldService.java文件

编写测试程序

使用maven构建

pom.xml文件

[html] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  4.   
  5.   <modelVersion>4.0.0</modelVersion>  
  6.   
  7.   <groupId>com.micmiu.thrift.demo</groupId>  
  8.   <artifactId>thrift-test</artifactId>  
  9.   <version>0.1.0-SNAPSHOT</version>  
  10.   
  11.   <dependencies>  
  12. <strong>    <dependency>  
  13.     <groupId>org.apache.thrift</groupId>  
  14.     <artifactId>libthrift</artifactId>  
  15.     <version>0.9.1</version>  
  16.     </dependency></strong>  
  17.     <dependency>  
  18.     <groupId>org.slf4j</groupId>  
  19.     <artifactId>slf4j-log4j12</artifactId>  
  20.     <version>1.5.8</version>  
  21. </dependency>  
  22.   </dependencies>  
  23.   
  24.   <build>  
  25.     <plugins>  
  26.       <plugin>  
  27.     <artifactId>maven-assembly-plugin</artifactId>  
  28.     <version>2.2-beta-5</version>  
  29.     <configuration>  
  30.       <descriptorRefs>  
  31.         <descriptorRef>jar-with-dependencies</descriptorRef>  
  32.       </descriptorRefs>  
  33.     </configuration>  
  34.       </plugin>  
  35.       <plugin>  
  36.     <groupId>org.apache.maven.plugins</groupId>  
  37.     <artifactId>maven-compiler-plugin</artifactId>  
  38.     <version>2.3.2</version>  
  39.     <configuration>  
  40.       <source>1.6</source>  
  41.       <target>1.6</target>  
  42.       <encoding>UTF-8</encoding>  
  43.     </configuration>  
  44.       </plugin>  
  45.     </plugins>  
  46.   </build>  
  47.   
  48. </project>  

以下代码来自:http://www.micmiu.com/soa/rpc/thrift-sample/

HelloClientDemo.java

[java] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. package com.micmiu.thrift.demo;  
  2.   
  3. import org.apache.thrift.TException;  
  4. import org.apache.thrift.protocol.TBinaryProtocol;  
  5. import org.apache.thrift.protocol.TCompactProtocol;  
  6. import org.apache.thrift.protocol.TJSONProtocol;  
  7. import org.apache.thrift.protocol.TProtocol;  
  8. import org.apache.thrift.transport.TSocket;  
  9. import org.apache.thrift.transport.TTransport;  
  10. import org.apache.thrift.transport.TTransportException;  
  11.   
  12. /** 
  13.  * blog http://www.micmiu.com 
  14.  * 
  15.  * @author Michael 
  16.  * 
  17.  */  
  18. public class HelloClientDemo {  
  19.   
  20.     public static final String SERVER_IP = "localhost";  
  21.     public static final int SERVER_PORT = 8090;  
  22.     public static final int TIMEOUT = 30000;  
  23.   
  24.     /** 
  25.      * 
  26.      * @param userName 
  27.      */  
  28.     public void startClient(String userName) {  
  29.         TTransport transport = null;  
  30.         try {  
  31.             transport = new TSocket(SERVER_IP, SERVER_PORT, TIMEOUT);  
  32.             // 协议要和服务端一致  
  33.             TProtocol protocol = new TBinaryProtocol(transport);  
  34.             // TProtocol protocol = new TCompactProtocol(transport);  
  35.             // TProtocol protocol = new TJSONProtocol(transport);  
  36.             HelloWorldService.Client client = new HelloWorldService.Client(  
  37.                     protocol);  
  38.             transport.open();  
  39.             String result = client.sayHello(userName);  
  40.             System.out.println("Thrify client result =: " + result);  
  41.         } catch (TTransportException e) {  
  42.             e.printStackTrace();  
  43.         } catch (TException e) {  
  44.             e.printStackTrace();  
  45.         } finally {  
  46.             if (null != transport) {  
  47.                 transport.close();  
  48.             }  
  49.         }  
  50.     }  
  51.   
  52.     /** 
  53.      * @param args 
  54.      */  
  55.     public static void main(String[] args) {  
  56.         HelloClientDemo client = new HelloClientDemo();  
  57.         client.startClient("Michael");  
  58.   
  59.     }  
  60.   
  61. }  

HelloServerDemo.java

[java] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. package com.micmiu.thrift.demo;  
  2.   
  3. import org.apache.thrift.TProcessor;  
  4. import org.apache.thrift.protocol.TBinaryProtocol;  
  5. import org.apache.thrift.protocol.TCompactProtocol;  
  6. import org.apache.thrift.protocol.TJSONProtocol;  
  7. import org.apache.thrift.protocol.TSimpleJSONProtocol;  
  8. import org.apache.thrift.server.TServer;  
  9. import org.apache.thrift.server.TSimpleServer;  
  10. import org.apache.thrift.transport.TServerSocket;  
  11.   
  12. /** 
  13.  * blog http://www.micmiu.com 
  14.  * 
  15.  * @author Michael 
  16.  * 
  17.  */  
  18. public class HelloServerDemo {  
  19.     public static final int SERVER_PORT = 8090;  
  20.   
  21.     public void startServer() {  
  22.         try {  
  23.             System.out.println("HelloWorld TSimpleServer start ....");  
  24.   
  25.             TProcessor tprocessor = new HelloWorldService.Processor<HelloWorldService.Iface>(  
  26.                     new HelloWorldImpl());  
  27.             // HelloWorldService.Processor<HelloWorldService.Iface> tprocessor =  
  28.             // new HelloWorldService.Processor<HelloWorldService.Iface>(  
  29.             // new HelloWorldImpl());  
  30.   
  31.             // 简单的单线程服务模型,一般用于测试  
  32.             TServerSocket serverTransport = new TServerSocket(SERVER_PORT);  
  33.             TServer.Args tArgs = new TServer.Args(serverTransport);  
  34.             tArgs.processor(tprocessor);  
  35.             tArgs.protocolFactory(new TBinaryProtocol.Factory());  
  36.             // tArgs.protocolFactory(new TCompactProtocol.Factory());  
  37.             // tArgs.protocolFactory(new TJSONProtocol.Factory());  
  38.             TServer server = new TSimpleServer(tArgs);  
  39.             server.serve();  
  40.   
  41.         } catch (Exception e) {  
  42.             System.out.println("Server start error!!!");  
  43.             e.printStackTrace();  
  44.         }  
  45.     }  
  46.   
  47.     /** 
  48.      * @param args 
  49.      */  
  50.     public static void main(String[] args) {  
  51.         HelloServerDemo server = new HelloServerDemo();  
  52.         server.startServer();  
  53.     }  
  54.   
  55. }  


maven工程的src/main/java/com/micmiu/thrift/demo文件夹下有4个文件:

HelloClientDemo.java

HelloServerDemo.java

HelloWorldImpl.java

HelloWorldService.java

其中HelloWorldService.java文件是用上文的thrift命令生成的。

执行测试程序

使用maven编译

mvn package assembly:assembly

运行

在target目录下,运行时需要指定main class

java -cp thrift-test-0.1.0-SNAPSHOT-jar-with-dependencies.jar com.micmiu.thrift.demo.HelloServerDemo
java -cp thrift-test-0.1.0-SNAPSHOT-jar-with-dependencies.jar com.micmiu.thrift.demo.HelloClientDemo

原文地址:https://www.cnblogs.com/qiumingcheng/p/5273928.html