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

时间:2022-01-27 06:42:48

安装Apache Thrift

ubuntu linux运行:

  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

  1. namespace java com.micmiu.thrift.demo
  2. service  HelloWorldService {
  3. string sayHello(1:string username)
  4. }

运行

thrift -gen java hello.thrift

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

编写测试程序

使用maven构建

pom.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.micmiu.thrift.demo</groupId>
  5. <artifactId>thrift-test</artifactId>
  6. <version>0.1.0-SNAPSHOT</version>
  7. <dependencies>
  8. <strong>    <dependency>
  9. <groupId>org.apache.thrift</groupId>
  10. <artifactId>libthrift</artifactId>
  11. <version>0.9.1</version>
  12. </dependency></strong>
  13. <dependency>
  14. <groupId>org.slf4j</groupId>
  15. <artifactId>slf4j-log4j12</artifactId>
  16. <version>1.5.8</version>
  17. </dependency>
  18. </dependencies>
  19. <build>
  20. <plugins>
  21. <plugin>
  22. <artifactId>maven-assembly-plugin</artifactId>
  23. <version>2.2-beta-5</version>
  24. <configuration>
  25. <descriptorRefs>
  26. <descriptorRef>jar-with-dependencies</descriptorRef>
  27. </descriptorRefs>
  28. </configuration>
  29. </plugin>
  30. <plugin>
  31. <groupId>org.apache.maven.plugins</groupId>
  32. <artifactId>maven-compiler-plugin</artifactId>
  33. <version>2.3.2</version>
  34. <configuration>
  35. <source>1.6</source>
  36. <target>1.6</target>
  37. <encoding>UTF-8</encoding>
  38. </configuration>
  39. </plugin>
  40. </plugins>
  41. </build>
  42. </project>

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

HelloClientDemo.java

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

HelloServerDemo.java

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

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