- 浏览: 54766 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
conansadan:
要用GCC来编译,如果用APPLE LLVM是link不上的。 ...
使用thrift做c++,java和python的相互调用 -
visoeclipse:
Undefined symbols for architect ...
使用thrift做c++,java和python的相互调用 -
visoeclipse:
通过你的说法, 我编译结果出现下面错误:Type 'UserS ...
使用thrift做c++,java和python的相互调用
linux上安装thrift见
http://jinghong.iteye.com/blog/1102535
thrift做为跨语言调用的方案有高效,支持语言较多,成熟等优点;代码侵入较强是其弱点。
下面记录以C++做服务器,C++,java和python做客户端的示例,这个和本人现在工作环境吻合,使用多线程长连接的socket来建立高效分布式系统的跨语言调用平台。
遗憾的是目前版本(0.7.0)的C语言还不支持Compact协议,导致在现在的环境中nginx c module调用thrift要使用binary协议。thrift开发团队似乎对C语言不太感冒。
1.定义idl文件acsuser.thrift
2.生成c++,java和python代码框架
这时生成子目录gen-cpp,gen-java,gen-py
3.生成C++服务端代码
修改UserServer.cpp
注意这段代码使用TCompactProtocol,需要#include <config.h>
另外这个是Blocking的多线程服务器
4.生成C++的client文件UserClient.cpp
5.生成Makefile
6.启动c++ server
7.测试c++ client
8.写java client文件UserClient.java
编译和运行java client
9.写Python client文件PythonClient.py
执行python client代码
http://jinghong.iteye.com/blog/1102535
thrift做为跨语言调用的方案有高效,支持语言较多,成熟等优点;代码侵入较强是其弱点。
下面记录以C++做服务器,C++,java和python做客户端的示例,这个和本人现在工作环境吻合,使用多线程长连接的socket来建立高效分布式系统的跨语言调用平台。
遗憾的是目前版本(0.7.0)的C语言还不支持Compact协议,导致在现在的环境中nginx c module调用thrift要使用binary协议。thrift开发团队似乎对C语言不太感冒。
1.定义idl文件acsuser.thrift
struct User{ 1: string uid, 2: string uname, 3: bool usex, 4: i16 uage, } service UserService{ void add(1: User u), User get(1: string uid), }
2.生成c++,java和python代码框架
thrift -r --gen cpp acsuser.thrift thrift -r --gen java acsuser.thrift thrift -r --gen py acsuser.thrift
这时生成子目录gen-cpp,gen-java,gen-py
3.生成C++服务端代码
cp gen-cpp/UserService_server.skeleton.cpp UserServer.cpp
修改UserServer.cpp
#include "UserService.h" #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 ::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); } void get(User& _return, const std::string& uid) { // Your implementation goes here _return.uid = "leo1"; _return.uname = "yueyue"; _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); } }; 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; }
注意这段代码使用TCompactProtocol,需要#include <config.h>
另外这个是Blocking的多线程服务器
4.生成C++的client文件UserClient.cpp
#include "UserService.h" #include <config.h> #include <transport/TSocket.h> #include <transport/TBufferTransports.h> #include <protocol/TCompactProtocol.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 TCompactProtocol(transport)); transport->open(); User u; u.uid = "leo"; u.uname = "yueyue"; u.usex = 1; u.uage = 3; UserServiceClient client(protocol); client.add(u); 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; }
5.生成Makefile
BOOST_DIR = /usr/local/include/boost/ THRIFT_DIR = /usr/local/include/thrift LIB_DIR = /usr/local/lib GEN_SRC = ./gen-cpp/acsuser_types.cpp ./gen-cpp/acsuser_constants.cpp ./gen-cpp/UserService.cpp default: server client server: UserServer.cpp g++ -g -o UserServer -I${THRIFT_DIR} -I${BOOST_DIR} -I./gen-cpp -L${LIB_DIR} -lthrift UserServer.cpp ${GEN_SRC} client: UserClient.cpp g++ -g -o UserClient -lm -pthread -lz -lrt -lssl -I${THRIFT_DIR} -I${BOOST_DIR} -I./gen-cpp -L${LIB_DIR} -lthrift UserClient.cpp ${GEN_SRC} clean: $(RM) -r UserServer UserClient
6.启动c++ server
./UserServer
7.测试c++ client
./UserClient
8.写java client文件UserClient.java
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(); } }
编译和运行java client
javac -classpath /usr/local/lib/libthrift-0.7.0.jar:/usr/local/lib/log4j-1.2.14.jar:/usr/local/lib/commons-logging-1.1.1.jar:/usr/local/lib/slf4j-api-1.5.8.jar UserClient.java ./gen-java/*.java java -classpath .:./gen-java:/usr/local/lib/libthrift-0.7.0.jar:/usr/local/lib/log4j-1.2.14.jar:/usr/local/lib/commons-logging-1.1.1.jar:/usr/local/lib/slf4j-api-1.5.8.jar:/usr/local/lib/slf4j-log4j12-1.5.8.jar UserClient
9.写Python client文件PythonClient.py
#!/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 client代码
chmod 777 PythonClient.py ./PythonClient.py
- sample.tar.gz (2.1 KB)
- 描述: 源文件
- 下载次数: 118
评论
3 楼
conansadan
2012-11-15
要用GCC来编译,如果用APPLE LLVM是link不上的。
2 楼
visoeclipse
2012-05-07
Undefined symbols for architecture x86_64:
"vtable for apache::thrift::server::TThreadPoolServer", referenced from:
apache::thrift::server::TThreadPoolServer::TThreadPoolServer<apache::thrift::TProcessor>(boost::shared_ptr<apache::thrift::TProcessor> const&, boost::shared_ptr<apache::thrift::transport::TServerTransport> const&, boost::shared_ptr<apache::thrift::transport::TTransportFactory> const&, boost::shared_ptr<apache::thrift::protocol::TProtocolFactory> const&, boost::shared_ptr<apache::thrift::concurrency::ThreadManager> const&, boost::enable_if<boost::is_convertible<apache::thrift::TProcessor*, apache::thrift::TProcessor*>::type, void*>::type)in UserServer.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
"vtable for apache::thrift::transport::TBufferedTransport", referenced from:
apache::thrift::transport::TBufferedTransport::TBufferedTransport(boost::shared_ptr<apache::thrift::transport::TTransport>)in UserServer.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
"apache::thrift::transport::TServerSocket::TServerSocket(int)", referenced from:
_main in UserServer.o
"apache::thrift::concurrency::ThreadManager::newSimpleThreadManager(unsigned long, unsigned long)", referenced from:
_main in UserServer.o
"apache::thrift::concurrency::PosixThreadFactory::PosixThreadFactory(apache::thrift::concurrency::PosixThreadFactory::POLICY, apache::thrift::concurrency::PosixThreadFactory::PRIORITY, int, bool)", referenced from:
_main in UserServer.o
"apache::thrift::server::TThreadPoolServer::serve()", referenced from:
_main in UserServer.o
"apache::thrift::server::TThreadPoolServer::~TThreadPoolServer()", referenced from:
_main in UserServer.o
"apache::thrift::TApplicationException::write(apache::thrift::protocol::TProtocol*) const", referenced from:
UserServiceProcessor::process(boost::shared_ptr<apache::thrift::protocol::TProtocol>, boost::shared_ptr<apache::thrift::protocol::TProtocol>, void*)in UserService.o
UserServiceProcessor::process_fn(apache::thrift::protocol::TProtocol*, apache::thrift::protocol::TProtocol*, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, int, void*)in UserService.o
UserServiceProcessor::process_add(int, apache::thrift::protocol::TProtocol*, apache::thrift::protocol::TProtocol*, void*)in UserService.o
UserServiceProcessor::process_get(int, apache::thrift::protocol::TProtocol*, apache::thrift::protocol::TProtocol*, void*)in UserService.o
"apache::thrift::TApplicationException::read(apache::thrift::protocol::TProtocol*)", referenced from:
UserServiceClient::recv_add() in UserService.o
UserServiceClient::recv_get(User&) in UserService.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
"vtable for apache::thrift::server::TThreadPoolServer", referenced from:
apache::thrift::server::TThreadPoolServer::TThreadPoolServer<apache::thrift::TProcessor>(boost::shared_ptr<apache::thrift::TProcessor> const&, boost::shared_ptr<apache::thrift::transport::TServerTransport> const&, boost::shared_ptr<apache::thrift::transport::TTransportFactory> const&, boost::shared_ptr<apache::thrift::protocol::TProtocolFactory> const&, boost::shared_ptr<apache::thrift::concurrency::ThreadManager> const&, boost::enable_if<boost::is_convertible<apache::thrift::TProcessor*, apache::thrift::TProcessor*>::type, void*>::type)in UserServer.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
"vtable for apache::thrift::transport::TBufferedTransport", referenced from:
apache::thrift::transport::TBufferedTransport::TBufferedTransport(boost::shared_ptr<apache::thrift::transport::TTransport>)in UserServer.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
"apache::thrift::transport::TServerSocket::TServerSocket(int)", referenced from:
_main in UserServer.o
"apache::thrift::concurrency::ThreadManager::newSimpleThreadManager(unsigned long, unsigned long)", referenced from:
_main in UserServer.o
"apache::thrift::concurrency::PosixThreadFactory::PosixThreadFactory(apache::thrift::concurrency::PosixThreadFactory::POLICY, apache::thrift::concurrency::PosixThreadFactory::PRIORITY, int, bool)", referenced from:
_main in UserServer.o
"apache::thrift::server::TThreadPoolServer::serve()", referenced from:
_main in UserServer.o
"apache::thrift::server::TThreadPoolServer::~TThreadPoolServer()", referenced from:
_main in UserServer.o
"apache::thrift::TApplicationException::write(apache::thrift::protocol::TProtocol*) const", referenced from:
UserServiceProcessor::process(boost::shared_ptr<apache::thrift::protocol::TProtocol>, boost::shared_ptr<apache::thrift::protocol::TProtocol>, void*)in UserService.o
UserServiceProcessor::process_fn(apache::thrift::protocol::TProtocol*, apache::thrift::protocol::TProtocol*, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, int, void*)in UserService.o
UserServiceProcessor::process_add(int, apache::thrift::protocol::TProtocol*, apache::thrift::protocol::TProtocol*, void*)in UserService.o
UserServiceProcessor::process_get(int, apache::thrift::protocol::TProtocol*, apache::thrift::protocol::TProtocol*, void*)in UserService.o
"apache::thrift::TApplicationException::read(apache::thrift::protocol::TProtocol*)", referenced from:
UserServiceClient::recv_add() in UserService.o
UserServiceClient::recv_get(User&) in UserService.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
1 楼
visoeclipse
2012-05-07
通过你的说法, 我编译结果出现下面错误:
Type 'UserServiceProcessor' could not be resolved
这个是啥意思?
Type 'UserServiceProcessor' could not be resolved
这个是啥意思?
发表评论
-
nginx,uwsgi,bottle,mako安装和性能测试
2011-11-30 19:44 4161上一篇 nginx,uwsgi,bottle,virtuale ... -
nginx,uwsgi,bottle,virtualenv在centos6上安装及性能测试
2011-11-29 15:12 4930终于决定废弃java而使用python来开发网站,测试web. ... -
thrift0.7.0在ubuntu11.04/11.10和centos6上的安装测试
2011-06-23 15:17 3459参照 http://jnb.ociweb.com/jnb/jn ... -
linux常用命令
2011-06-10 10:59 1147查看linux某个服务端口的establish连接总数 net ... -
linux内核版本对吞吐量的影响
2011-03-11 14:08 1722硬件环境Dell 1950,E5410X2,16G 原先在使用 ... -
linux系统获取基础硬件信息的shell
2011-01-31 10:24 4498来自http://code.google.com/p/aspe ...
相关推荐
Thrift中实现Java与Python的RPC互相调用示例代码;Thrift中实现Java与Python的RPC互相调用示例代码;Thrift中实现Java与Python的RPC互相调用示例代码
一个java和python的thrift例子,其中java作为服务端,python作为客户端
它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, and OCaml 这些编程语言间无缝结合的、高效的服务。
使用thrift、websocket在javascript和c++之间建立rpc调用机制。 如果要使用js+html来写界面,cpp来写底层业务逻辑,这就非常有用了。 当然,如果底层不用cpp来写,也可以参考本代码的js对thrift的rpc包装。
java代码使用thrift2操作hbase示例,thrift2连接hbase添加数据,单条查找,删除数据,根据扫描器查找,修改数据等测试实例
flume通过thrift协议收集日志-Python。博客地址:http://blog.csdn.net/wangshuwei5/article/details/48902757
本文档详细说明了Linux和windows系统下配置thrift环境和hbaseC++客户端的使用。
RPC(Remote Procedure Call Protocol)远程过程调用协议实例,学习和使用thrift无痛入门代码。具体使用方法,可以看博客详解。
【Thrift之C++远程调用helloworld菜鸟教程】
1、编译后的thrift C++和.net库文件; ...4、thrift使用了boost,C++开发需要包含boost相关库和头文件; 5、boost编译的C++库和头文件下载地址:http://download.csdn.net/detail/xzhswust/8395491
Thrift双向通讯java代码
压缩包分为三个部分,java工程(java的服务方,和java客户端) php工程,php的客户端 linux环境的服务方部署包以及脚本 其中myserver.tar.gz压缩包是linux环境的部署包, 在linux环境先解压, tar -zxvf myserver.tar.gz ...
thrift的使用一定会依赖boost,但是因为boost过大,附件没有包含,还需要自行编译
thrift服务,java调用C#完整代码
本例改编自Apache Thrift教程: ... ... 原教程使用的是c++ server和php client. 我用c++和c#分别实现了client和server,他们之间可以用任意组合互相调用,没有太复杂的功能,非常利于初学Thrift.
thrift生成好的python hbase库,配合thrift库可直接操作hbase
Thrift的简单使用demo
Thrift作为可伸缩的跨语言服务开发框架,网上的资源较少,这里是一个简单的入门小程序,文件中的mylib下包含了依赖的jar包,并且在file目录下放了一个简单的thrift文件和生成thrift-0.9.0.exe工具,直接使用 thrift-...
thrift 生成的java包 全部的包,一次下全了。。。
使用wireshark抓取thrift协议接口调用