thrift之Hello--python client & cpp sever
2011-04-17 23:35
573 查看
Thrift是一个开发跨语言服务的软件框架。编写thrift文件,通过自带的代码生成引擎即可生成各种语言(C++,Java,Python,PHP,Ruby,Erlang,C#等)的对应代码,下面以最经典的hello为例讲述,如何通过thrift编写跨语言的RPC程序:
1编写thrift文件,保存为hello.thrift:
2生成cpp和py框架文件
在hello.thrift文件所在目录执行:
thrift -r --gen cpp hello.thrift
thrift -r --gen py hello.thrift
会在当前目录下面产生两个文件夹,分别为gen-cpp和gen-py,
3编写cpp服务器端代码,拷贝gen-cpp目录中的Hello_server.skeleton.cpp到当前目录,重命名为CppServer.cpp,修改如下:
4编写python客户端代码,PythonClient.py如下:
5编写cpp端Makefile如下:
6编译cpp端,直接make即可生成相应的可执行文件,python端可以直接运行。
至此一个最简单的跨语言PRC程序即完成了,很简单吧。
1编写thrift文件,保存为hello.thrift:
service Hello { void Hello() }
2生成cpp和py框架文件
在hello.thrift文件所在目录执行:
thrift -r --gen cpp hello.thrift
thrift -r --gen py hello.thrift
会在当前目录下面产生两个文件夹,分别为gen-cpp和gen-py,
3编写cpp服务器端代码,拷贝gen-cpp目录中的Hello_server.skeleton.cpp到当前目录,重命名为CppServer.cpp,修改如下:
#include "Hello.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 HelloHandler : virtual public HelloIf { public: HelloHandler() { // Your initialization goes here } void Hello() { // Your implementation goes here printf("Hello,Thrift/n"); } }; int main(int argc, char **argv) { int port = 9090; shared_ptr<HelloHandler> handler(new HelloHandler()); shared_ptr<TProcessor> processor(new HelloProcessor(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; }
4编写python客户端代码,PythonClient.py如下:
#!/usr/bin/env python import sys sys.path.append('./gen-py') from hello import Hello from hello.ttypes import * from thrift import Thrift from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol # Make socket transport = TSocket.TSocket('localhost', 9090) # Buffering is critical. Raw sockets are very slow transport = TTransport.TBufferedTransport(transport) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder client = Hello.Client(protocol) # Connect! transport.open() # Call Server services client.Hello()
5编写cpp端Makefile如下:
BOOST_DIR = /usr/local/include/boost/ THRIFT_DIR = /usr/local/include/thrift LIB_DIR = /usr/local/lib GEN_SRC = ./gen-cpp/hello_types.cpp ./gen-cpp/Hello.cpp default: server server: CppServer.cpp g++ -o CppServer -I${THRIFT_DIR} -I${BOOST_DIR} -I./gen-cpp -L${LIB_DIR} -lthrift CppServer.cpp ${GEN_SRC} clean: $(RM) -r CppServer
6编译cpp端,直接make即可生成相应的可执行文件,python端可以直接运行。
至此一个最简单的跨语言PRC程序即完成了,很简单吧。
相关文章推荐
- Python&Thrift--Server&Client
- To say "Hello world" in Python CGI Web Programming in 5 minutes
- Python案例-网络编程-socket入门-server&client
- 【Hello Python World】Week 1(1):探索 & 展望
- Thrift in python<转>
- python print'hello' File "<stdin>",line 1 print 'hello' SyntaxError:invalid syntax
- thrift C++做server, C++,python, java做Client例子
- 在 Python中使用 cout << 'Hello,world!' << endl
- UDPClient.py&UDPServer.py——我的第一行Python代码
- Python on ROS - (1) - Service & Client
- python 3.4.0 简单的print 'hello world',出错--SyntaxError: invalid syntax
- thrift C++做server, C++,python, java做Client例子
- Thrift tutorial(python server and php client)
- memcached java && python client api 共享
- Golang&Python测试thrift
- python 3.4.0 简单的print 'hello world',出错--SyntaxError: invalid syntax
- 【Python】Visual Studio Code 安装&&使用 hello python~~~~
- python-gtk-3-tutorial(2)--"Print hello world"
- thrift使用:java作为client端调用python服务端
- python thrift 示例<转>