Install

Go to thrift page download thrift.

1
2
3
4
brew install boost
./configure --without-python
sudo make
sudo make install

Maven

add depndency

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.9.1</version>
</dependency> <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.6</version>
</dependency> <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.6</version>
</dependency> <dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>

add plugin

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<plugin>
<groupId>org.apache.thrift.tools</groupId>
<artifactId>maven-thrift-plugin</artifactId>
<version>0.1.10</version>
<configuration>
<thriftExecutable>/usr/local/bin/thrift</thriftExecutable>
</configuration>
<executions>
<execution>
<id>thrift-sources</id>
<phase>generate-sources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>thrift-test-sources</id>
<phase>generate-test-sources</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>

Hello World

Now we can define our service.

first create the file test.thrift

1
2
3
4
5
6
namespace java com.app.testThrift
service Test{ void say(1: string word) }

We define a function say, then we run command to generate java class

1
thrift  --gen java test.thrfit

We copy the class to our java project. and implements the test.Iface interface

1
2
3
4
5
public class TestImpl implements Test.Iface {
public void say(String word) throws TException{
System.out.println("I am server, I want to say: " + word);
}
}

Now we build the service to let server say something.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class Server {
public void startServer() {
try { TServerSocket serverTransport = new TServerSocket(1234); Test.Processor process = new Processor(new TestImpl()); Factory portFactory = new TBinaryProtocol.Factory(true, true); Args args = new Args(serverTransport);
args.processor(process);
args.protocolFactory(portFactory); TServer server = new TThreadPoolServer(args);
server.serve();
} catch (TTransportException e) {
e.printStackTrace();
}
} public static void main(String[] args) {
Server server = new Server();
server.startServer();
}
}

We also should have a client to send word.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException; public class Client {
public void startClient() {
TTransport transport;
try {
transport = new TSocket("localhost", 1234);
TProtocol protocol = new TBinaryProtocol(transport);
Test.Client client = new Test.Client(protocol);
transport.open();
client.say(" Hello I am client");
transport.close();
} catch (TTransportException e) {
e.printStackTrace();
} catch (TException e) {
e.printStackTrace();
}
} public static void main(String[] args) {
Client client = new Client();
client.startClient();
}
}

Now when start server and run client, the message will show like this:

1
I am server, I want to say:  Hello I am client

Reference

Linux大棚版Thrift入门教程

Thrift java服务器与客户端示例

Thrift入门 (一)的更多相关文章

  1. 2016windows(10) wamp 最简单30分钟thrift入门使用讲解,实现php作为服务器和客户端的hello world

    2016最简单windows(10) wamp 30分钟thrift入门使用讲解,实现php作为服务器和客户端的hello world thrift是什么 最简单解释 thrift是用来帮助各个编程语 ...

  2. Thrift入门及Java实例演示<转载备用>

    Thrift入门及Java实例演示 作者: Michael 日期: 年 月 日 •概述 •下载配置 •基本概念 .数据类型 .服务端编码基本步骤 .客户端编码基本步骤 .数据传输协议 •实例演示(ja ...

  3. Thrift入门初探--thrift安装及java入门实例

    什么是thrift? 简单来说,是Facebook公布的一款开源跨语言的RPC框架. 那么问题来了. 什么是RPC框架? RPC全称为Remote Procedure Call,意为远程过程调用. 假 ...

  4. Thrift入门初探(2)--thrift基础知识详解

    昨天总结了thrift的安装和入门实例,Thrift入门初探--thrift安装及java入门实例,今天开始总结一下thrift的相关基础知识. Thrift使用一种中间语言IDL,来进行接口的定义, ...

  5. RPC远程协议之Thrift入门

    在上一篇文章<RPC远程协议之原理分析>中,我介绍了RPC的工作原理及欲实现RPC框架功能应该做哪些事情,因为要做的事情太多,完全由开发人员研发实现,不是很现实,所以市面上出现了诸多RPC ...

  6. 【thrift】thrift入门初探--thrift安装及java入门实例

    转载:https://www.cnblogs.com/fingerboy/p/6424248.html 公司的一些平台服务框架底层封装了thrift提供服务,最近项目不是很紧,于是研究了一下,刚刚入门 ...

  7. Thrift入门及Java实例演示

    目录: 概述 下载配置 基本概念 数据类型 服务端编码基本步骤 客户端编码基本步骤 数据传输协议 实例演示(java) thrift生成代码 实现接口Iface TSimpleServer服务模型 T ...

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

    安装Apache Thrift ubuntu linux运行: #!/bin/bash #下载 wget http://mirrors.cnnic.cn/apache/thrift/0.9.1/thr ...

  9. Thrift入门

    简介 Thrift最初由Facebook研发,主要用于各个服务之间的RPC通信,支持跨语言,常用的语言比如C++, Java, Python, PHP, Ruby, Erlang, Perl, Has ...

随机推荐

  1. 转——iptables详细配置

    基本原理及命令使用  http://my.oschina.net/hevakelcj/blog/313212 基础知识 Linux系统内核内建了netfilter防火墙机制.Netfilter(数据包 ...

  2. 水池(DFS)

    水池数目 点我 描述 南阳理工学院校园里有一些小河和一些湖泊,现在,我们把它们通一看成水池,假设有一张我们学校的某处的地图,这个地图上仅标识了此处是否是水池,现在,你的任务来了,请用计算机算出该地图中 ...

  3. MYSQL 插入二进制数的 2 种方法。

    方法 1.insert into TableName set column =''; 方法 2.insert into TableName .... values(.....); ---------- ...

  4. SSAS父子层次结构的增强-UnaryOperatorColumn属性

    上次我有讲到自定义汇总,这次的内容跟上次的差不多也算是自定义汇总,实现的方式不同而已!使用的是UnaryOperatorColumn属性. 这个属性说明: 一元运算符用于将成员自定义汇总到父级,汇总运 ...

  5. Elf 32

    [CentOS]安装软件:/ld-linux.so.2: ELF interpreter解决   环境: [orangle@localhost Downloads]$ uname -m&&am ...

  6. mysql 分区 按 PARTITION BY RANGE (TO_DAYS(startTime))

    to_days() Given a date date, returns a day number (the number of days since year 0). 给定一个date 日期,返回天 ...

  7. python官方文档

    Tutorialstart here Library Referencekeep this under your pillow Language Referencedescribes syntax a ...

  8. ubuntuOS

    OpenStack icehource for Ubuntu OS 1,网络规划 2,Ntp apt-get install ntp 3,generate secure passwd apt-get ...

  9. Problem "g++" ("gcc") not found in PATH [ in omnet++ ] ---- 关于OMNeT++软件使用问题

    出现的问题就像下面这样: 解释一下我出现这种情况的背景: 1. 首先安装好了OMNeT++软件,关于OMNeT++软件是否安装成功详见另一篇文章 OMNeT++安装教程 2. 也安装好了GCC编译环境 ...

  10. sort(水题)

    sort Time Limit: 6000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...