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. 【maven学习笔记】 01 初见

    想学maven,maven是ant的替代品. 1:下载 maven是apache的顶级项目,在http://maven.apache.org/可以直接下载. 2:环境变量 下载完要配置环境变量,把bi ...

  2. 利用Azure Automation实现云端自动化运维(4)

    在上述基本准备工作做完后,wo们看看如何实现利用Azure Automation实现定时自动开关机的操作,这种场景非常适合Dev/Test环境,因为Azure的虚拟机是按照分钟收费的,所以我们可以在开 ...

  3. Android之SurfaceView学习

    首先我们先来看下官方API对SurfaceView的介绍 SurfaceView的API介绍 Provides a dedicated drawing surface embedded inside ...

  4. PM产品经理练级攻略(1-5等级)

    大家都叫“PM”,但做的事情却完全不同? “PM”这个词到底是什么意思? 这个话题恐怕也是各位同行都一直在想,也一直想不清楚的吧,我也是. 每次看到各种“产品经理的能力模型”,我都觉得有点扯淡,总觉得 ...

  5. poj 1083 Moving Tables_dp

    题意:给你n个凳子,接着告诉你一个凳子从a房间到b房间,运输时间为10分钟,走廊很窄能通过一张凳子,当然不堵塞的话能同时扮凳子,问最小花费多少时间 因为数据很小就直接用数组统计了,a,b如果是奇数的话 ...

  6. Android二维码开源项目zxing编译

    ZXing是一个开放源代码的,用Java实现的多种格式的1D/2D条码图像处理库,它包括了联系到其它语言的port.Zxing能够实现使用手机的内置的摄像头完毕条形码的扫描及解码.该项目可实现的条形码 ...

  7. Oracle SecureFiles 说明(转)

    Oracle SecureFiles 说明 Oracle Database 11g 将LOB 数据类型作为Oracle SecureFiles 进行了完全重新设计,显著改进了应用程序开发的性能.可管理 ...

  8. CXF interceptor拦截顺序

    CXF Interceptor中Phase的先后顺序 org.apache.cxf.phase.PhaseManagerImpl中 final void createInPhases() { int  ...

  9. Position详解---转

    position有四个属性值: relative absolute fixed static 下面分别讲述这四个属性. 1. relative relative属性,相对定位,我们要搞清它是相对哪个对 ...

  10. 移动端的几款jq插件

    移动手机用户的数量每日都在增长,人们现在都习惯于使用手机来浏览网页,看小说,读新闻.如何确保你的网站对移动用户友好,是目前你需要解决的最重要的问 题之一.这里给大家介绍10款在移动手机上使用的jQue ...