系统环境:
[email protected] ~/t/gen-cpp> lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 14.04.3 LTS Release: 14.04 Codename: trusty [email protected] ~/t/gen-cpp>
git:https://github.com/li-chunli/thrift_study
thrift官网: http://thrift.apache.org/
官方安装教程: http://thrift.apache.org/docs/install/debian
使用方法: http://wiki.apache.org/thrift/ThriftUsageC%2B%2B
安装依赖(如果不安装,编译也通过,但是用的时候各种问题,比如链接时找不到thfift库)
[email protected]:~# apt-get install automake bison flex g++ git libboost-all-dev libevent-dev libssl-dev libtool make pkg-config
下载thrift源代码:
[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 12 2015 thrift-0.9.3.tar.gz
解压:
[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
编译10分钟:
[email protected]:~/thrift/thrift-0.9.3$ make [email protected]:~/thrift/thrift-0.9.3$ echo $? 0
安装:
[email protected]:~/thrift/thrift-0.9.3$ sudo make install [sudo] password for chunli: [email protected]:~/thrift/thrift-0.9.3$ echo $? 0
检查:
[email protected]:~/thrift/thrift-0.9.3$ ll /usr/local/bin/thrift -rwxr-xr-x 1 root root 41M Oct 15 16:46 /usr/local/bin/thrift [email protected]:~/thrift/thrift-0.9.3$ /usr/local/bin/thrift -version Thrift version 0.9.3 [email protected]:~/thrift/thrift-0.9.3$ thrift -version Thrift version 0.9.3
测试:
1,Generating the server code
[email protected]:~/thrift$ vim test.thrift #!/usr/local/bin/thrift --gen cpp namespace cpp Test service Something { i32 ping() }
2,run thrift :
[email protected]:~/thrift$ thrift --gen cpp test.thrift [email protected]:~/thrift$ ll drwxrwxr-x 2 chunli chunli 4.0K Oct 15 16:50 gen-cpp -rw-rw-r-- 1 chunli chunli 90 Oct 15 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 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 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$
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依赖boost,安装boost库
[email protected]:~/thrift/gen-cpp$ sudo apt-get install libboost-all-dev thrift依赖libevent,安装libevent库 [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.o g++ -Wall -I/home/chunli/thrift/thrift-0.9.3/lib/cpp/src/ -c Something_server.cpp -o server.o g++ -Wall -I/home/chunli/thrift/thrift-0.9.3/lib/cpp/src/ -c test_constants.cpp -o constants.o g++ -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,看看结果:
[email protected]:~/thrift/gen-cpp$ ll total 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]
自己写一个客户端程序:
[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::protocol; 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$
编译,链接
[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
开始运行:
检查服务端的库文件:
[email protected]:~/thrift/gen-cpp$ ldd Something_server linux-vdso.so.1 => (0x00007ffcae577000) libthrift-0.9.3.so => not found libstdc++.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)
可以看到缺少一个库,查找库的路径:
[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
把库的路径添加到系统的路径中
[email protected]:~/thrift/gen-cpp# cat /etc/ld.so.conf.d/libc.conf # libc default configuration /usr/local/lib/ [email protected]:~/thrift/gen-cpp#
运行服务端 程序:
[email protected]:~/thrift/gen-cpp$ ./Something_server
查看网卡的监听:9090端口
[email protected]:~/thrift/gen-cpp# netstat -tnlp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 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#
运行客户端:
[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
服务端的输出:
[email protected]:~/thrift/gen-cpp$ ./Something_server ping ping ping ping
时间: 2024-12-20 14:34:49