Thrift多语言,Server(C++) Client(Java)
2016-10-30 00:00
483 查看
摘要: Thrift安装和使用方法
安装
1. 安装Boost
下载地址: http://sourceforge.net/projects/boost/files/boost/ cd boost_version
./bootstrap.sh
./b2 install
注意:
linux程序运行时加载共享库出现的错误:
"error while loading shared libraries: xxxx: cannot open shared object file: No such file or directory"
解决步骤:
1、使用find命令查找缺失的xxxx共享库文件所在位置。参考:#find 目录 -name "xxxx*"
2、将找到的目录位置写入 /etc/ld.so.conf 配置文件,这个文件记录了编译时使用的动态链接库的路径。
3、然后使用ldconfig命令,使配置生效。
2.安装其他依赖项
sudo yum -y install libevent-devel zlib-devel openssl-devel
Ubuntu:安装Thrift编译和安装所以来的工具和库
sudo apt-get install libboost-dev libboost-test-dev libboost-program-options-dev libevent-dev automake libtool flex bison pkg-config g++ libssl-dev
CentOS(需要自己安装boost)
sudo yum -y install libevent-devel zlib-devel openssl-devel
如果需要支持Java语言,则需要安装 Apache Ant
./configure --with-cpp --with-boost --without-python --without-csharp --with-java --without-erlang --without-perl --with-php --without-php_extension --without-ruby --without-haskell --without-go
./make
./make install
3.安装Thrift
./configure
./make
./make install
./thrift -version 便可以看到thrift 版本
这篇文章介绍得比较详细: http://blog.csdn.net/qq910894904/article/details/41132779
________________________________________
使用
在使用本内容前,需要安装 Thrift
首先编辑 一个接口文件 .thrift
demo.thrift 代码如下:
运行如下命令:
# thrift -r --gen cpp demo.thrift
可以看到在当前目录下产生了一个gen-cpp的目录,该目录下即以上命令产生的文件:
UserStorage.cpp
UserStorage.h
UserStorage_server.skeleton.cpp
demo_constants.cpp
demo_constants.h
demo_types.cpp
demo_types.h
注意:在以上文件中,只有UserStorage_server.skeleton.cpp是跟业务相关的,是可以修改的,其余文件都是框架相关的。
可以看到,该文件只是一个框架,用户可以根据需要扩展该文件,笔者修改如下(蓝色部分为添加的代码,同时将文件改名为 serStorage_server.cpp):
________________________________________
C++客户端实现
Makefile: 编译C++ 服务端和C++客户端
Java客户端实现
到thrift- x.x.x/tutorial/java/src/拷贝:JavaClient.java
重命名为:UserProfileClient.java
编译Java客户端需要使用Thrift Java 库,可以使用Maven
编译成功后便可以通讯了
Thrift介绍文档下载:
http://pan.baidu.com/s/1qXx7n7Q" target=_blank>http:// http://pan.baidu.com/s/1qXx7n7Q Thrift 官方Demo
http://thrift.apache.org/tutorial/
安装
1. 安装Boost
下载地址: http://sourceforge.net/projects/boost/files/boost/ cd boost_version
./bootstrap.sh
./b2 install
注意:
linux程序运行时加载共享库出现的错误:
"error while loading shared libraries: xxxx: cannot open shared object file: No such file or directory"
解决步骤:
1、使用find命令查找缺失的xxxx共享库文件所在位置。参考:#find 目录 -name "xxxx*"
2、将找到的目录位置写入 /etc/ld.so.conf 配置文件,这个文件记录了编译时使用的动态链接库的路径。
3、然后使用ldconfig命令,使配置生效。
2.安装其他依赖项
sudo yum -y install libevent-devel zlib-devel openssl-devel
Ubuntu:安装Thrift编译和安装所以来的工具和库
sudo apt-get install libboost-dev libboost-test-dev libboost-program-options-dev libevent-dev automake libtool flex bison pkg-config g++ libssl-dev
CentOS(需要自己安装boost)
sudo yum -y install libevent-devel zlib-devel openssl-devel
如果需要支持Java语言,则需要安装 Apache Ant
./configure --with-cpp --with-boost --without-python --without-csharp --with-java --without-erlang --without-perl --with-php --without-php_extension --without-ruby --without-haskell --without-go
./make
./make install
3.安装Thrift
./configure
./make
./make install
./thrift -version 便可以看到thrift 版本
这篇文章介绍得比较详细: http://blog.csdn.net/qq910894904/article/details/41132779
________________________________________
使用
在使用本内容前,需要安装 Thrift
首先编辑 一个接口文件 .thrift
demo.thrift 代码如下:
struct UserProfile{ 1:i32 id, //注意这里是逗号,而不是分号 2:string name, 3:string blurb } //这里没有分号 service UserStorage{ void store(1: UserProfile user), //注意这里是逗号,而不是分号 UserProfile getUser(1: i32 uid) }
运行如下命令:
# thrift -r --gen cpp demo.thrift
可以看到在当前目录下产生了一个gen-cpp的目录,该目录下即以上命令产生的文件:
UserStorage.cpp
UserStorage.h
UserStorage_server.skeleton.cpp
demo_constants.cpp
demo_constants.h
demo_types.cpp
demo_types.h
注意:在以上文件中,只有UserStorage_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 "UserStorage.h" #include <protocol/TBinaryProtocol.h> #include <server/TSimpleServer.h> #include <transport/TServerSocket.h> #include <transport/TBufferTransports.h> #include <map> using namespace std; using namespace ::apache::thrift; using namespace ::apache::thrift::protocol; using namespace ::apache::thrift::transport; using namespace ::apache::thrift::server; using boost::shared_ptr; class UserStorageHandler : virtual public UserStorageIf { public: UserStorageHandler() { // Your initialization goes here } void store(const UserProfile& user) { // Your implementation goes here printf("store\n"); } void getUser(UserProfile& _return, const int32_t uid) { // Your implementation goes here printf("getUser\n"); } }; int main(int argc, char **argv) { int port = 9090; shared_ptr<UserStorageHandler> handler(new UserStorageHandler()); shared_ptr<TProcessor> processor(new UserStorageProcessor(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; }
可以看到,该文件只是一个框架,用户可以根据需要扩展该文件,笔者修改如下(蓝色部分为添加的代码,同时将文件改名为 serStorage_server.cpp):
// This autogenerated skeleton file illustrates how to build a server. // You should copy it to another filename to avoid overwriting it. #include "UserStorage.h" #include <thrift/protocol/TBinaryProtocol.h> #include <thrift/server/TSimpleServer.h> #include <thrift/transport/TServerSocket.h> #include <thrift/transport/TBufferTransports.h> #include <map> using namespace std; using namespace ::apache::thrift; using namespace ::apache::thrift::protocol; using namespace ::apache::thrift::transport; using namespace ::apache::thrift::server; using boost::shared_ptr; class UserStorageHandler : virtual public UserStorageIf { public: UserStorageHandler() { // Your initialization goes here } void store(const UserProfile& user) { // Your implementation goes here log[user.id] = user; printf("store\n"); } void getUser(UserProfile& _return, const int32_t uid) { // Your implementation goes here _return = log[uid]; printf("getUser\n"); } protected: map<int32_t, UserProfile> log; }; int main(int argc, char **argv) { int port = 9090; shared_ptr<UserStorageHandler> handler(new UserStorageHandler()); shared_ptr<TProcessor> processor(new UserStorageProcessor(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; }
________________________________________
C++客户端实现
#include <stdio.h> #include <unistd.h> #include <sys/time.h> #include <protocol/TBinaryProtocol.h> #include <transport/TSocket.h> #include <transport/TTransportUtils.h> #include "UserStorage.h" using namespace std; using namespace apache::thrift; using namespace apache::thrift::protocol; using namespace apache::thrift::transport; using namespace boost; int main(int argc, char** argv) { shared_ptr<TTransport> socket(new TSocket("localhost", 9090)); shared_ptr<TTransport> transport(new TBufferedTransport(socket)); shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport)); //传输格式 UserStorageClient client(protocol); try { transport->open(); UserProfile user; user.id = 1; user.name = "liqb"; user.blurb = "aaaaaa"; client.store(user); UserProfile user2; client.getUser(user2, 1); printf("user.id = %d user.name = %s user.blurb = %s\n", user2.id, user2.name.c_str(), user2.blurb.c_str()); transport->close(); } catch (TException &tx) { printf("ERROR: %s\n", tx.what()); } }
Makefile: 编译C++ 服务端和C++客户端
#BOOST_DIR = /usr/local/boost/include/boost-1_33_1/ THRIFT_DIR = /usr/local/include/thrift LIB_DIR = /usr/local/lib GEN_SRC = UserStorage.cpp demo_constants.cpp demo_types.cpp default: server client server: UserStorage_server.cpp g++ -o CppServer -I../gen-cpp -I${THRIFT_DIR} -L${LIB_DIR} -lthrift UserStorage_server.cpp ${GEN_SRC} client: CppClient.cpp g++ -o CppClient -I../gen-cpp -I${THRIFT_DIR} -L${LIB_DIR} -lthrift CppClient.cpp ${GEN_SRC} clean: $(RM) -r CppClient CppServer
Java客户端实现
到thrift- x.x.x/tutorial/java/src/拷贝:JavaClient.java
重命名为:UserProfileClient.java
import org.apache.thrift.TException; import org.apache.thrift.transport.TSSLTransportFactory; import org.apache.thrift.transport.TTransport; import org.apache.thrift.transport.TSocket; import org.apache.thrift.transport.TSSLTransportFactory.TSSLTransportParameters; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.protocol.TProtocol; public class UserProfileClient { public static void main(String [] args) { /*if (args.length != 1) { System.out.println("Please enter 'simple' or 'secure'"); System.exit(0); } */ try { TTransport transport; //if (args[0].contains("simple")) { transport = new TSocket("localhost", 9090); transport.open(); //} //else { /* * Similar to the server, you can use the parameters to setup client parameters or * use the default settings. On the client side, you will need a TrustStore which * contains the trusted certificate along with the public key. * For this example it's a self-signed cert. */ //TSSLTransportParameters params = new TSSLTransportParameters(); //params.setTrustStore("../../lib/java/test/.truststore", "thrift", "SunX509", "JKS"); /* * Get a client transport instead of a server transport. The connection is opened on * invocation of the factory method, no need to specifically call open() */ //transport = TSSLTransportFactory.getClientSocket("localhost", 9091, 0, params); //} TProtocol protocol = new TBinaryProtocol(transport); UserStorage.Client client = new UserStorage.Client(protocol); int uid=123; System.out.println(client.getUser(uid)); UserProfile u = new UserProfile(); u.id=999; u.name="kaining"; u.blurb="test 999"; client.store(u); transport.close(); } catch (TException x) { x.printStackTrace(); } } }
编译Java客户端需要使用Thrift Java 库,可以使用Maven
<dependency> <groupId>org.apache.thrift</groupId> <artifactId>libthrift</artifactId> <version>0.9.3</version> </dependency>
编译成功后便可以通讯了
Thrift介绍文档下载:
http://pan.baidu.com/s/1qXx7n7Q" target=_blank>http:// http://pan.baidu.com/s/1qXx7n7Q Thrift 官方Demo
http://thrift.apache.org/tutorial/
相关文章推荐
- thrift C++做server, C++,python, java做Client例子
- Thrift c++ server / java client配置
- thrift C++做server, C++,python, java做Client例子
- java 和 C++ Socket通信(java作为服务端server,C++作为客户端client,解决中文乱码问题GBK和UTF8)
- thrift 安装 C++server 及PHP Client 测试Demo
- java和python的结合------python编写的udpserver,java语言写的udpclient
- Java Client 与C++ Servertcp通信 demo
- TCP/UDP client/server library for Java, 最好的java语言tcp udp 服务器客户端实现库
- java 和 C++ Socket通信(java作为服务端server,C++作为客户端client,解决中文乱码问题GBK和UTF8)
- java 和 C++ Socket通信(java作为服务端server,C++作为客户端client,解决中文乱码问题GBK和UTF8)
- gsoap的c++ server和java client实现
- 使用thrift实现了Javaserver和nodejsclient之间的跨平台通信
- java 和 C++ Socket通信(java作为服务端server,C++作为客户端client,解决中文乱码问题GBK和UTF8)
- java 和 C++ Socket通信(java作为服务端server,C++作为客户端client,解决中文乱码问题GBK和UTF8)
- java 和 C++ Socket通信(java作为服务端server,C++作为客户端client,解决中文乱码问题GBK和UTF8)
- C++程序员学Java系列之三五:一个简易的Client,Server样例
- C/C++和Java混合语言编程
- 请大家付责任的说话——C++/C#,java某种语言强,某种语言弱
- A Generic Client/Server Architecture for Java @ JDJ
- C++ JAVA .NET 哪种语言在企业常用