Apache Thrift 在Windows下的安装与开发实例(问题解决)

1、参考信息

安装参考:Apache Thrift 在Windows下的安装与开发:
https://blog.csdn.net/colouroo/article/details/38588297
我的环境:
OS:Win7
VS:VS2012
libevent:libevent-2.0.22-stable(http://libevent.org/)
boost:boost_1_64_0
注:我尝试了thrift-0.12.0.tar但问题较多,所有还是先使用thrift-0.9.1.tar

2、按照参考安装时遇到的问题

1、libthrift工程属性->包含目录中的$(THIRD_PARTY)\openssl\OpenSSL-Win32\include\可以不管,openssl貌似没用到
2、按照自己的boost安装目录和libevent目录修改包含目录和库目录
3、编译遇到boost智能指针和std智能指针混淆无法转换的问题,修改\lib\cpp\src\thrift\windows\config.h中相关信息如下:

// use std::thread in MSVC11 (2012) or newer
//#if _MSC_VER >= 1700
//#  define USE_STD_THREAD 1
// otherwise use boost threads
//#else
#  define USE_BOOST_THREAD 1
//#endif

4、参考中的例子中hello.thrift文件内容修改如下带参数的形式

service hello {
    string func1(1:i32 key, 2:string value)
}

5、参考中第6步的例子代码client.cpp修改为

#include "boost/shared_ptr.hpp"
#include <protocol/TBinaryProtocol.h>
#include <server/TSimpleServer.h>
#include <transport/TServerSocket.h>
#include <transport/TBufferTransports.h>
#include <transport/TSocket.h>
#include <string>
#include "gen-cpp/hello.h"

#pragma comment(lib, "libthrift.lib")
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;
using boost::shared_ptr;
int main(int argc, char** argv) {
	int port = 9090;
	shared_ptr<TSocket> socket(new TSocket("127.0.0.1", 9090));
	shared_ptr<TBufferedTransport> transport(new TBufferedTransport(socket));
	shared_ptr<TBinaryProtocol> protocol(new TBinaryProtocol(transport));
	helloClient client(protocol);
	try{
		transport->open();
		std::string outStr;
		std::string inStr("cuixy");
		int32_t inInt = 32;
		client.func1(outStr,inInt, inStr);
		printf("Client: ServerReturn:%s\n", outStr.c_str());
		transport->close();
	}catch(TException& tx){
		printf("ERROR:%s\n",tx.what());
	}
	getchar();
	return 0;
}

6、参考中第6步的例子代码hello_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 "hello.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;
#pragma comment(lib, "libthrift.lib")
using boost::shared_ptr;

class helloHandler : virtual public helloIf {
 public:
  helloHandler() {
    // Your initialization goes here
  }
  void func1(std::string& _return, const int32_t key, const std::string& value) {
    // Your implementation goes here
	_return = std::string("wangby");
    printf("Server :ClientInput:%s\n",value.c_str());
  }
};

int main(int argc, char **argv) {
  WORD wVersionRequested;
  WSADATA wsaData;
  int err;
  wVersionRequested =MAKEWORD( 2, 2 );
  err = WSAStartup( wVersionRequested, &wsaData );
	
  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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值