【问题标题】:thrift async c++ example节俭异步 C++ 示例
【发布时间】:2017-05-29 18:03:20
【问题描述】:

我查看了 Apache Thrift 发行版和 apache.org 网站,寻找示例,但没有成功。

我希望有人指出异步客户端的示例实现 和经典 Apache Thrift 中的非阻塞服务器(不是来自 Facebook),它使用“--gen cpp”。

我可以看到标题为“apache thrift C++ 异步客户端”的类似问题,但答案并未包含所有内容。

我想看看“.thrift”文件,然后是填写的服务器,以及对应的客户端代码。

我真的很想相信有人这样做了,而我只是不像我想象的那样优秀的谷歌用户。

我了解 Facebook 版本 (fbthrift) 旨在帮助更好地做到这一点,但我对该版本的不稳定程度感到沮丧。如果有人可以向我指出没有每天修改的稳定版本的 fbthrift,我可以考虑将其作为替代方案。

【问题讨论】:

    标签: c++ asynchronous thrift


    【解决方案1】:

    不确定您所说的异步客户端是什么意思,但我会根据我的理解尽力回答这个问题。

    如果通过“异步客户端”,您的意思是在 node.js 中谈论异步,其中执行遵循回调结构,AFAIK 不是开源节俭的一部分。但是,它在fbthrift 中可用。 Facebook 有很多与fbthrift 结合使用的工具,包括他们流行的开源 C++ 库folly。通过 thrift C++ 客户端接口调用其他 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;
    }
    

    【讨论】:

    • 我很高兴看到这段代码。这正是我正在寻找的那种例子,因为它似乎不是来自“fbthrift”,并且它不依赖于我们希望避免的包(如“folly”)。今晚晚些时候我将尝试这个例子,但我现在想花时间感谢你!更多信息稍后...
    • 进一步阅读后,您的示例代码似乎没有实现任何类型的回调。我认为您试图告诉我,我必须使用 fbthrift 成功创建一个立即返回的客户端调用,并在服务器完成其任务时结合回调机制。这是你的理解吗?
    • @GordonFossum 是的,这就是我从中得到的,也是我看到的节俭问题。 fbthrift 使用带有回调支持的愚蠢期货来启用基于回调的处理。 Boost futures 确实有这个功能,但我认为还没有人在开源节俭中实现过这个功能。
    猜你喜欢
    • 2013-06-29
    • 2018-01-29
    • 2012-12-12
    • 1970-01-01
    • 2012-11-28
    • 2019-03-31
    • 2011-01-07
    • 1970-01-01
    • 2015-10-18
    相关资源
    最近更新 更多