挂载iso镜像
mkdir /home/centos-cdrom mkdir /mnt/sdb/iso
mount -o loop /mnt/sdb/iso/CentOS_6.4_Final.iso /home/centos-cdrom
df -h 查看一下是否挂载成功
配置local.repo ,cd /etc/yum.repos.d/ 将其下的其他repo文件都移走
1 cd /etc/yum.repos.d/ 2 mkdir bak 3 mv * bak/ 4 新建个local.repo
vi /etc/yum.repo.d/local.repo[local]
sudo yum -y update
sudo yum -y groupinstall "Development Tools"
sudo yum install -y wget
wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz tar xvf autoconf-2.69.tar.gz cd autoconf-2.69 ./configure --prefix=/usr make sudo make install cd ..
wget http://ftp.gnu.org/gnu/automake/automake-1.14.tar.gz tar xvf automake-1.14.tar.gz cd automake-1.14 ./configure --prefix=/usr make sudo make install cd ..
wget http://ftp.gnu.org/gnu/bison/bison-2.5.1.tar.gz tar xvf bison-2.5.1.tar.gz cd bison-2.5.1 ./configure --prefix=/usr make sudo make install cd ..
http://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz
http://ftp.gnu.org/gnu/automake/automake-1.14.tar.gz
http://ftp.gnu.org/gnu/bison/bison-2.5.1.tar.gz
http://sourceforge.net/projects/boost/files/boost/1.53.0/boost_1_53_0.tar.gz
https://git-wip-us.apache.org/repos/asf/thrift.git
sudo yum -y install libevent-devel zlib-devel openssl-devel
wget http://sourceforge.net/projects/boost/files/boost/1.53.0/boost_1_53_0.tar.gz tar xvf boost_1_53_0.tar.gz cd boost_1_53_0 ./bootstrap.sh sudo ./b2 install
./configure --with-lua=no make sudo make install测试IDL
namespace cpp test.thrift service HelloWorldService { string sayHello(1:string username)进入源码dir的compiler/cpp/下 已经生成了编译器thrift
thrift -version
执行 thrift -r ---gen cpp test.thrift(IDL文件)
生成gen-cpp文件夹,然后修改服务端代码
vim HelloWorldService_server.skeleton.cpp
25 void sayHello(std::string& _return, const std::string& username) {
26 // Your implementation goes here
27 // printf("sayHello\n",username);
28 cout << "sayHello : " << username <
然后生成服务端
g++ -g -o Hello -I/usr/local/include/thrift -I/usr/local/include/boost -I./gen-cpp -L/usr/local/lib -lthrift HelloWorldService_server.skeleton.cpp HelloWorldService.cpp test_types.cpp test_constants.cpp -DHAVE_NETINET_IN_H
‘htons’的声明必须可用[-fpermissive],则修改Makefile,加上编译选项-DHAVE_NETINET_IN_H ,放在最后
DHAVE_NETINET_IN_H 这个编译选项要有 放在最后
编译客户端代码
1 #include "HelloWorldService.h" 2 #include执行 g++ -g -o Client -I/usr/local/include/thrift -I/usr/local/include/boost -I./gen-cpp -L/usr/local/lib -lthrift client.cpp HelloWorldService.cpp test_types.cpp test_constants.cpp -DHAVE_NETINET_IN_H3 #include 4 #include 5 #include 6 #include 7 #include 8 using namespace ::apache::thrift; 9 using namespace ::apache::thrift::protocol; 10 using namespace ::apache::thrift::transport; 11 using namespace ::apache::thrift::server; 12 using namespace std; 13 using boost::shared_ptr; 14 15 using namespace ::test::thrift; 16 17 int main(){ 18 shared_ptr socket(new TSocket("localhost", 7999)); 19 shared_ptr transport(new TBufferedTransport(socket)); 20 shared_ptr protocol(new TBinaryProtocol(transport)); 21 22 HelloWorldServiceClient client(protocol); 23 transport->open(); 24 string returnString ; 25 const string name = "Tom"; 26 client.sayHello(returnString,name); 27 // 我们的代码写在这里 28 cout << returnString << endl; 29 30 transport->close(); 31 32 return 0; 33 }
测试
先启动服务端
./Hello
再启动客户端
./Client
如图:
亲试经验
相关参考
http://roclinux.cn/?p=3316
http://www.cnblogs.com/bornfish/p/4184099.html