windows第一次搭建thrift服务器(c++)
- 第一次使用thrift,网上找了很多资源,最后自己总结一下。我这里搭建的是c++版本的,在vs2015建立空的控制台项目,然后调用libthrift.lib实现
准备工作:
先官网下载thrift源码,我下载的0.10.0版本解压后进入lib/cpp目录,用vs2015打开sln项目,里面如图!
可以看到就只有两个lib工程(1个阻塞的libthrift,另一个是非阻塞的libthriftnb)
到这一步了,如果直接点生成,肯定会报很多找不到XXX的错误,接下来解决错误
先把环境配置好,怎么配呢,很简单,这里不需要配置什么属性啊(这里给NuGet点个赞,有了它不需要配置属性,可从此vs变得超级便利,我感觉)。步骤如图
这里如果用的阻塞的就只要添加boost和openssl就行,如果用非阻塞还得多加一个libevent的库。把这些库添加进来后,会发现那些找不到boost的h文件的错误都消失了。这时候可以生成lib库了(这里注意自己的release和debug,还有win32和x64)。libthrift.lib生成好之后,下载
- 然后编写faceDector.thrift文件`
namespace cpp faceDector
struct User{
1:i64 id,
2:string name,
3:i32 age,
4:bool vip
}
service FaceDector{
i32 add(1:i32 a,2:i32 b)
string faceDector(1:string username,2:string password)
}`
- 然后用命令行生成一堆h文件和cpp文件。命令行如下图
- 会在gen-cpp文件夹下生成的文件
- 看一下FaceDector_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 "FaceDector.h"
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/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;
using namespace ::faceDector;
class FaceDectorHandler : virtual public FaceDectorIf {
public:
FaceDectorHandler() {
// Your initialization goes here
}
int32_t add(const int32_t a, const int32_t b) {
// Your implementation goes here
printf("add\n");
return 1;
}
void faceDector(std::string& _return, const std::string& username, const std::string& password) {
// Your implementation goes here
printf("faceDector\n");
}
};
int main(int argc, char **argv) {
int port = 9090;
shared_ptr<FaceDectorHandler> handler(new FaceDectorHandler());
shared_ptr<TProcessor> processor(new FaceDectorProcessor(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;
}
- 把生成的这一堆文件复制到开始建立的空项目里面,同样的把boost和openssl的库添加进来,然后还把原来生成的libthrift.lib 配置进来:
- 属性–>链接器–>常规–>附加库目录
. 属性–>链接器–>常规–>输入
.
属性–>c/c++–>常规–>附加包含目录
看下整体目录结构
include文件夹下是把原来thrift-0.10.0\lib\cpp\src目录下的thrift复制过来的,因为这里面保函了thrift的h文件和cpp文件
到此配置完毕,点编译运行,接着报错(我在这里耗了好久好久),错误如下
看到这里第一反应就是还少库,毕竟第一次弄,也不知道到底少什么库。最后的发现的原因是:是不是觉得很狗血,这是官网下的源码,有这个坑。把lib/cpp/server下的所有文件添加到thrift项目后,再生成lib,再复制过来调用就没问题了,服务器就启动成功接下来就是客户端了,服务端的代码是命令行生成的,客户端得自己写一个client.cpp,重新建立空项目,把原来命令行生成的文件除了FaceDector_server.skeleton.cpp不要,全都复制进去,然后自己写个client.cpp
- #include <iostream>
#include "FaceDector.h"
#include <thrift/transport/TSocket.h>
#include <thrift/transport/TBufferTransports.h>
#include <thrift/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));
faceDector::FaceDectorClient client(protocol);
try {
transport->open();
int res = client.add(100, 200);
// 我们的代码写在这里
if(res == 1)
printf("sss");
transport->close();
}
catch(TException& tx){
printf("error:",tx.what());
}
return 0;
}
- 环境配置跟服务器那边一样的,最后启动结果证明能和服务器通信,可以调用那边的方法并能返回,意味着成功了