CentOS 6.6 Thrift 环境搭建
2015-07-14 13:07
543 查看
在做一个多台机器远程调用的东西,用到了thrift,使用C++语言。
yum update
yum -y install automake libtool flex bison pkgconfig gcc-c++ boost-devel libevent-devel zlib-devel python-devel ruby-devel crypto-utils openssl openssl-devel
#下载thrift
git clone https://git-wip-us.apache.org/repos/asf/thrift.git
cd thrift./bootstrap.sh
./configure --with-lua=no
./configure --with-boost=/usr/include/ 这里猜想是因为boost安装在/usr/include里
make
sudo make install
运行编译好的时有可能找不到相关的库,因此将相关的库(即.so文件,默认在/usr/local/lib中)处理
# cat /etc/ld.so.conf
include ld.so.conf.d/*.conf
# echo "/usr/local/lib" >> /etc/ld.so.conf
# ldconfig
下面是在网上找的资料,主要参考点是其中的如何编写一个客户端服务器模式的小程序,以及如何编译运行
网址是 http://wiki.apache.org/thrift/ThriftUsageC++
Thrift library files
Thrift header files.
Download a snapshot of Thrift and extract if you haven't done so already - Direct
Link
Navigate to lib/cpp using the terminal of your choice
Run make to compile
Run make install to install the library. Your user needs root permissions.
Now run:
The generated code should be under the gen-cpp directory. You will see a number of generated C++ and header files along with an automatically generated server skeleton code (in bold).
Something.cpp
Something.h
Something_server.skeleton.cpp
your_thrift_file_constants.cpp
your_thrift_file_constants.h
your_thrift_file_types.cpp
your_thrift_file_types.h
When this server is run in console it prints "ping" on the console window each time the function is called from a client.
Here's the autogenerated skeleton file to illustrate how to write a server: Something_server.cpp
Toggle line numbers
Toggle line numbers
connections while throttling the processor threads using a pool.
* TNonblockingServer with a thread pool is the c++ alternative of the JAVA THsHaServer; * TNonblockingServer withOUT a thread pool is the c++ alternative
of the JAVA TNonblockingServer;
Server code with thread pool:
C++ client code (you have to use TFramedTransport here):
PHP client code (you have to use TFramedTransport here):
yum update
yum -y install automake libtool flex bison pkgconfig gcc-c++ boost-devel libevent-devel zlib-devel python-devel ruby-devel crypto-utils openssl openssl-devel
#下载thrift
git clone https://git-wip-us.apache.org/repos/asf/thrift.git
cd thrift./bootstrap.sh
./configure --with-lua=no
./configure --with-boost=/usr/include/ 这里猜想是因为boost安装在/usr/include里
make
sudo make install
运行编译好的时有可能找不到相关的库,因此将相关的库(即.so文件,默认在/usr/local/lib中)处理
# cat /etc/ld.so.conf
include ld.so.conf.d/*.conf
# echo "/usr/local/lib" >> /etc/ld.so.conf
# ldconfig
下面是在网上找的资料,主要参考点是其中的如何编写一个客户端服务器模式的小程序,以及如何编译运行
网址是 http://wiki.apache.org/thrift/ThriftUsageC++
getting started
The first thing you need to know is that the C++ code generated by Thrift compiles only on Unix based systems, although some success has been reported using Cygwin on Win32 in ThriftInstallationWin32.Requirements
Make sure that your system meets the requirements as noted in ThriftRequirementsThrift library files
Thrift header files.
Installing the Thrift library
Installing the Thrift library is trivial to link with the generated code.Download a snapshot of Thrift and extract if you haven't done so already - Direct
Link
Navigate to lib/cpp using the terminal of your choice
Run make to compile
Run make install to install the library. Your user needs root permissions.
Generating the server code
In this example we use an imaginary file called your_thrift_file.thrift:#!/usr/local/bin/thrift --gen cpp namespace cpp Test service Something { i32 ping() }
Now run:
thrift --gen cpp your_thrift_file.thrift
Exploring the generated code - The Ser
4000
ver
The generated code should be under the gen-cpp directory. You will see a number of generated C++ and header files along with an automatically generated server skeleton code (in bold).Something.cpp
Something.h
Something_server.skeleton.cpp
your_thrift_file_constants.cpp
your_thrift_file_constants.h
your_thrift_file_types.cpp
your_thrift_file_types.h
Implementing the Server
Copy the generated server skeleton to a file named Something_server.cpp and keep the original:cp Something_server.skeleton.cpp Something_server.cpp
When this server is run in console it prints "ping" on the console window each time the function is called from a client.
Here's the autogenerated skeleton file to illustrate how to write a server: Something_server.cpp
Toggle line numbers
1 #include "Something.h" 2 #include <protocol/TBinaryProtocol.h> 3 #include <server/TSimpleServer.h> 4 #include <transport/TServerSocket.h> 5 #include <transport/TTransportUtils.h> 6 7 using namespace apache::thrift; 8 using namespace apache::thrift::protocol; 9 using namespace apache::thrift::transport; 10 using namespace apache::thrift::server; 11 12 using boost::shared_ptr; 13 14 using namespace Test; 15 16 class SomethingHandler : virtual public SomethingIf { 17 public: 18 SomethingHandler() { 19 // Your initialization goes here 20 } 21 22 int32_t ping() { 23 // Your implementation goes here 24 printf("ping\n"); 25 } 26 27 }; 28 29 int main(int argc, char **argv) { 30 int port = 9090; 31 shared_ptr<SomethingHandler> handler(new SomethingHandler()); 32 shared_ptr<TProcessor> processor(new SomethingProcessor(handler)); 33 shared_ptr<TServerTransport> serverTransport(new TServerSocket(port)); 34 shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory()); 35 shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory()); 36 37 TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory); 38 server.serve(); 39 return 0; 40 }
Compiling/Building the server
To quickly build a binary using a single command use:g++ -DHAVE_INTTYPES_H -DHAVE_NETINET_IN_H -Wall -I/usr/local/include/thrift *.cpp -L/usr/local/lib -lthrift -o something
Compiling
You need to point your compiler to the thrift include path (CXX flag: -I/usr/local/include/thrift)g++ -Wall -I/usr/local/include/thrift -c Something.cpp -o something.o g++ -Wall -I/usr/local/include/thrift -c Something_server.cpp -o server.o g++ -Wall -I/usr/local/include/thrift -c your_thrift_file_constants.cpp -o constants.o g++ -Wall -I/usr/local/include/thrift -c your_thrift_file_types.cpp -o types.o
Linking
You need to point your linker to the thrift library. (Linker flag: -lthrift or -l/usr/local/lib/libthrift.so )g++ -L/usr/local/lib *.o -o Something_server -lthrift
Writing the client code
thrift does not auto generate a client interface, so you have to create the file.Toggle line numbers
1 #include "Something.h" // As an example 2 3 #include <transport/TSocket.h> 4 #include <transport/TBufferTransports.h> 5 #include <protocol/TBinaryProtocol.h> 6 7 using namespace apache::thrift; 8 using namespace apache::thrift::protocol; 9 using namespace apache::thrift::transport; 10 11 using namespace Test; 12 13 int main(int argc, char **argv) { 14 boost::shared_ptr<TSocket> socket(new TSocket("localhost", 9090)); 15 boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket)); 16 boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport)); 17 18 SomethingClient client(protocol); 19 transport->open(); 20 client.ping(); 21 transport->close(); 22 23 return 0; 24 }
Compiling
g++ -Wall -I/usr/local/include/thrift -c Something_client.cpp -o client.o
Linking
g++ -L/usr/local/lib client.o Something.o constants.o types.o -o Something_client -lthrift
Compiling/Building everything with Makefile
GEN_SRC := Something.cpp your_thrift_file_constants.cpp your_thrift_file_types.cpp GEN_OBJ := $(patsubst %.cpp,%.o, $(GEN_SRC)) THRIFT_DIR := /usr/local/include/thrift BOOST_DIR := /usr/local/include INC := -I$(THRIFT_DIR) -I$(BOOST_DIR) .PHONY: all clean all: something_server something_client %.o: %.cpp $(CXX) -Wall -DHAVE_INTTYPES_H -DHAVE_NETINET_IN_H $(INC) -c $< -o $@ something_server: Something_server.o $(GEN_OBJ) $(CXX) $^ -o $@ -L/usr/local/lib -lthrift something_client: Something_client.o $(GEN_OBJ) $(CXX) $^ -o $@ -L/usr/local/lib -lthrift clean: $(RM) *.o something_server something_client
Appendix: About TNonblockingServer
If you are writing an application that will serve a lot of connection (like php front end calling thrift service), you'd better use TNonblockingServer. TNonblockingServer can accept a lot ofconnections while throttling the processor threads using a pool.
* TNonblockingServer with a thread pool is the c++ alternative of the JAVA THsHaServer; * TNonblockingServer withOUT a thread pool is the c++ alternative
of the JAVA TNonblockingServer;
Server code with thread pool:
shared_ptr<TProcessor> processor(new SomethingProcessor(handler)); shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory()); // using thread pool with maximum 15 threads to handle incoming requests shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(15); shared_ptr<PosixThreadFactory> threadFactory = shared_ptr<PosixThreadFactory>(new PosixThreadFactory()); threadManager->threadFactory(threadFactory); threadManager->start(); TNonblockingServer server(processor, protocolFactory, 8888, threadManager); server.serve(); // ...
C++ client code (you have to use TFramedTransport here):
boost::shared_ptr<TSocket> socket(new TSocket("localhost", 8888)); boost::shared_ptr<TTransport> transport(new TFramedTransport(socket)); boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport)); SomethingClient client(protocol); transport->open(); // do something here... transport->close();
PHP client code (you have to use TFramedTransport here):
$transport = new TFramedTransport(new TSocket("localhost", 8888)); $transport->open(); $protocol = new TBinaryProtocol($transport); $client= new SomethingClient($protocol, $protocol); // do something here... transport->close();
相关文章推荐
- centos安装pycrypto
- linux的ipc信号量简单实例
- Linux下firefox安装flash player插件
- SELinux角色的用途
- linux动态链接相关
- linux系统下查看图片尺寸的命令
- Centos 7 安装jdk1.7
- Linux下安装php扩展
- Linux中解决SSH连接慢问题
- 安装centos 7 体验安装过程
- 安装centos 7 体验安装过程
- 如何在win 7/8 中制作一个Ubuntu 14.04 的USB随身碟
- linux grep命令 More命令 Less命令 基本使用
- Linux 编写ls -l 命令
- Linux 编写ls -l 命令
- Linux 编写ls -l 命令
- Linux 编写ls -l 命令
- cmd&Linux 下使用mysql全记录
- Linux中iptables设置详细
- linux 内存文件系统