安装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

Apache Thrift入门(安装、测试与java程序编写)的更多相关文章

  1. apache thrift 入门(一)

    1.简介 Apache Thrift软件框架,是用来开发可扩展的跨语言的软件服务.通过软件堆栈和代码生成引擎相结合的方式来构建服务,使C++, Java, Python, PHP, Ruby, Erl ...

  2. JAVA程序编写入门

    在任意文件夹下创建一个文本,然后重命名,把文本后缀名改为.java.然后用eclipse打开此文件编写程序内容. public class nihao{ public static void main ...

  3. centos下apache thrift的安装

    参考:http://running.iteye.com/blog/1983463  thrift-0.9.0安装 最好切换到root用户操作,避免不必要的麻烦. 进行例子程序tutorial目录下,通 ...

  4. Lucene入门-安装和运行Demo程序

    Lucene版本:7.1 一.下载安装包 https://lucene.apache.org/core/downloads.html 二.安装 把4个必备jar包和路径添加到CLASSPATH \lu ...

  5. apache ab工具安装测试

    1.安装 安装包下载地址: 将 httpd-2.2.29.tar.gz 解压到目录 /apps/install/httpd-2.2.29 ,这是我放置的位置 cd /apps/install/http ...

  6. Docker入门-构建第一个Java程序

    定制镜像 准备一个没有第三方依赖的java web项目,可能参考示例maven结构项目: session-web.war 把该war上传到安装有docker软件的服务器上宿主目录下.在同级目录创建Do ...

  7. java 程序编写规则(自己总结)

    1.命名规范 (1)所有的标示符都只能用ASCⅡ字母(A-Z或a-z).数字(0-9)和下划线"_". (2)类名是一个名词,采用大小写混合的方式,每个单词的首字母大写.例如:Us ...

  8. java程序编写需注意的问题

    初学java,免不了很多注意事项 加分号 类名与文件名一致 javac fileName而非javac fileName.class ```java System.out.println(" ...

  9. 阶段5 3.微服务项目【学成在线】_day04 页面静态化_21-页面静态化-静态化测试-静态化程序编写

    public String getPageHtml(String pageId){ /** * 静态化程序获取页面的DataUrl * * 静态化程序远程请求DataUrl获取数据模型 * * 静态化 ...

随机推荐

  1. textField 判断输入长度限制

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementS ...

  2. 了解Serialization

    对象的串行化(Serialization) 一.串行化的概念和目的 1.什么是串行化 对象的寿命通常随着生成该对象的程序的终止而终止.有时候,可能需要将对象的状态保存下来,在需要时再将对象恢复.我们把 ...

  3. 具体解释EBS接口开发之物料导入API

    create_item inv_item_grp.create_item(p_commit => fnd_api.g_true, -- p_item_rec => l_item_rec, ...

  4. unity tips

    1.在unity 的mecanim中,如果一个动画指向两个或两个以上的动画,那么在inspector中,transitions中可以看到所有的过渡路径,这些路径是有先后顺序的.

  5. vlc-android对于通过Live555接收到音视频数据包后的处理分析

    通过ndk-gdb跟踪调试vlc-android来分析从连接到RTSP服务器并接收到音视频数据包后的处理过程. 首先,从前面的文章有分析过vlc-android的处理过程通过线程函数Run()(Src ...

  6. HTML5新增的主体元素和新增的非主体结构元素

    HTML5新增的主体元素 article元素 article元素表示文档.页面或应用程序中独立的.完整的.可以独自被外部引用的内容.它可以是一篇博客或者报刊中的文章,一篇论坛帖子.一段用户评论或独立的 ...

  7. 疯狂Android第一章:Android环境配置以及基本概念

    第一章 无关痒痛:Android Studio安装,配置,基本功能介绍! 重点内容:Android应用基本结构分析. 基础概念部分(只需知道作用,原理后见代码): Activity:安卓系统中负责与用 ...

  8. Android 自动更新 + IIS7 添加APK mime

    如果APK文件放在IIS下面需要添加APK的mime,否则会出现下面错误 可以在IIS上添加mime映射 .apk application/vnd.android   下面内容转自:http://ww ...

  9. USACO 1.3... 虫洞 解题报告(搜索+强大剪枝+模拟)

    这题可真是又让我找到了八数码的感觉...哈哈. 首先,第一次见题,没有思路,第二次看题,感觉是搜索,就这样写下来了. 这题我几乎是一个点一个点改对的(至于为什么是这样,后面给你看一个神奇的东西),让我 ...

  10. 单光纤udp通信

    环境:      两块板子,拥有独立系统(Linux),通过单光纤连接(数据只能单向发送,无反馈).两块板子采用udp协议通信. 问题: 发送板子发送数据后,接收板子上的进程收不到数据. 确认两块光纤 ...