Thrift分析#001#apt安装指南

Thrift在Ubuntu/Debian系统上的安装一般有两种方式:

  • 一是APT仓库安装,简单方便,但这种方式安装的软件并非官方最新版本,版本更新有一定延迟;
  • 二是从源代码编译安装,可以编译使用最新特性版本,但过程略显复杂,需要有一定的技术功底;

读者自行选择某种方法,本文介绍apt安装方法。

安装

参考安装指南:
https://thrift.apache.org/docs/install/debian.html
https://d3s.mff.cuni.cz/files/teaching/nswi080/labs/Files/prepare-2thrift-en.html


第一步,首先使用apt search命令查找thrift的相关软件包名称,使用命令:

1
$ apt-cache search thrift

查询结果如下:

其中
thrift-compiler 是 thrift idl文件的编译器,用于翻译ID文件到代码。
libthrift-dev 是 thrift c++ 版本的开发库。
至于这两个包包含的具体内容,后面我们再做分析。下面我们开始使用apt安装。


第二步,安装thrift-compiler包,使用命令:

1
$ sudo apt install thrift-compiler

安装输出:


第三步,安装libthrift-dev包,使用命令:

1
$ sudo apt install libthrift-dev

安装输出:


经过上述三步安装,如果没有报错,说明我们正确安装了thrift的开发环境,后续我们将简单测试下thrift。

测试

首先安装依赖库libboost和libevent,使用如下命令:

1
2
$ sudo apt install libboost-dev
$ sudo apt install libevent-dev

下面我们基于Thrift开发框架,编写一个简单的Echo服务来测试下Thrif框架。

第一步,编写idl文件

1
2
3
4
5
namespace cpp echo

service EchoService {
string TestEcho(1:string sInput)
}

保存上述内容到文本文件 echo.thrift。


第二步,在服务器上使用thrift编译器编译echo.thrift,来生成文件。使用命令如下:

1
$ thrift -out . --gen cpp echo.thrift


第三步,修改EchoService_server.skeleton.cpp文件中的TestEcho函数,对返回值进行赋值。如下图:


第四步,编写客户端代码,将如下内容保存为EchoClient.cpp。

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
#include <iostream>
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/transport/TTransportUtils.h>
#include <thrift/transport/TSocket.h>
#include "EchoService.h"

using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;

int main(int argc, const char ** argv)
{
std::shared_ptr<TSocket> socket = std::shared_ptr<TSocket>(new TSocket("127.0.0.1", 9090));
std::shared_ptr<TTransport> transport = std::make_shared<TBufferedTransport>(socket);
std::shared_ptr<TProtocol> protocol = std::make_shared<TBinaryProtocol>(transport);

transport->open();

echo::EchoServiceClient testClient(protocol);

std::string sOut;
testClient.TestEcho(sOut, argv[1]);

std::cout << sOut << std::endl;

return 0;
}

第五步,编写Makefile文件,将如下内容保存为Makefile。

1
2
3
4
5
6
7
8
9
10
all:echoserver echoclient

echoserver: EchoService.cpp EchoService_server.skeleton.cpp
g++ -o echoserver EchoService.cpp EchoService_server.skeleton.cpp /usr/lib/x86_64-linux-gnu/libthrift.a

echoclient: EchoClient.cpp
g++ -o echoclient EchoClient.cpp EchoService.cpp /usr/lib/x86_64-linux-gnu/libthrift.a

clean:
rm -rf *.o echoclient echoserver

第六步,编译生成echoclient和echoserver,使用命令:

1
$ make

执行完毕,在当前目录下文件如下图所示:


第七步,启动echoserver,然后使用echoclient向服务端发送请求。
服务端启动命令:

1
$ ./echoserver

客户端调用命令:

1
$ ./echoclient hello

客户端输出内容如下:


至此,我们使用echo服务测试thrift框架成功。
测试代码下载地址:

thrift-compiler 包内容分析

首先看下 thrift-compiler 软件包有哪些内容,使用命令:

1
$ dpkg -L thrift-compiler

输入如下:

该软件包中主要包含了例子、手册以及最重要的编译器可执行程序 /usr/bin/thrift

libthrift-dev 包内容分析

我们再看下 libthrift-dev 软件包有哪些内容,使用命令:

1
$ dpkg -L libthrift-dev

该软件包主要包含了头文件、库等信息,由于内容较多,我们逐段分析,可以在这里查看全部输出( 右键另存为 )。

