0.9.1 要求autoconf是2.56以上,默认的centos6.5是2.53
0.9.0
0.8.0 gcc版本不能是4.4,make写得太严格了,生成的代码,在gcc4.4下不可用
都不行
查到的解决方法http://blog.csdn.net/hoho568/article/details/7321611 尝试也不都能通过
最终确认
0.7.0
参考:
http://blog.csdn.net/hbuxiaoshe/article/details/6558391
http://jinghong.iteye.com/blog/1222713
下载
http://archive.apache.org/dist/thrift/0.7.0/thrift-0.7.0.tar.gz
yum install automake libtool flex bison pkgconfig gcc-c++ boost-devel libevent-devel zlib-devel python-devel ruby-devel openssl-devel
yum remove qt
./configure --prefix=/usr/local/thrift --with-java --with-cpp --with-python
make
make install
设置环境变量
export PATH=$HADOOP_HOME/bin:$HIVE_HOME/bin:/usr/local/java/jdk1.6.0_45/bin:$HBASE_HOME/bin:$PATH:/usr/local/thrift/bin:/usr/local/git/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib:/usr/local/thrift/lib
student.thrift: struct Student{ 1: i32 sno, 2: string sname, 3: bool ssex, 4: i16 sage, } service Serv{ void put(1: Student s), }
thrift -r --gen cpp student.thrift
vim client.cpp
----------------
include "Serv.h" #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 boost::shared_ptr; 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)); transport->open(); // 我们的代码写在这里 Student s; s.sno = 123; s.sname = "xiaoshe"; s.ssex = 1; s.sage = 30; ServClient client(protocol); client.put(s); //--- transport->close(); return 0; }
----------------
---------------------------
[root@localhost gen-cpp]# cat Serv_server.skeleton.cpp
// This autogenerated skeleton file illustrates how to build a server. // You should copy it to another filename to avoid overwriting it. #include "Serv.h" #include <protocol/TBinaryProtocol.h> #include <server/TSimpleServer.h> #include <transport/TServerSocket.h> #include <transport/TBufferTransports.h> using namespace ::apache::thrift; using namespace ::apache::thrift::protocol; using namespace ::apache::thrift::transport; using namespace ::apache::thrift::server; using boost::shared_ptr; class ServHandler : virtual public ServIf { public: ServHandler() { // Your initialization goes here } void put(const Student& s) { // Your implementation goes here printf("sno=%d sname=%s ssex=%d sage=%d/n", s.sno, s.sname.c_str(), s.ssex, s.sage);//这里是新加的 printf("put\n"); } }; int main(int argc, char **argv) { int port = 9090; shared_ptr<ServHandler> handler(new ServHandler()); shared_ptr<TProcessor> processor(new ServProcessor(handler)); shared_ptr<TServerTransport> serverTransport(new TServerSocket(port)); shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory()); shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory()); TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory); server.serve(); return 0; }
-----------------
g++ -g -I/usr/local/thrift/include/thrift -L/usr/local/thrift/lib/ -lthrift Serv.cpp student_types.cpp student_constants.cpp Serv_server.skeleton.cpp -o server g++ -g -I/usr/local/thrift/include/thrift -L/usr/local/thrift/lib/ -lthrift -lm -pthread -lz -lrt -lssl Serv.cpp student_types.cpp student_constants.cpp client.cpp -o client
root@localhost gen-cpp]# ./server &
[1] 8553
[root@localhost gen-cpp]# ./client
sno=123 sname=xiaoshe ssex=1 sage=30/nput (这里是server返回的)
[root@localhost gen-cpp]#
-------------------java客户端----------
--------code begin---- import org.apache.thrift.TException; import org.apache.thrift.protocol.TCompactProtocol; import org.apache.thrift.protocol.TProtocol; import org.apache.thrift.transport.TFramedTransport; import org.apache.thrift.transport.TNonblockingSocket; import org.apache.thrift.transport.TSocket; import org.apache.thrift.transport.TTransport; import org.apache.thrift.transport.TTransportException; //import UserService.Client; public class UserClient { private void start() { try { TTransport socket = new TSocket("localhost", 9090); //TTransport transport = new TFramedTransport(socket); TProtocol protocol = new TCompactProtocol(socket); UserService.Client client = new UserService.Client(protocol); socket.open(); System.out.println(client.get("lll")); User u = new User(); u.uid="leojava"; u.uname="yueyue"; u.usex=true; u.uage=3; client.add(u); socket.close(); } catch (TTransportException e) { e.printStackTrace(); } catch (TException e) { e.printStackTrace(); } } public static void main(String[] args) { UserClient c = new UserClient(); c.start(); } } --------code end----
编译
javac -classpath /usr/local/lib/libthrift-0.7.0.jar:/usr/local/lib/slf4j-api-1.5.8.jar:/usr/local/lib/commons-lang-2.5.jar:/usr/local/lib/commons-codec-1.3.jar:/usr/local/lib/httpclient-4.0.1.jar:/usr/local/lib/httpcore-4.0.1.jar:/usr/local/lib/servlet-api-2.5.jar UserClient.java ./gen-java/*.java java -classpath .:/usr/local/lib/libthrift-0.7.0.jar:/usr/local/lib/slf4j-api-1.5.8.jar:/usr/local/lib/commons-lang-2.5.jar:/usr/local/lib/commons-codec-1.3.jar:/usr/local/lib/httpclient-4.0.1.jar:/usr/local/lib/httpcore-4.0.1.jar:/usr/local/lib/servlet-api-2.5.jar:/usr/local/lib/slf4j-log4j12-1.6.1.jar:/usr/local/lib/log4j-1.2.17.jar:./gen-java/ UserClient
结果:
客户端
[root@localhost acsuser]# java -classpath .:/usr/local/lib/libthrift-0.7.0.jar:/usr/local/lib/slf4j-api-1.5.8.jar:/usr/local/lib/commons-lang-2.5.jar:/usr/local/lib/commons-codec-1.3.jar:/usr/local/lib/httpclient-4.0.1.jar:/usr/local/lib/httpcore-4.0.1.jar:/usr/local/lib/servlet-api-2.5.jar:/usr/local/lib/slf4j-log4j12-1.6.1.jar:/usr/local/lib/log4j-1.2.17.jar:./gen-java/ UserClient
SLF4J: The requested version 1.6 by your slf4j binding is not compatible with [1.5.5, 1.5.6, 1.5.7, 1.5.8]
SLF4J: See http://www.slf4j.org/codes.html#version_mismatch for further details.
User(uid:leo1, uname:yueyue, usex:true, uage:3)
[root@localhost acsuser]#
服务端
[root@localhost acsuser]# ./UserServer
start user server...
uid=leo1 uname=yueyue usex=1 uage=3
uid=leojava uname=yueyue usex=1 uage=3
------------python---客户端
#!/usr/bin/env python import sys sys.path.append('./gen-py') from acsuser import UserService from acsuser.ttypes import * from thrift import Thrift from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TCompactProtocol # Make socket transport = TSocket.TSocket('localhost', 9090) # Buffering is critical. Raw sockets are very slow transport = TTransport.TBufferedTransport(transport) # Wrap in a protocol protocol = TCompactProtocol.TCompactProtocol(transport) # Create a client to use the protocol encoder client = UserService.Client(protocol) # Connect! transport.open() # Call Server services u = client.get('lll') print 'uid=%s uname=%s usex=%d u.uage=%d' %(u.uid,u.uname,u.usex,u.uage) u1 = User() u1.uid='leo' u1.uname='yueyue' u1.usex=1 u1.uage=3 client.add(u1)
结果
python PythonClient.py
uid=leo1 uname=yueyue usex=1 u.uage=3
-----------------传list的例子--------------
struct User{ 1: string uid, 2: string uname, 3: bool usex, 4: i16 uage, 5: list<i32> subNodeList, 6: map<i32,string> subNodeMap, 7: set<i32> subNodeSet } service UserService{ void add(1: User u), void addlist(1: list<User> u), User get(1: string uid), }
UserServer.cpp :
[root@haoning listuser]# cat UserServer.cpp #include "UserService.h" #include <vector> #include <config.h> //#include <protocol/TBinaryProtocol.h> #include <protocol/TCompactProtocol.h> #include <server/TSimpleServer.h> #include <transport/TServerSocket.h> #include <transport/TBufferTransports.h> #include <concurrency/ThreadManager.h> #include <concurrency/PosixThreadFactory.h> #include <server/TThreadPoolServer.h> #include <server/TThreadedServer.h> using namespace std; using namespace ::apache::thrift; using namespace ::apache::thrift::protocol; using namespace ::apache::thrift::transport; using namespace ::apache::thrift::server; using namespace ::apache::thrift::concurrency; using boost::shared_ptr; class UserServiceHandler : virtual public UserServiceIf { public: UserServiceHandler() { // Your initialization goes here } void add(const User& u) { // Your implementation goes here printf("uid=%s uname=%s usex=%d uage=%d\n", u.uid.c_str(), u.uname.c_str(), u.usex, u.uage); printf("add\n"); } //void addlist(const std::vector<User, std::allocator<User> > & u) { void addlist(const std::vector<User> & u) { std::vector<User>::const_iterator iter; for(iter=u.begin();iter!=u.end();iter++) { printf("u size: %d\n",u.size()); printf("send 1: %s, %s\n",(*iter).uid.c_str(),(*iter).uname.c_str()); } //User rect; //rect.uid="ning"; //rect.uname="haohao"; //rect.usex=0; //rect.uage=3; //u.push_back(rect); //for(int x = 0; x < 5; x++) //{ // u.push_back(new User()); //} //std::vector<User, std::allocator<User>>::iterator it=u.begin(); //for(it=u.begin();it!=u.end();it++){ // std::cout<<"test"<<std::endl; // cout<<(*it).id<<endl; // printf("addlist---\n"); // } printf("addlist---\n"); } // void addlist(const std::Vector<User>& vec) { // std::vector<User>::iterator it=vec.begin(); // for(it=vec.begin();it!=vec.end();it++){ // // cout<<"test"<<endl; // // std::cout<<"addlist:"<<(*it).uid<<std::endl; // printf("addlist id --%s\n",(*it).uid,c_str()); // } // printf("addlist ---------\n"); // } void get(User& _return, const std::string& uid) { // Your implementation goes here _return.uid = "tinghaode"; _return.uname = "ningge"; _return.usex = 1; _return.uage = 3; printf("uid=%s uname=%s usex=%d uage=%d\n", _return.uid.c_str(), _return.uname.c_str(), _return.usex, _return.uage); printf("get\n"); } }; int main(int argc, char **argv) { shared_ptr<UserServiceHandler> handler(new UserServiceHandler()); shared_ptr<TProcessor> processor(new UserServiceProcessor(handler)); shared_ptr<TProtocolFactory> protocolFactory(new TCompactProtocolFactory()); shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory()); shared_ptr<TServerTransport> serverTransport(new TServerSocket(9090)); shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(10); shared_ptr<PosixThreadFactory> threadFactory = shared_ptr<PosixThreadFactory>(new PosixThreadFactory()); threadManager->threadFactory(threadFactory); threadManager->start(); printf("start user server...\n"); TThreadPoolServer server(processor, serverTransport, transportFactory, protocolFactory, threadManager); server.serve(); return 0; } [root@haoning listuser]#
UserClient.cpp :
[root@haoning listuser]# cat UserClient.cpp #include "UserService.h" #include <config.h> #include <vector> #include <transport/TSocket.h> #include <transport/TBufferTransports.h> #include <protocol/TCompactProtocol.h> using namespace std; using namespace apache::thrift; using namespace apache::thrift::protocol; using namespace apache::thrift::transport; using boost::shared_ptr; 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 TCompactProtocol(transport)); transport->open(); User u; u.uid = "leo"; u.uname = "yueyue"; u.usex = 1; u.uage = 3; UserServiceClient client(protocol); client.add(u); //-----list begin-- vector<User> vec; User rect; rect.uid="ning"; rect.uname="haohao"; rect.usex=0; rect.uage=3; vec.push_back(rect); client.addlist(vec); //-----list end---- User u1; client.get(u1,"lll"); transport->close(); printf("uid=%s uname=%s usex=%d uage=%d\n", u1.uid.c_str(), u1.uname.c_str(), u1.usex, u1.uage); return 0; } [root@haoning listuser]#
注意
std::vector<User>::const_iterator iter
用const_iterator