zoukankan      html  css  js  c++  java
  • Thrift的C++服务端(线程池和非阻塞)模式

    非阻塞模式

    #include "RpcServiceHandler.h"
    
    #include <thrift/concurrency/ThreadManager.h>
    #include <thrift/concurrency/PosixThreadFactory.h>
    #include <thrift/protocol/TBinaryProtocol.h>
    #include <thrift/server/TThreadPoolServer.h>
    #include <thrift/server/TNonblockingServer.h>
    #include <thrift/server/TThreadedServer.h>
    #include <thrift/transport/TServerSocket.h>
    #include <thrift/transport/TBufferTransports.h>
    #include <thrift/TToString.h>
    
    int main(int argc, char **argv)
    {
        RpcServiceHandler *rpcServiceHanlder = new RpcServiceHandler();
    
        int port = CFG()->getInt(kCfgProcPort);
        int workerCount = CFG()->getInt(kCfgProcWCnt);
    
        boost::shared_ptr<RpcServiceHandler> handler(rpcServiceHanlder);
        boost::shared_ptr<TProcessor> processor(new RpcServiceProcessor(handler));
        boost::shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
        boost::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
    
        boost::shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workerCount);
        boost::shared_ptr<PosixThreadFactory> threadFactory = boost::shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
        threadManager->threadFactory(threadFactory);
        threadManager->start();
        
        TNonblockingServer server(processor,    
                                  protocolFactory,    
                                  port,    
                                  threadManager);
    
        std::cout << "Starting the server..." << std::endl;
    
        server.serve();
        return 0;
    }
    

      

    线程池模式

    #include "RpcServiceHandler.h"
    
    #include <thrift/concurrency/ThreadManager.h>
    #include <thrift/concurrency/PosixThreadFactory.h>
    #include <thrift/protocol/TBinaryProtocol.h>
    #include <thrift/server/TThreadPoolServer.h>
    #include <thrift/server/TNonblockingServer.h>
    #include <thrift/server/TThreadedServer.h>
    #include <thrift/transport/TServerSocket.h>
    #include <thrift/transport/TBufferTransports.h>
    #include <thrift/TToString.h>
    
    int main(int argc, char **argv)
    {
        RpcServiceHandler *rpcServiceHanlder = new RpcServiceHandler();
    
        int port = CFG()->getInt(kCfgProcPort);
        int workerCount = CFG()->getInt(kCfgProcWCnt);
    
        boost::shared_ptr<RpcServiceHandler> handler(rpcServiceHanlder);
        boost::shared_ptr<TProcessor> processor(new RpcServiceProcessor(handler));
        boost::shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
        boost::shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
        boost::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
    
        boost::shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workerCount);
        boost::shared_ptr<PosixThreadFactory> threadFactory = boost::shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
        threadManager->threadFactory(threadFactory);
        threadManager->start();
        
        TThreadPoolServer server(processor,
                                 serverTransport,
                                 transportFactory,
                                 protocolFactory,
                                 threadManager);
    
        std::cout << "Starting the server..." << std::endl;
    
        server.serve();
        return 0;
    }
    

    单独Server模式

    #include "RpcServiceHandler.h"
    
    #include <thrift/protocol/TBinaryProtocol.h>
    #include <thrift/server/TSimpleServer.h>
    #include <thrift/transport/TServerSocket.h>
    #include <thrift/transport/TBufferTransports.h>
    #include <thrift/TToString.h>
    
    int main(int argc, char **argv)
    {
        RpcServiceHandler *rpcServiceHanlder = new RpcServiceHandler();
    
        int port = CFG()->getInt(kCfgProcPort);
    
        boost::shared_ptr<RpcServiceHandler> handler(rpcServiceHanlder);
        boost::shared_ptr<TProcessor> processor(new RpcServiceProcessor(handler));
        boost::shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
        boost::shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
        boost::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
    
    
        std::cout << "Starting the server..." << std::endl;
        TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
    
        server.serve();
        return 0;
    }
    

      

  • 相关阅读:
    jQuery同步Ajax带来的UI线程阻塞问题及解决办法
    jQuery的deferred对象详解
    原生js,jquery ajax请求以及jsonp的调用
    vue10行代码实现上拉翻页加载更多数据,纯手写js实现下拉刷新上拉翻页不引用任何第三方插件
    js判断手机或Pc端登陆.并跳转到相应的页面
    移动端touch事件封装
    坦然面对:应对前端疲劳
    webpack2 项目
    PPK提供的浏览器类型及版本检测方法
    2013年五大主流浏览器 HTML5 和 CSS3 兼容性大比拼
  • 原文地址:https://www.cnblogs.com/voipman/p/7519657.html
Copyright © 2011-2022 走看看