第一部分

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/usr/include/thrift/TApplicationException.h
/usr/include/thrift/TBase.h
/usr/include/thrift/TConfiguration.h
/usr/include/thrift/TDispatchProcessor.h
/usr/include/thrift/TLogging.h
/usr/include/thrift/TNonCopyable.h
/usr/include/thrift/TOutput.h
/usr/include/thrift/TProcessor.h
/usr/include/thrift/TToString.h
/usr/include/thrift/Thrift.h
/usr/include/thrift/async
/usr/include/thrift/async/TAsyncBufferProcessor.h
/usr/include/thrift/async/TAsyncChannel.h
/usr/include/thrift/async/TAsyncDispatchProcessor.h
/usr/include/thrift/async/TAsyncProcessor.h
/usr/include/thrift/async/TAsyncProtocolProcessor.h
/usr/include/thrift/async/TConcurrentClientSyncInfo.h
/usr/include/thrift/async/TEvhttpClientChannel.h
/usr/include/thrift/async/TEvhttpServer.h
/usr/include/thrift/concurrency
/usr/include/thrift/concurrency/Exception.h
/usr/include/thrift/concurrency/FunctionRunner.h
/usr/include/thrift/concurrency/Monitor.h
/usr/include/thrift/concurrency/Mutex.h
/usr/include/thrift/concurrency/Thread.h
/usr/include/thrift/concurrency/ThreadFactory.h
/usr/include/thrift/concurrency/ThreadManager.h
/usr/include/thrift/concurrency/TimerManager.h
/usr/include/thrift/config.h
/usr/include/thrift/processor
/usr/include/thrift/processor/PeekProcessor.h
/usr/include/thrift/processor/StatsProcessor.h
/usr/include/thrift/processor/TMultiplexedProcessor.h
/usr/include/thrift/protocol
/usr/include/thrift/protocol/TBase64Utils.h
/usr/include/thrift/protocol/TBinaryProtocol.h
/usr/include/thrift/protocol/TBinaryProtocol.tcc
/usr/include/thrift/protocol/TCompactProtocol.h
/usr/include/thrift/protocol/TCompactProtocol.tcc
/usr/include/thrift/protocol/TDebugProtocol.h
/usr/include/thrift/protocol/TEnum.h
/usr/include/thrift/protocol/THeaderProtocol.h
/usr/include/thrift/protocol/TJSONProtocol.h
/usr/include/thrift/protocol/TList.h
/usr/include/thrift/protocol/TMap.h
/usr/include/thrift/protocol/TMultiplexedProtocol.h
/usr/include/thrift/protocol/TProtocol.h
/usr/include/thrift/protocol/TProtocolDecorator.h
/usr/include/thrift/protocol/TProtocolException.h
/usr/include/thrift/protocol/TProtocolTap.h
/usr/include/thrift/protocol/TProtocolTypes.h
/usr/include/thrift/protocol/TSet.h
/usr/include/thrift/protocol/TVirtualProtocol.h
/usr/include/thrift/qt
/usr/include/thrift/qt/TQIODeviceTransport.h
/usr/include/thrift/qt/TQTcpServer.h
/usr/include/thrift/server
/usr/include/thrift/server/TConnectedClient.h
/usr/include/thrift/server/TNonblockingServer.h
/usr/include/thrift/server/TServer.h
/usr/include/thrift/server/TServerFramework.h
/usr/include/thrift/server/TSimpleServer.h
/usr/include/thrift/server/TThreadPoolServer.h
/usr/include/thrift/server/TThreadedServer.h
/usr/include/thrift/thrift-config.h
/usr/include/thrift/thrift_export.h
/usr/include/thrift/transport
/usr/include/thrift/transport/PlatformSocket.h
/usr/include/thrift/transport/SocketCommon.h
/usr/include/thrift/transport/TBufferTransports.h
/usr/include/thrift/transport/TFDTransport.h
/usr/include/thrift/transport/TFileTransport.h
/usr/include/thrift/transport/THeaderTransport.h
/usr/include/thrift/transport/THttpClient.h
/usr/include/thrift/transport/THttpServer.h
/usr/include/thrift/transport/THttpTransport.h
/usr/include/thrift/transport/TNonblockingSSLServerSocket.h
/usr/include/thrift/transport/TNonblockingServerSocket.h
/usr/include/thrift/transport/TNonblockingServerTransport.h
/usr/include/thrift/transport/TPipe.h
/usr/include/thrift/transport/TPipeServer.h
/usr/include/thrift/transport/TSSLServerSocket.h
/usr/include/thrift/transport/TSSLSocket.h
/usr/include/thrift/transport/TServerSocket.h
/usr/include/thrift/transport/TServerTransport.h
/usr/include/thrift/transport/TShortReadTransport.h
/usr/include/thrift/transport/TSimpleFileTransport.h
/usr/include/thrift/transport/TSocket.h
/usr/include/thrift/transport/TSocketPool.h
/usr/include/thrift/transport/TSocketUtils.h
/usr/include/thrift/transport/TTransport.h
/usr/include/thrift/transport/TTransportException.h
/usr/include/thrift/transport/TTransportUtils.h
/usr/include/thrift/transport/TVirtualTransport.h
/usr/include/thrift/transport/TWebSocketServer.h
/usr/include/thrift/transport/TZlibTransport.h

上述为开发使用的头文件。


第二部分

1
2
3
4
/usr/lib/x86_64-linux-gnu/libthrift.a
/usr/lib/x86_64-linux-gnu/libthriftnb.a
/usr/lib/x86_64-linux-gnu/libthriftqt5.a
/usr/lib/x86_64-linux-gnu/libthriftz.a

上述文件为静态库。


第三部分

1
2
3
4
/usr/lib/x86_64-linux-gnu/pkgconfig/thrift-nb.pc
/usr/lib/x86_64-linux-gnu/pkgconfig/thrift-qt5.pc
/usr/lib/x86_64-linux-gnu/pkgconfig/thrift-z.pc
/usr/lib/x86_64-linux-gnu/pkgconfig/thrift.pc

上述文件为pkg-config命令所用文件,用于获得thrift开发库所有编译相关的信息。示例如下:


第四部分

1
2
3
4
/usr/lib/x86_64-linux-gnu/libthrift.so
/usr/lib/x86_64-linux-gnu/libthriftnb.so
/usr/lib/x86_64-linux-gnu/libthriftqt5.so
/usr/lib/x86_64-linux-gnu/libthriftz.so

上述文件为动态库。