Thrift辅助类,用于简化Thrift编程
CThriftServerHelper用于服务端,CThriftClientHelper用于客户端。
IDL定义:
service PackageManagerService
{
}
服务端使用示例:
CThriftServerHelper<CPackageManagerHandler, PackageManagerServiceProcessor> _thrift_server_helper;
return _thrift_server_helper.serve(FLAGS_package_port, rpc_threads);
客户端使用示例:
CThriftClientHelper<PackageManagerServiceClient> thrift_client_helper(FLAGS_package_ip, FLAGS_package_port);
thrift_client_helper.connect(); // 注意需要处理异常TTransportException/TApplicationException/TException
#include <arpa/inet.h>
#include <thrift/concurrency/PosixThreadFactory.h>
#include <thrift/concurrency/ThreadManager.h>
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TNonblockingServer.h>
#include <thrift/transport/TSocketPool.h>
#include <thrift/transport/TTransportException.h>
#include <thrift/async/TAsyncChannel.h>
using namespace apache;
// 用来判断thrift是否已经连接,包括两种情况:
// 1.从未连接过,也就是还未打开过连接
// 2.连接被对端关闭了
inline bool thrift_not_connected(
thrift::transport::TTransportException::TTransportExceptionType type)
{
return (thrift::transport::TTransportException::NOT_OPEN == type)
|| (thrift::transport::TTransportException::END_OF_FILE == type);
}
// 封装对thrift服务端的公共操作
template <class ThriftHandler, class ServiceProcessor>
class CThriftServerHelper
{
public:
// 启动rpc服务,请注意该调用是同步阻塞的,所以需放最后调用
bool serve(uint16_t port);
bool serve(uint16_t port, uint8_t num_threads);
bool serve(const std::string& ip, uint16_t port, uint8_t num_threads);
void stop();
private:
boost::shared_ptr<ThriftHandler> _handler;
boost::shared_ptr<thrift::TProcessor> _processor;
boost::shared_ptr<thrift::protocol::TProtocolFactory> _protocol_factory;
boost::shared_ptr<thrift::server::ThreadManager> _thread_manager;
boost::shared_ptr<thrift::concurrency::PosixThreadFactory> _thread_factory;
boost::shared_ptr<thrift::server::TServer> _server;
};
// 封装对thrift客户端的公共操作
template <class ThriftClient>
class CThriftClientHelper
{
public:
CThriftClientHelper(const std::string& host, uint16_t port
, int timeout = RPC_TIMEOUT);
~CThriftClientHelper();
void connect();
void close();
ThriftClient* operator ->() const
{
return _container_client.get();
}
ThriftClient* get()
{
return _container_client.get();
}
private:
std::string _host;
uint16_t _port;
int _timeout;
boost::shared_ptr<thrift::transport::TSocketPool> _sock_pool;
boost::shared_ptr<thrift::transport::TTransport> _socket;
boost::shared_ptr<thrift::transport::TFramedTransport> _transport;
boost::shared_ptr<thrift::protocol::TProtocol> _protocol;
boost::shared_ptr<ThriftClient> _container_client;
};
///////////////////////////////////////////////////////////////////////////////
template <class ThriftHandler, class ServiceProcessor>
bool CThriftServerHelper<ThriftHandler, ServiceProcessor>::serve(uint16_t port)
{
return serve("0.0.0.0", port, 1);
}
template <class ThriftHandler, class ServiceProcessor>
bool CThriftServerHelper<ThriftHandler, ServiceProcessor>::serve(uint16_t port, uint8_t num_threads)
{
return serve("0.0.0.0", port, num_threads);
}
template <class ThriftHandler, class ServiceProcessor>
bool CThriftServerHelper<ThriftHandler, ServiceProcessor>::serve(const std::string& ip, uint16_t port, uint8_t num_threads)
{
try
{
_handler.reset(new ThriftHandler);
_processor.reset(new ServiceProcessor(_handler));
_protocol_factory.reset(new thrift::protocol::TBinaryProtocolFactory());
_thread_manager = thrift::server::ThreadManager::newSimpleThreadManager(num_threads);
_thread_factory.reset(new thrift::concurrency::PosixThreadFactory());
_thread_manager->threadFactory(_thread_factory);
_thread_manager->start();
_server.reset(new thrift::server::TNonblockingServer(
_processor, _protocol_factory, port, _thread_manager));
_server->serve();
}
catch (thrift::TException& tx)
{
LOG(ERROR) << "start thrift error: " << tx.what();
return false;
}
LOG(INFO) << "container-thrift start";
return true;
}
template <class ThriftHandler, class ServiceProcessor>
void CThriftServerHelper<ThriftHandler, ServiceProcessor>::stop()
{
_server->stop();
}
///////////////////////////////////////////////////////////////////////////////
template <class ThriftClient>
CThriftClientHelper<ThriftClient>::CThriftClientHelper(
const std::string& host, uint16_t port, int timeout)
: _host(host)
, _port(port)
, _timeout(timeout)
{
_sock_pool.reset(new thrift::transport::TSocketPool());
_sock_pool->addServer(host, port);
_sock_pool->setConnTimeout(timeout);
_sock_pool->setRecvTimeout(timeout);
_sock_pool->setSendTimeout(timeout);
_socket = _sock_pool;
_transport.reset(new thrift::transport::TFramedTransport(_socket));
_protocol.reset(new thrift::protocol::TBinaryProtocol(_transport));
_container_client.reset(new ThriftClient(_protocol));
}
template <class ThriftClient>
CThriftClientHelper<ThriftClient>::~CThriftClientHelper()
{
close();
}
template <class ThriftClient>
void CThriftClientHelper<ThriftClient>::connect()
{
if (!_transport->isOpen())
{
_transport->open();
}
}
template <class ThriftClient>
void CThriftClientHelper<ThriftClient>::close()
{
if (_transport->isOpen())
{
_transport->close();
}
}
Thrift辅助类,用于简化Thrift编程的更多相关文章
- 使用任务Task 简化异步编程
使用任务简化异步编程 Igor Ostrovsky 下载代码示例 异步编程是实现与程序其余部分并发运行的较大开销操作的一组技术. 常出现异步编程的一个领域是有图形化 UI 的程序环境:当开销较大的操作 ...
- 第十节:利用async和await简化异步编程模式的几种写法
一. async和await简介 PS:简介 1. async和await这两个关键字是为了简化异步编程模型而诞生的,使的异步编程跟简洁,它本身并不创建新线程,但在该方法内部开启多线程,则另算. 2. ...
- 关于thrift的一些探索——thrift序列化技术
thrift的IDL,相当于一个钥匙.而thrift传输过程,相当于从两个房间之间的传输数据. 图-1 (因为Thrift采用了C/S模型,不支持双向通信:client只能远程调用server端的RP ...
- 示例:WPF中自定义StoryBoarService在代码中封装StoryBoard、Animation用于简化动画编写
原文:示例:WPF中自定义StoryBoarService在代码中封装StoryBoard.Animation用于简化动画编写 一.目的:通过对StoryBoard和Animation的封装来简化动画 ...
- 简化Java编程的法宝,让工作更高效
如果你没有看过之前的文章,也不要紧,这并不影响你对接下来的内容的理解,不过为了照顾直接看到第二篇的同学,还是有必要介绍一下HuTool的引入方式. 在项目的pom.xml的dependencies中加 ...
- Thrift RPC实战(三) thrift序列化揭秘
本文主要讲解Thrift的序列化机制, 看看thrift作为数据交换格式是如何工作的? 1.构造应用场景: 1). 首先我们先来定义下thrift的简单结构. 1 2 3 4 5 namespace ...
- NGK与Captain technology合作 推出贷款体验用于简化汽车经销商流程
据外媒报导,近日,NGK.IO正在与Captain technology恰谈合作事宜,以简化购车体验,包括简化购车流程.NGK的CTO Stephen Litan表示:"NGK宣布与Capt ...
- Thrift RPC实战(二) Thrift 网络服务模型
限于篇幅关系,在观察源码的时候,只列举了部分源代码 TServer类层次体系 TSimpleServer/TThreadPoolServer是阻塞服务模型 TNonblockingServer/THs ...
- Thrift入门初探(2)--thrift基础知识详解
昨天总结了thrift的安装和入门实例,Thrift入门初探--thrift安装及java入门实例,今天开始总结一下thrift的相关基础知识. Thrift使用一种中间语言IDL,来进行接口的定义, ...
随机推荐
- 枚举生成1~n的排序
/*枚举生成1~n的排列*/ #include <iostream> #include<algorithm> #include<queue> #include< ...
- react组件生命
组件的生命周期主要由三个部分组成: Mounting:组件正在被插入DOM中 Updating:如果DOM需要更新,组件正在被重新渲染 Unmounting:组件从DOM中移除 React提供了方法, ...
- 【转】详解Data Binding 通过几个简单示例深入了解WinForm数据绑定特性
原文地址:http://www.cnblogs.com/lichence/archive/2012/02/17/2356001.html
- mongodb 安装、windows服务、创建用户
http://www.cnblogs.com/best/p/6212807.html 打开MongoDB的安装目录如“C:\Program Files\MongoDB\Server\3.4\bin”, ...
- Linux内核系统调用列表
一.进程控制: fork 创建一个新进程 clone 按指定条件创建子进程 execve 运行可执行文件 exit 中止进程 _exit 立即中止当前进程 getdtablesize 进程所能打开的最 ...
- tornado 自定义session (二)
有了上一步的基础,我们将session改造成一个模块,这样我们就可以通过配置,来使用不同方式(redis,数据库等)的session. 添加一个新目录:TornadoSession conf.py是配 ...
- Angularjs Ng_repeat中实现复选框选中并显示不同的样式
最近做了一个选择标签的功能,把一些标签展示给用户,用户选择自己喜欢的标签,就类似我们在购物网站看到的那种过滤标签似的: 简单的效果如图所示: 首先看一下html代码: 1 <!DOCTYPE h ...
- npm安装elasticsearch-reindex
由于新版的nodejs已经集成了npm,所以之前npm也一并安装好了.同样可以通过输入 "npm -v" 来测试是否成功安装. npm -v 你可以使用以下命令来查看所有全局安装的 ...
- 前端开发之HTML篇一
主要内容: 一.HTML简介 二.HTML标签 三.HTML文档结构和注释 四.head标签及相关内容 五.body标签及相关内容 1️⃣ HTM ...
- Kafka集群中 topic数据的分区 迁移到其他broker
前言 kafka集群扩容后,新的broker上面不会数据进入这些节点,也就是说,这些节点是空闲的:它只有在创建新的topic时才会参与工作.除非将已有的partition迁移到新的服务器上面:所以需要 ...