我查看了 Apache Thrift 发行版和 apache.org 网站,寻找示例,但没有成功。
我希望有人向我指出异步客户端的示例实现 以及经典 Apache Thrift 中的非阻塞服务器(不是来自较新的分支) Facebook),它使用“--gen cpp”。
我可以看到标题为“apache thrift C++ async client”的类似问题,但答案并未包含所有内容。
我想查看“.thrift”文件,然后是填写的服务器和相应的客户端代码。
我真的很想相信有人已经做到了这一点,但我只是没有我想象的那么好。
我知道 Facebook 版本 (fbthrift) 旨在帮助更好地做到这一点,但我对该版本的不稳定程度感到沮丧。如果有人可以向我指出一个不每天修改的 fbthrift 稳定版本,我可以考虑将其作为替代方案。
最佳答案
不确定异步客户端是什么意思,但我会尽力根据我的理解回答这个问题。
如果通过“异步客户端”,您的意思是像 Node.js 中那样谈论异步,其中执行遵循回调结构,据我所知,这不是开源 Thrift 的一部分。然而,它可以在 fbthrift
中找到。 。 Facebook 有很多与fbthrift
结合使用的工具。包括他们流行的开源 C++ 库 folly
。通过 thrift C++ 客户端接口(interface)调用其他 thrift 服务必须阻塞。
这是我在尝试异步非阻塞服务器时开始使用的代码。希望对您有所帮助!
something.thrift
#!/usr/local/bin/thrift --gen cpp
namespace cpp something
service Something {
i32 ping()
}
SomethingServer.cpp
#include "gen-cpp/Something.h"
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/server/TThreadedServer.h>
#include <thrift/server/TNonblockingServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>
#include <thrift/concurrency/ThreadManager.h>
#include <iostream>
using std::cout;
using std::endl;
class SomethingHandler : virtual public something::SomethingIf {
public:
SomethingHandler() {
cout << "Initialized" << endl;
}
int32_t ping() override {
// Your implementation goes here
cout << "Ping!" << endl;
return 1;
}
};
int main(int argc, char **argv) {
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;
using namespace ::something;
int port = 9090;
shared_ptr<SomethingHandler> handler(new SomethingHandler());
shared_ptr<TProcessor> processor(new SomethingProcessor(handler));
shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
// using thread pool with maximum 15 threads to handle incoming requests
shared_ptr<ThreadManager> threadManager
= ThreadManager::newSimpleThreadManager(15);
shared_ptr<PosixThreadFactory> threadFactory
= shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
threadManager->threadFactory(threadFactory);
threadManager->start();
TNonblockingServer server(processor, protocolFactory, port, threadManager);
server.serve();
return 0;
}
SomethingClient.cpp
#include "Something.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 namespace Test;
int main(int /* argc */, char** /* argv */) {
boost::shared_ptr<TSocket> socket(new TSocket("localhost", 9090));
boost::shared_ptr<TTransport> transport(new TFramedTransport(socket));
boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
SomethingClient client(protocol);
transport->open();
for (auto i = 0; i < 10000; ++i) {
client.ping();
}
transport->close();
return 0;
}
关于c++ - Thrift 异步 C++ 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44223453/