System environment:
[Email protected] ~/t/gen-cpp> Lsb_release-ano LSB modules is available. Distributor ID:UbuntuDescription:Ubuntu 14.04.3 Ltsrelease:14.04codename:trusty[email protected] ~/t/gen-cpp>
git: https://github.com/li-chunli/thrift_study
Thrift Official Website:http://thrift.apache.org/
Official installation Tutorial:Http://thrift.apache.org/docs/install/debian
How to use:Http://wiki.apache.org/thrift/ThriftUsageC%2B%2B
Installation dependencies (if not installed, compilation also passes, but when using various problems, such as the link can not find the Thfift library)
[Email protected]:~# apt-get Install Automake Bison flex g++ git libboost-all-dev libevent-dev libssl-dev libtool make pkg -config
Download Thrift Source Code:
[Email protected]:~$ mkdir thrift[email protected]:~$ cd thrift/[email protected]:~/thrift$ wget/http/ Mirrors.tuna.tsinghua.edu.cn/apache/thrift/0.9.3/thrift-0.9.3.tar.gz[email protected]:~/thrift$ ll-rw-rw-r--1 Chunli Chunli 8.5M Oct thrift-0.9.3.tar.gz
Extract:
[Email protected]:~/thrift$ tar XF thrift-0.9.3.tar.gz [email protected]:~/thrift$ cd Thrift-0.9.3/[email protected]:~/ thrift/thrift-0.9.3$ [email protected]:~/thrift/thrift-0.9.3$./configure[email protected]:~/thrift/thrift-0.9.3$ Echo $?0
Compile for 10 minutes:
[Email protected]:~/thrift/thrift-0.9.3$ make[email protected]:~/thrift/thrift-0.9.3$ Echo $?0
Installation:
[Email protected]:~/thrift/thrift-0.9.3$ sudo make install [sudo] password for chunli: [Email protected]:~/thrift/ thrift-0.9.3$ Echo $?0
Check:
[Email protected]:~/thrift/thrift-0.9.3$ ll/usr/local/bin/thrift-rwxr-xr-x 1 root root 41M Oct 16:46/usr/local/bin/t Hrift[email protected]:~/thrift/thrift-0.9.3$/usr/local/bin/thrift-versionthrift version 0.9.3[email protected]:~/ thrift/thrift-0.9.3$ Thrift-versionthrift version 0.9.3
Test:
1,generating the server code
[Email protected]:~/thrift$ vim test.thrift #!/usr/local/bin/thrift--gen cppnamespace cpp testservice Something {i32 pi Ng ()}
2,run Thrift:
[Email protected]:~/thrift$ Thrift--gen CPP test.thrift [email protected]:~/thrift$ lldrwxrwxr-x 2 Chunli Chunli 4.0K Oc T 16:50 gen-cpp-rw-rw-r--1 Chunli chunli Oct 16:49 test.thrift
3,exploring the generated code-the Server
[Email protected]:~/thrift$ ll gen-cpp/total 44k-rw-rw-r--1 Chunli chunli 11K Oct 16:50 something.cpp-rw-rw-r--1 chu Nli Chunli 8.1K Oct 16:50 something.h-rw-rw-r--1 chunli chunli 1.3K Oct 16:50 something_server.skeleton.cpp-rw-rw-r --1 Chunli Chunli 276 Oct 16:50 test_constants.cpp-rw-rw-r--1 chunli Chunli 356 Oct 16:50 TEST_CONSTANTS.H-RW-RW -r--1 Chunli Chunli 268 Oct All 16:50 test_types.cpp-rw-rw-r--1 Chunli chunli 427 Oct 16:50 test_types.h[email prote cted]:~/thrift$
4,implementing the Server
[Email protected]:~/thrift$ cd gen-cpp/[email protected]:~/thrift/gen-cpp$ CP Something_server.skeleton.cpp Something _server.cpp [Email protected]:~/thrift/gen-cpp$
5, Find Lib_path
[Email protected]:~# Find/-name tdispatchprocessor.h/home/chunli/thrift/thrift-0.9.3/lib/cpp/src/thrift/ TDispatchProcessor.h
6,compiling
6.1 Thrift relies on boost to install the Boost library
[Email protected]:~/thrift/gen-cpp$ sudo apt-get install Libboost-all-dev thrift rely on libevent, install libevent library [email protected]:~/thrift/gen-cpp$ sudo apt-get install Libevent-dev
6.2
g++-wall-i/home/chunli/thrift/thrift-0.9.3/lib/cpp/src/-C something.cpp-o something.og++-wall-i/home/chunli/ thrift/thrift-0.9.3/lib/cpp/src/-C Something_server.cpp-o server.og++-wall-i/home/chunli/thrift/thrift-0.9.3/lib /cpp/src/-C Test_constants.cpp-o constants.og++-wall-i/home/chunli/thrift/thrift-0.9.3/lib/cpp/src/-C test_ Types.cpp-o TYPES.O
7,linking
g++-l/usr/local/lib *.o-o Something_server-lthrift
8, look at the results:
[email protected]:~/thrift/gen-cpp$ lltotal 820k-rw-rw-r-- 1 chunli chunli 4.3k oct 15 20:53 constants.o-rw-rw-r-- 1 chunli chunli 332k oct 15 20:53 server.o-rw-rw-r-- 1 chunli chunli 11k oct 15 16:50 something.cpp-rw-rw-r-- 1 chunli chunli 8.1k oct 15 16:50 Something.h-rw-rw-r-- 1 chunli chunli 228K Oct 15 20:53 Something.o-rwxrwxr-x 1 chunli chunli 203k oct 15 20:53 something_ Server-rw-rw-r-- 1 chunli chunli 1.3k oct 15 17:52 something_ Server.cpp-rw-rw-r-- 1 chunli chunli 1.3k oct 15 16:50 something_ server.skeleton.cpp-rw-rw-r-- 1 chunli chunli 276 oct 15 16:50 Test_constants.cpp-rw-rw-r-- 1 chUnli chunli 356 oct 15 16:50 test_constants.h-rw-rw-r-- 1 chunli chunli 268 Oct 15 16:50 test_types.cpp-rw-rw-r-- 1 chunli chunli 427 oct 15 16:50 test_types.h[email protected]:~/thrift/gen-cpp$
[Writing the client code]
Write a client program yourself:
[Email protected]:~/thrift/gen-cpp$ vim something_client.cpp#include "Something.h"//As an Example#include < transport/tsocket.h> #include <transport/TBufferTransports.h> #include <protocol/TBinaryProtocol.h> Using namespace apache::thrift;using namespace Apache::thrift::p rotocol;using namespace Apache::thrift::transport; Using namespace Test;int main (int argc, char **argv) {boost::shared_ptr<tsocket> socket (new Tsocket ("localhost", 9090));boost::shared_ptr<ttransport> Transport (new Tbufferedtransport (socket));boost::shared_ptr< Tprotocol> Protocol (new Tbinaryprotocol (transport)); Somethingclient Client (protocol); Transport->open (); client.ping (); Transport->close (); return 0;} [Email protected]:~/thrift/gen-cpp$
Compile, link
[Email protected]:~/thrift/gen-cpp$ g++-wall-i/usr/local/include/thrift/-C something_client.cpp-o client.o[email protected]:~/thrift/gen-cpp$ g++-l/usr/local/lib client.o something.o constants.o types.o-o something_client-lthrift
Start running:
Check the server-side library file:
[email protected]:~/thrift/gen-cpp$ ldd something_serverlinux-vdso.so.1 = (0x00007ffcae577000) libthrift-0.9.3.so = Not foundlibstdc++.so.6 =/usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fa496d98000) Libgcc_s.so.1 =/lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fa496b82000) libc.so.6 =/lib/x86_64-linux-gnu/ Libc.so.6 (0x00007fa4967bd000) libm.so.6 =/lib/x86_64-linux-gnu/libm.so.6 (0x00007fa4964b7000)/lib64/ Ld-linux-x86-64.so.2 (0x00007fa49709c000)
You can see that a library is missing, and find the path to the library:
[Email protected]:~/thrift/gen-cpp# Find/-name "*libthrift-0.9.3.so*"/usr/local/lib/libthrift-0.9.3.so/home/ Chunli/thrift/thrift-0.9.3/lib/cpp/.libs/libthrift-0.9.3.so
Add the path of the library to the path of the system
[Email protected]:~/thrift/gen-cpp# cat/etc/ld.so.conf.d/libc.conf # libc default Configuration/usr/local/lib/[email protected]:~/thrift/gen-cpp#
To run the service-side program:
[Email protected]:~/thrift/gen-cpp$./something_server
Viewing the network card's listening: 9090 port
[email protected]:~/thrift/gen-cpp# netstat -tnlpactive internet connections ( Only servers) proto recv-q send-q local address foreign address state PID/Program nametcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN 801/vsftpd tcp 0 0 0.0.0.0:22 0.0.0.0:* listen 950/sshd tcp 0 0 127.0.0.1:3306 0.0.0.0:* listen 1103/mysqld tcp6 0 0 :::22 :::* LISTEN 950/sshd tcp6 0 0 :::9090 :::* listen 23737/something_ser[email protected]:~/thrift/gen-cpp#
To run the client:
[Email protected] ~/t/gen-cpp>/something_client [email protected] ~/t/gen-cpp>./something_client [Email Protected] ~/t/gen-cpp>./something_client [email protected] ~/t/gen-cpp>./something_client
Output from the service side:
[Email protected]:~/thrift/gen-cpp$./something_server pingpingpingping
This article from "Soul Bucket Luo" blog, declined reprint!
Linux Installation Thrift Library