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;
    }
    

      

  • 相关阅读:
    如何在帮助页面添加测试工具
    如何给你的ASP.NET页面添加HelpPage
    各种序列化库的性能数据
    Quartz.NET配置
    T-SQL中只截取日期的日期部分和日期的时间部分
    sql 根据指定条件获取一个字段批量获取数据插入另外一张表字段中+MD5加密
    读取图片数据流转换成图片
    T-SQL Transact-SQL 编程
    Python 链接Mysql数据库
    c 生成随机不重复的整数序列
  • 原文地址:https://www.cnblogs.com/voipman/p/7519657.html
Copyright © 2011-2022 走看看