Apache Thrift入门(安装、测试与java程序编写)
安装Apache Thrift
ubuntu linux运行:
- #!/bin/bash
- #下载
- wget http://mirrors.cnnic.cn/apache/thrift/0.9.1/thrift-0.9.1.tar.gz
- tar zxvf thrift-0.9.1.tar.gz
- cd thrift-0.9.1.tar.gz
- ./configure
- make
- make install
- #编译java依赖包
- cd lib/java
- 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
- namespace java com.micmiu.thrift.demo
- service HelloWorldService {
- string sayHello(1:string username)
- }
运行
thrift -gen java hello.thrift
将在同级目录下生成gen-java/com/micmiu/thrift/demo/HelloWorldService.java文件
编写测试程序
使用maven构建
pom.xml文件
- <?xml version="1.0" encoding="UTF-8"?>
- <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">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.micmiu.thrift.demo</groupId>
- <artifactId>thrift-test</artifactId>
- <version>0.1.0-SNAPSHOT</version>
- <dependencies>
- <strong> <dependency>
- <groupId>org.apache.thrift</groupId>
- <artifactId>libthrift</artifactId>
- <version>0.9.1</version>
- </dependency></strong>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- <version>1.5.8</version>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <artifactId>maven-assembly-plugin</artifactId>
- <version>2.2-beta-5</version>
- <configuration>
- <descriptorRefs>
- <descriptorRef>jar-with-dependencies</descriptorRef>
- </descriptorRefs>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>2.3.2</version>
- <configuration>
- <source>1.6</source>
- <target>1.6</target>
- <encoding>UTF-8</encoding>
- </configuration>
- </plugin>
- </plugins>
- </build>
- </project>
以下代码来自:http://www.micmiu.com/soa/rpc/thrift-sample/
HelloClientDemo.java
- package com.micmiu.thrift.demo;
- import org.apache.thrift.TException;
- import org.apache.thrift.protocol.TBinaryProtocol;
- import org.apache.thrift.protocol.TCompactProtocol;
- import org.apache.thrift.protocol.TJSONProtocol;
- import org.apache.thrift.protocol.TProtocol;
- import org.apache.thrift.transport.TSocket;
- import org.apache.thrift.transport.TTransport;
- import org.apache.thrift.transport.TTransportException;
- /**
- * blog http://www.micmiu.com
- *
- * @author Michael
- *
- */
- public class HelloClientDemo {
- public static final String SERVER_IP = "localhost";
- public static final int SERVER_PORT = 8090;
- public static final int TIMEOUT = 30000;
- /**
- *
- * @param userName
- */
- public void startClient(String userName) {
- TTransport transport = null;
- try {
- transport = new TSocket(SERVER_IP, SERVER_PORT, TIMEOUT);
- // 协议要和服务端一致
- TProtocol protocol = new TBinaryProtocol(transport);
- // TProtocol protocol = new TCompactProtocol(transport);
- // TProtocol protocol = new TJSONProtocol(transport);
- HelloWorldService.Client client = new HelloWorldService.Client(
- protocol);
- transport.open();
- String result = client.sayHello(userName);
- System.out.println("Thrify client result =: " + result);
- } catch (TTransportException e) {
- e.printStackTrace();
- } catch (TException e) {
- e.printStackTrace();
- } finally {
- if (null != transport) {
- transport.close();
- }
- }
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- HelloClientDemo client = new HelloClientDemo();
- client.startClient("Michael");
- }
- }
HelloServerDemo.java
- package com.micmiu.thrift.demo;
- import org.apache.thrift.TProcessor;
- import org.apache.thrift.protocol.TBinaryProtocol;
- import org.apache.thrift.protocol.TCompactProtocol;
- import org.apache.thrift.protocol.TJSONProtocol;
- import org.apache.thrift.protocol.TSimpleJSONProtocol;
- import org.apache.thrift.server.TServer;
- import org.apache.thrift.server.TSimpleServer;
- import org.apache.thrift.transport.TServerSocket;
- /**
- * blog http://www.micmiu.com
- *
- * @author Michael
- *
- */
- public class HelloServerDemo {
- public static final int SERVER_PORT = 8090;
- public void startServer() {
- try {
- System.out.println("HelloWorld TSimpleServer start ....");
- TProcessor tprocessor = new HelloWorldService.Processor<HelloWorldService.Iface>(
- new HelloWorldImpl());
- // HelloWorldService.Processor<HelloWorldService.Iface> tprocessor =
- // new HelloWorldService.Processor<HelloWorldService.Iface>(
- // new HelloWorldImpl());
- // 简单的单线程服务模型,一般用于测试
- TServerSocket serverTransport = new TServerSocket(SERVER_PORT);
- TServer.Args tArgs = new TServer.Args(serverTransport);
- tArgs.processor(tprocessor);
- tArgs.protocolFactory(new TBinaryProtocol.Factory());
- // tArgs.protocolFactory(new TCompactProtocol.Factory());
- // tArgs.protocolFactory(new TJSONProtocol.Factory());
- TServer server = new TSimpleServer(tArgs);
- server.serve();
- } catch (Exception e) {
- System.out.println("Server start error!!!");
- e.printStackTrace();
- }
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- HelloServerDemo server = new HelloServerDemo();
- server.startServer();
- }
- }
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程序编写)的更多相关文章
- apache thrift 入门(一)
1.简介 Apache Thrift软件框架,是用来开发可扩展的跨语言的软件服务.通过软件堆栈和代码生成引擎相结合的方式来构建服务,使C++, Java, Python, PHP, Ruby, Erl ...
- JAVA程序编写入门
在任意文件夹下创建一个文本,然后重命名,把文本后缀名改为.java.然后用eclipse打开此文件编写程序内容. public class nihao{ public static void main ...
- centos下apache thrift的安装
参考:http://running.iteye.com/blog/1983463 thrift-0.9.0安装 最好切换到root用户操作,避免不必要的麻烦. 进行例子程序tutorial目录下,通 ...
- Lucene入门-安装和运行Demo程序
Lucene版本:7.1 一.下载安装包 https://lucene.apache.org/core/downloads.html 二.安装 把4个必备jar包和路径添加到CLASSPATH \lu ...
- apache ab工具安装测试
1.安装 安装包下载地址: 将 httpd-2.2.29.tar.gz 解压到目录 /apps/install/httpd-2.2.29 ,这是我放置的位置 cd /apps/install/http ...
- Docker入门-构建第一个Java程序
定制镜像 准备一个没有第三方依赖的java web项目,可能参考示例maven结构项目: session-web.war 把该war上传到安装有docker软件的服务器上宿主目录下.在同级目录创建Do ...
- java 程序编写规则(自己总结)
1.命名规范 (1)所有的标示符都只能用ASCⅡ字母(A-Z或a-z).数字(0-9)和下划线"_". (2)类名是一个名词,采用大小写混合的方式,每个单词的首字母大写.例如:Us ...
- java程序编写需注意的问题
初学java,免不了很多注意事项 加分号 类名与文件名一致 javac fileName而非javac fileName.class ```java System.out.println(" ...
- 阶段5 3.微服务项目【学成在线】_day04 页面静态化_21-页面静态化-静态化测试-静态化程序编写
public String getPageHtml(String pageId){ /** * 静态化程序获取页面的DataUrl * * 静态化程序远程请求DataUrl获取数据模型 * * 静态化 ...
随机推荐
- hdu1753()模拟大型实景数字相加
标题信息: 手动求大的实数在一起, pid=1753">http://acm.hdu.edu.cn/showproblem.php?pid=1753 AC代码: /** *大实数相加 ...
- linux C连接mysql
linux 环境下C语言连接数据库首先要配置环境 1,确定你的linux下安装mysql我们可以做个测试. 打开你的终端,在终端下输入:service mysqld status [root@bogo ...
- QUdpSocket Class
翻译自:QT官网文档QUdpSocket类 QUdpSocket类提供一个UDP套接字. Header: #include <QUdpSocket> qmake: QT += networ ...
- live555从RTSP服务器读取数据到使用接收到的数据流程分析
本文在linux环境下编译live555工程,并用cgdb调试工具对live555工程中的testProgs目录下的openRTSP的执行过程进行了跟踪分析,直到将从socket端读取视频数据并保存为 ...
- mysql命令学习笔记(1):show table status like 'user';显示表的相关信息
show table status like 'user';显示表的相关信息 +------------+--------+---------+------------+------+-------- ...
- gauge.js的应用
最近项目要做个手机端的仪表盘,但是画风太给力,echarts.highcharts.D3等等都不能满足业务的需求,你懂的!开找,找到个gauge.js 下面简单介绍下这个插件官网http://bern ...
- Oozie — What Why and How
Oozie是什么? Oozie最初是Yahoo!为Hadoop开发的一个工作流调度器,一个工作流有多个Job组成.它允许用户提交由多个Job组成的工作流配置文件,这些Job既可以顺序执行,也可以并行执 ...
- 【常用小命令】解决windows下有些文件文件名识别不了导致删除不了的问题
在百度上找的解决方案哈,只为自己存档一份. 因为发现现在从csdn上下载的文件都是“.pdf_”格式,下载2个文件,将一个文件格式改成 “.pdf”,另一个文件就扔回不了回收站了, 所以没有办法就找各 ...
- mysql事物处理
mysql事物主要用于处理操作量大,复杂度高的数据.比如说,在人员管理系统中,你删除一个人员,你既要删除人员的基本资料,也要删除和该人员相关的信息,如信箱,文章等.这样,这些数据库操作语句就构成一个事 ...
- [转载]字符编码笔记:ASCII,Unicode和UTF-8
[转载] :http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf-8.html 1. ASCII码 在计算机内部,所有的信息最终都表 ...