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;
}
Thrift的C++服务端(线程池和非阻塞)模式的更多相关文章
- 将服务端select设置为非阻塞,处理更多业务
服务端代码: #include<WinSock2.h> #include<Windows.h> #include<vector> #include<stdio ...
- Python中的Tcp协议应用之TCP服务端-线程版
利用线程实现,一个服务端同时服务多个客户端的需求. TCP服务端-线程版代码实现: import socket import threading def handle_client_socket(ne ...
- Java线程池(Callable+Future模式)
转: Java线程池(Callable+Future模式) Java线程池(Callable+Future模式) Java通过Executors提供四种线程池 1)newCachedThreadPoo ...
- springBoot服务整合线程池ThreadPoolTaskExecutor与@Async详解使用
ThreadPoolExecutor:=======这个是java自己实现的线程池执行类,基本上创建线程池都是通过这个类进行的创建.ThreadPoolTaskExecutor:========这个是 ...
- 设计模式:基于线程池的并发Visitor模式
1.前言 第二篇设计模式的文章我们谈谈Visitor模式. 当然,不是简单的列个的demo,我们以电商网站中的购物车功能为背景,使用线程池实现并发的Visitor模式,并聊聊其中的几个关键点. 一,基 ...
- spring线程池ThreadPoolTaskExecutor与阻塞队列BlockingQueue
一: ThreadPoolTaskExecutor是一个spring的线程池技术,查看代码可以看到这样一个字段: private ThreadPoolExecutor threadPoolExecut ...
- java多线程:线程池原理、阻塞队列
一.线程池定义和使用 jdk 1.5 之后就引入了线程池. 1.1 定义 从上面的空间切换看得出来,线程是稀缺资源,它的创建与销毁是一个相对偏重且耗资源的操作,而Java线程依赖于内核线程,创建线程需 ...
- 线程池ThreadPoolExecutor与阻塞队列BlockingQueue应用
1.线程池介绍 JDK5.0以上: java.util.concurrent.ThreadPoolExecutor 构造函数签名: public ThreadPoolExecutor( int co ...
- ThreadPool.QueueUserWorkItem引发的血案,线程池异步非正确姿势导致程序闪退的问题
ThreadPool是.net System.Threading命名空间下的线程池对象.使用QueueUserWorkItem实现对异步委托的先进先出有序的回调.如果在回调的方法里面发生异常则应用程序 ...
随机推荐
- Django ListView实现分页
效果: url.py main-urls from django.urls import path,include urlpatterns = [ path('admin/', admin.site. ...
- 运维笔记--docker高效查看后台日志
场景描述: 应用程序运行在 Docker环境中,经常使用的查看后台日志的命令是:docker attach 容器名该命令优点:实时输出:不足之处:日志大量输出的时候,屏幕一闪而过,不便于调试,并且有一 ...
- [转]MySQL 清空慢查询文件
概述 本章主要写当慢查询文件很大的时候怎样在线生成一个新的慢查询文件. 测试环境:mysql 5.6.21 步骤 配置慢查询 默认的my.cnf文件在/etc/目录下 vim /etc/my.cnf ...
- 微信小程序内嵌业务域名内的网页
微信小程序在2017年11月左右开放了内嵌网页的功能,即新组件<web-view>.官方文档链接:https://mp.weixin.qq.com/debug/wxadoc/dev/com ...
- mysql数据库单表只有一个主键自增id字段,ibatis实现id自增
mysql数据库单表只有一个主键自增id字段,ibatis实现id自增 <insert id="autoid"> insert into user_id ...
- Facebook ATC弱网环境搭建
用户的网络环境千姿百态,弱网的.高延时的.丢包的.常有用户反馈偶发bug,我们测试人员却始终无法复现,根据用户的描述,开发排查可能是网络不稳定导致的,所以急需建个弱网环境来测试. 弱网工具简介: Au ...
- 第三方工具系列--Lombok常用注解
原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9329798.html Lombok注解解析: @NonNull 使用在方法的参数或者构造 ...
- MyBatis源码解析(五)——DataSource数据源模块之非池型数据源
原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/6675633.html 1 回顾 上一篇中我解说了数据源接口DataSource与数据源工厂 ...
- Perl一行式:处理空白符号
perl一行式程序系列文章:Perl一行式 假如文件file.log内容如下: root x 0 0 root /root /bin/bash daemon x 1 1 daemon /usr/sbi ...
- 翻译:low_priority和high_priority(已提交到MariaDB官方手册)
本文为mariadb官方手册:HIGH_PRIORITY and LOW_PRIORITY的译文. 原文:https://mariadb.com/kb/en/high_priority-and-low ...