首页 > 代码库 > linux安装thrift库
linux安装thrift库
系统环境:
chunli@linux ~/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 chunli@linux ~/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库)
root@linux:~# apt-get install automake bison flex g++ git libboost-all-dev libevent-dev libssl-dev libtool make pkg-config
下载thrift源代码:
chunli@linux:~$ mkdir thrift chunli@linux:~$ cd thrift/ chunli@linux:~/thrift$ wget http://mirrors.tuna.tsinghua.edu.cn/apache/thrift/0.9.3/thrift-0.9.3.tar.gz chunli@linux:~/thrift$ ll -rw-rw-r-- 1 chunli chunli 8.5M Oct 12 2015 thrift-0.9.3.tar.gz
解压:
chunli@linux:~/thrift$ tar xf thrift-0.9.3.tar.gz chunli@linux:~/thrift$ cd thrift-0.9.3/ chunli@linux:~/thrift/thrift-0.9.3$ chunli@linux:~/thrift/thrift-0.9.3$ ./configure chunli@linux:~/thrift/thrift-0.9.3$ echo $? 0
编译10分钟:
chunli@linux:~/thrift/thrift-0.9.3$ make chunli@linux:~/thrift/thrift-0.9.3$ echo $? 0
安装:
chunli@linux:~/thrift/thrift-0.9.3$ sudo make install [sudo] password for chunli: chunli@linux:~/thrift/thrift-0.9.3$ echo $? 0
检查:
chunli@linux:~/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 chunli@linux:~/thrift/thrift-0.9.3$ /usr/local/bin/thrift -version Thrift version 0.9.3 chunli@linux:~/thrift/thrift-0.9.3$ thrift -version Thrift version 0.9.3
测试:
1,Generating the server code
chunli@linux:~/thrift$ vim test.thrift #!/usr/local/bin/thrift --gen cpp namespace cpp Test service Something { i32 ping() }
2,run thrift :
chunli@linux:~/thrift$ thrift --gen cpp test.thrift chunli@linux:~/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
chunli@linux:~/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 chunli@linux:~/thrift$
4,Implementing the Server
chunli@linux:~/thrift$ cd gen-cpp/ chunli@linux:~/thrift/gen-cpp$ cp Something_server.skeleton.cpp Something_server.cpp chunli@linux:~/thrift/gen-cpp$
5, find lib_path
root@linux:~# find / -name TDispatchProcessor.h /home/chunli/thrift/thrift-0.9.3/lib/cpp/src/thrift/TDispatchProcessor.h
6,Compiling
6.1 thrift依赖boost,安装boost库
chunli@linux:~/thrift/gen-cpp$ sudo apt-get install libboost-all-dev thrift依赖libevent,安装libevent库 chunli@linux:~/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,看看结果:
chunli@linux:~/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 chunli@linux:~/thrift/gen-cpp$
[Writing the client code]
自己写一个客户端程序:
chunli@linux:~/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; } chunli@linux:~/thrift/gen-cpp$
编译,链接
chunli@linux:~/thrift/gen-cpp$ g++ -Wall -I/usr/local/include/thrift/ -c Something_client.cpp -o client.o chunli@linux:~/thrift/gen-cpp$ g++ -L/usr/local/lib client.o something.o constants.o types.o -o Something_client -lthrift
开始运行:
检查服务端的库文件:
chunli@linux:~/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)
可以看到缺少一个库,查找库的路径:
root@linux:~/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
把库的路径添加到系统的路径中
root@linux:~/thrift/gen-cpp# cat /etc/ld.so.conf.d/libc.conf # libc default configuration /usr/local/lib/ root@linux:~/thrift/gen-cpp#
运行服务端 程序:
chunli@linux:~/thrift/gen-cpp$ ./Something_server
查看网卡的监听:9090端口
root@linux:~/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 root@linux:~/thrift/gen-cpp#
运行客户端:
chunli@linux ~/t/gen-cpp> ./Something_client chunli@linux ~/t/gen-cpp> ./Something_client chunli@linux ~/t/gen-cpp> ./Something_client chunli@linux ~/t/gen-cpp> ./Something_client
服务端的输出:
chunli@linux:~/thrift/gen-cpp$ ./Something_server ping ping ping ping
本文出自 “魂斗罗” 博客,谢绝转载!
linux安装thrift库
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。