由浅入深了解Thrift之客户端连接池化续
前文《由浅入深了解Thrift之客户端连接池化》中我们已经实现了服务调用端 连接的池化,实现的过于简陋,离实际的项目运用还很遥远。本文将在进一步改造,主要是两方面:1、服务端如何注册多个服务 2、调用端如何获取服务对象而不是服务连接
一、实现思路
1、通过spring配置文件,配置服务类
2、反射生成服务类实例,依次注册服务
调用端获取服务对象亦是如此,废话不多说了
二、主要实现
1、服务端
/**
* thrift server端,向zk中注册server address
*
* @author wy
*
*/
public class ThriftServiceServerFactory implements InitializingBean {
private final Logger logger = LoggerFactory.getLogger(getClass()); // thrift server 服务端口
private Integer port;
// default 权重
private Integer priority = 1;
// service 实现类
private Map<String, Object> services;
// thrift server 注册路径
private String configPath;
// thrift service module
private String configService; private ThriftServerIpTransfer ipTransfer;
// thrift server注册类
private ThriftServerAddressReporter addressReporter;
// thrift server开启服务
private ServerThread serverThread; @Override
public void afterPropertiesSet() throws Exception {
if (ipTransfer == null) {
ipTransfer = new LocalNetworkIpTransfer();
}
String ip = ipTransfer.getIp();
if (ip == null) {
throw new NullPointerException("cant find server ip...");
}
String hostName = ip + ":" + port + ":" + priority;
logger.info("registry services address = " + hostName);
logger.info("registry services count = " + services.size());
Set<Entry<String, Object>> set = services.entrySet();
Iterator<Entry<String, Object>> iter = set.iterator();
Map<String, TProcessor> processors = new HashMap<String, TProcessor>();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
for (; iter.hasNext();) {
Entry<String, Object> entry = iter.next();
// 通过反射获取service interface
String servieName = entry.getKey();
Object serviceImplObject = entry.getValue();
Class<?> serviceClass = serviceImplObject.getClass();
// 返回本类直接实现的接口.不包含泛型参数信息
Class<?>[] interfaces = serviceClass.getInterfaces();
if (interfaces.length == 0) {
throw new IllegalClassFormatException("service-class should implements Iface");
}
TProcessor processor = null;
for (Class<?> interfaceClazz : interfaces) {
logger.info("service Interfaces count = " + interfaces.length);
// 获取源代码中给出的'底层类'简称
String cname = interfaceClazz.getSimpleName();
if (!cname.equals("Iface")) {
continue;
}
// 获取外部类Class;以String的形式,返回Class对象的'实体'名称
String pname = interfaceClazz.getEnclosingClass().getName() + "$Processor";
Class<?> pclass = classLoader.loadClass(pname);
// class是否相同或是另一个类的超类或接口
if (!pclass.isAssignableFrom(Processor.class)) {
continue;
}
Constructor<?> constructor = pclass.getConstructor(interfaceClazz);
processor = (TProcessor) constructor.newInstance(serviceImplObject);
processors.put(servieName, processor);
break;
}
if (processor == null) {
throw new IllegalClassFormatException("service-class should implements Iface");
} } // 需要单独的线程,因为serve方法是阻塞的.
serverThread = new ServerThread(processors, port);
serverThread.start();
// 向ZK中注册服务
if (addressReporter != null) {
addressReporter.report(configPath, hostName);
}
} class ServerThread extends Thread {
private TServer server; ServerThread(Map<String, TProcessor> processors, int port) throws Exception {
// 设置传输通道
TNonblockingServerSocket serverTransport = new TNonblockingServerSocket(
port);
// 设置二进制协议
Factory protocolFactory = new TBinaryProtocol.Factory();
// 网络服务模型
TThreadedSelectorServer.Args tssArgs = new TThreadedSelectorServer.Args(
serverTransport);
//
TMultiplexedProcessor processor = new TMultiplexedProcessor(); Set<Entry<String, TProcessor>> set = processors.entrySet();
Iterator<Entry<String, TProcessor>> iter = set.iterator();
//
for (; iter.hasNext();) {
Entry<String, TProcessor> entry = iter.next();
processor.registerProcessor(entry.getKey(),
(TProcessor) entry.getValue());
tssArgs.processor(processor);
} tssArgs.protocolFactory(protocolFactory);
//
tssArgs.transportFactory(new TFramedTransport.Factory());
int num = Runtime.getRuntime().availableProcessors() * 2 + 1;
tssArgs.selectorThreads(num);
tssArgs.workerThreads(num * 10); server = new TThreadedSelectorServer(tssArgs); } @Override
public void run() {
try {
server.serve();
} catch (Exception e) {
e.printStackTrace();
}
} // 关闭thrift server
public void stopServer() {
if (server != null && !server.isServing()) {
server.stop();
}
}
} // 关闭thrift server
public void close() {
serverThread.stopServer();
} public void setServices(Map<String, Object> services) {
this.services = services;
} public void setPriority(Integer priority) {
this.priority = priority;
} public void setPort(Integer port) {
this.port = port;
} public void setIpTransfer(ThriftServerIpTransfer ipTransfer) {
this.ipTransfer = ipTransfer;
} public void setAddressReporter(ThriftServerAddressReporter addressReporter) {
this.addressReporter = addressReporter;
} public void setConfigPath(String configPath) {
this.configPath = configPath;
} public String getConfigService() {
return configService;
} public void setConfigService(String configService) {
this.configService = configService;
} }
spring配置文件
<!-- zookeeper -->
<bean id="thriftZookeeper" class="com.wy.thriftpool.commzkpool.zookeeper.ZookeeperFactory" destroy-method="close">
<property name="connectString" value="127.0.0.1:2181" />
<property name="namespace" value="thrift/thrift-service" />
</bean> <bean id="serviceAddressReporter" class="com.wy.thriftpool.commzkpool.support.impl.DynamicAddressReporter" destroy-method="close">
<property name="zookeeper" ref="thriftZookeeper" />
</bean> <bean id="userService" class="com.wy.thrift.service.UserServiceImpl"/> <bean class="com.wy.thriftpool.commzkpool.server.ThriftServiceServerFactory" destroy-method="close">
<property name="configPath" value="serviceAddress" />
<property name="configService" value="serviceModule" />
<property name="port" value="9090" />
<property name="addressReporter" ref="serviceAddressReporter" />
<property name="services">
<map>
<entry key="userService" value-ref="userService" />
</map>
</property>
</bean>
2、调用端
/**
* TServiceClient,工厂类
* thrift Client端负责于thrift server通信
*
* @author wy
*/
public class AbstractThriftServiceClientFactory extends BasePoolableObjectFactory<TServiceClient> {
private final Logger logger = LoggerFactory.getLogger(getClass());
// 超时设置
private int timeOut; private final ThriftServerAddressProvider addressProvider;
private final TServiceClientFactory<TServiceClient> clientFactory;
private PoolOperationCallBack callback; protected AbstractThriftServiceClientFactory(
ThriftServerAddressProvider addressProvider,
TServiceClientFactory<TServiceClient> clientFactory)
throws Exception {
this.addressProvider = addressProvider;
this.clientFactory = clientFactory;
} protected AbstractThriftServiceClientFactory(
ThriftServerAddressProvider addressProvider,
TServiceClientFactory<TServiceClient> clientFactory,
PoolOperationCallBack callback) throws Exception {
this.addressProvider = addressProvider;
this.clientFactory = clientFactory;
this.callback = callback;
} protected AbstractThriftServiceClientFactory(
ThriftServerAddressProvider addressProvider,
TServiceClientFactory<TServiceClient> clientFactory,
PoolOperationCallBack callback, int timeOut) throws Exception {
this.addressProvider = addressProvider;
this.clientFactory = clientFactory;
this.callback = callback;
this.timeOut = timeOut;
} /**
* 创建对象
*/
@Override
public TServiceClient makeObject() throws Exception {
InetSocketAddress address = addressProvider.selector();
logger.info("server address=" + address.getHostName() + ":" + address.getPort());
TSocket tsocket = new TSocket(address.getHostName(), address.getPort(), this.timeOut);
TProtocol protocol = new TBinaryProtocol(tsocket);
TServiceClient client = this.clientFactory.getClient(protocol);
tsocket.open();
if (callback != null) {
callback.make(client);
}
return client;
} /**
* 销毁对象
*/
public void destroyObject(TServiceClient client) throws Exception {
if (callback != null) {
callback.destroy(client); try {
TTransport transport = client.getInputProtocol().getTransport();
if (transport.isOpen()) {
transport.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
} /**
* 检验对象是否可以由pool安全返回
*/
public boolean validateObject(TServiceClient client) {
try {
TTransport transport = client.getInputProtocol().getTransport();
if (transport.isOpen()) {
return true;
} else {
return false;
}
} catch (Exception e) {
return false;
}
} public static interface PoolOperationCallBack {
// 创建成功前执行
void make(TServiceClient client); // 销毁client之前执行
void destroy(TServiceClient client);
} }
/**
* 服务调用方使用
*
* @author wy
*
*/
public class ThriftServiceClientProxyFactory implements FactoryBean<Object>, InitializingBean {
private final Logger logger = LoggerFactory.getLogger(getClass()); // 最大活跃连接数
private Integer maxActive = 32;
// 最小空闲对象数量
private Integer minIdle =1;
// 链接空闲时间default 3
private Integer idleTime = 1000 * 60 * 3;
// 连接超时配置
private int conTimeOut = 1000 * 2;
//
private boolean testOnBorrow = true;
//
private boolean testOnReturn = true;
//
private boolean testWhileIdle = true; // 具体的服务接口类
private String service;
// fixed server address provider
private String serverAddress;
// dynamic server address provider
private ThriftServerAddressProvider addressProvider;
// 对象缓存池
private GenericObjectPool<TServiceClient> pool; // 服务实例
private Object proxyClient;
// 服务接口类Iface
private Class<?> objectClass; @Override
public void afterPropertiesSet() throws Exception {
logger.info("init objectPool..."); if (serverAddress != null) {
logger.info("FixedAddressProvider...");
addressProvider = new FixedAddressProvider(serverAddress);
} else {
logger.info("DynamicAddressProvider...");
} ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
// 加载Iface接口
objectClass = classLoader.loadClass(service + "$Iface");
// 加载Client.Factory类
Class<TServiceClientFactory<TServiceClient>> clazz = (Class<TServiceClientFactory<TServiceClient>>)
classLoader.loadClass(service + "$Client$Factory");
TServiceClientFactory<TServiceClient> clientFactory = clazz.newInstance();
// PoolFactory
AbstractThriftServiceClientFactory clientPool = new AbstractThriftServiceClientFactory(
addressProvider, clientFactory, callback, conTimeOut);
// 对象缓存池
GenericObjectPool.Config poolConfig = new GenericObjectPool.Config();
// 缓存池中分配对象的最大数量
poolConfig.maxActive = maxActive;
// 缓存池中最小空闲对象数量
poolConfig.minIdle = minIdle;
//
poolConfig.minEvictableIdleTimeMillis = idleTime;
//
poolConfig.timeBetweenEvictionRunsMillis = idleTime / 2L;
//
poolConfig.testOnBorrow = testOnBorrow;
//
poolConfig.testOnReturn = testOnReturn;
//
poolConfig.testWhileIdle = testWhileIdle; pool = new GenericObjectPool<TServiceClient>(clientPool, poolConfig); // 反射,服务实例
proxyClient = Proxy.newProxyInstance(classLoader,
new Class[] { objectClass }, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method,
Object[] args) throws Throwable {
// 从对象池取对象
TServiceClient client = pool.borrowObject();
try {
return method.invoke(client, args);
} catch (Exception e) {
throw e;
} finally {
// 将对象放回对象池
pool.returnObject(client);
}
}
});
} /**
* 获取服务实例,并已归还池中对象
*/
@Override
public Object getObject() throws Exception {
return proxyClient;
} /**
* 销毁对象池
*
* @throws Exception
*/
public void destroyObject() throws Exception {
try {
pool.close();
} catch (Exception e) {
throw new RuntimeException("erorr destroy()", e);
}
} /**
* 关闭server address provider
*/
public void close() {
if (addressProvider != null) {
addressProvider.close();
}
} @Override
public boolean isSingleton() {
// To change body of implemented methods use File | Settings | File Templates.
return true;
} @Override
public Class<?> getObjectType() {
return objectClass;
} PoolOperationCallBack callback = new PoolOperationCallBack() { @Override
public void make(TServiceClient client) {
logger.info("create proxyClient...");
} @Override
public void destroy(TServiceClient client) {
logger.info("destroy proxyClient...");
}
}; public void setMaxActive(Integer maxActive) {
this.maxActive = maxActive;
} public void setIdleTime(Integer idleTime) {
this.idleTime = idleTime;
} public void setService(String service) {
this.service = service;
} public void setServerAddress(String serverAddress) {
this.serverAddress = serverAddress;
} public void setAddressProvider(ThriftServerAddressProvider addressProvider) {
this.addressProvider = addressProvider;
} public int getConTimeOut() {
return conTimeOut;
} public void setConTimeOut(int conTimeOut) {
this.conTimeOut = conTimeOut;
} public Integer getMaxActive() {
return maxActive;
} public Integer getIdleTime() {
return idleTime;
} public boolean isTestOnBorrow() {
return testOnBorrow;
} public void setTestOnBorrow(boolean testOnBorrow) {
this.testOnBorrow = testOnBorrow;
} public boolean isTestOnReturn() {
return testOnReturn;
} public void setTestOnReturn(boolean testOnReturn) {
this.testOnReturn = testOnReturn;
} public boolean isTestWhileIdle() {
return testWhileIdle;
} public void setTestWhileIdle(boolean testWhileIdle) {
this.testWhileIdle = testWhileIdle;
} }
spring配置文件
<!-- zookeeper -->
<bean id="thriftZookeeper" class="com.wy.thriftpool.commzkpool.zookeeper.ZookeeperFactory" destroy-method="close">
<property name="connectString" value="127.0.0.1:2181" />
<property name="namespace" value="thrift/thrift-service" />
</bean>
<bean id="userService" class="com.wy.thriftpool.commzkpool.client.ThriftServiceClientProxyFactory" destroy-method="close">
<property name="maxActive" value="5" />
<property name="idleTime" value="1800000" />
<property name="addressProvider">
<bean class="com.wy.thriftpool.commzkpool.support.impl.DynamicAddressProvider">
<property name="configPath" value="serviceAddress" />
<property name="zookeeper" ref="thriftZookeeper" />
</bean>
</property>
<property name="service" value="com.wy.thrift.service.UserService" />
</bean>
由于本人经验有限,文章中难免会有错误,请浏览文章的您指正或有不同的观点共同探讨!
由浅入深了解Thrift之客户端连接池化续的更多相关文章
- 由浅入深了解Thrift之客户端连接池化
一.问题描述 在上一篇<由浅入深了解Thrift之服务模型和序列化机制>文章中,我们已经了解了thrift的基本架构和网络服务模型的优缺点.如今的互联网圈中,RPC服务化的思想如火如荼.我 ...
- Redis客户端连接池
使用场景 对于一些大对象,或者初始化过程较长的可复用的对象,我们如果每次都new对象出来,那么意味着会耗费大量的时间. 我们可以将这些对象缓存起来,当接口调用完毕后,不是销毁对象,当下次使用的时候,直 ...
- thrift - C#(CSharp)客户端连接池(ConnectionPool)
调用示例: var tran = ThriftPool.Instance().BorrowInstance(); TProtocol protocol = new TBinaryProtoco ...
- Swoole2.0协程客户端连接池的实现
Swoole2.0官方默认的实例是短连接的,在请求处理完毕后就会切断redis或mysql的连接.实际项目可以使用连接池实现复用. 实现原理也很简单,使用SplQueue,在请求到来时判断资源队列中是 ...
- 高可用的池化 Thrift Client 实现(源码分享)
本文将分享一个高可用的池化 Thrift Client 及其源码实现,欢迎阅读源码(Github)并使用,同时欢迎提出宝贵的意见和建议,本人将持续完善. 本文的主要目标读者是对 Thrift 有一定了 ...
- 由浅入深了解Thrift之服务模型和序列化机制
一.Thrift介绍 Thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发.它结合了功能强大的软件堆栈和代码生成引擎.其允许你定义一个简单的定义文件中的数据类型和服务接口.以作为输入文件,编 ...
- 一个I/O线程可以并发处理N个客户端连接和读写操作 I/O复用模型 基于Buf操作NIO可以读取任意位置的数据 Channel中读取数据到Buffer中或将数据 Buffer 中写入到 Channel 事件驱动消息通知观察者模式
Tomcat那些事儿 https://mp.weixin.qq.com/s?__biz=MzI3MTEwODc5Ng==&mid=2650860016&idx=2&sn=549 ...
- redis客户端连接异常
本文参考:http://mdba.cn/2015/04/02/redistwemproxy-%e5%ae%a2%e6%88%b7%e7%ab%af%e8%bf%9e%e6%8e%a5%e5%bc%82 ...
- atitit.客户端连接oracle数据库的方式总结
客户端连接oracle数据库的方式总结 目录 Java程序连接一般使用jar驱动连接..... 桌面GUI一般采取c语言驱动oci.dll 直接连接... 间接连接(需要配置tns及其envi var ...
随机推荐
- Python超简单的HTTP服务器
Python超简单的HTTP服务器 安装了python就可以 python -m SimpleHTTPServer 执行这一个命令即可实现一个HTTP服务器,将当前目录设为HTTP服务目录,可以通过h ...
- LaTex中让页码从正文开始编号
在正文和目录之前这样设置即可 \setcounter{page}{}
- SQLserver中idendity的妙用
假设:现在有产品信息需要入库,要给每个产品按找预定的规则进行编号,编号规则如下: 产品编码:6位产品类型码+1位仓库码+2位年份+5位顺序码(要求从00001开始自增) 6位产品类型码:P00001 ...
- hdu 4857 逃生
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4857 逃生 Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能 ...
- golang的nil
golang中什么样的类型可以赋值nil? 类型文档中有注定"uninitialized value is nil"的类型都可以赋值nil. golang的基本类型不能赋值nil: ...
- TFS使用指南
上一篇文章已经简略介绍过TFS的安装与管理,本篇文章主要描述一下我个人在工作过程中使用TFS的一些指南与建议.本章内容预览: 1. 项目计划与跟踪 经常有很多朋友在日常聊天中抱怨做计划很无畏,因为计 ...
- 获得N位数字字母随机组合
import string import random def get_rand(n): allw = string.letters+string.digits r = [] for i in ran ...
- 重装win7系统的方法
1.当电脑可以运行的情况下重装系统: 用傻瓜似的重装系统就可以了,这个简单,不在累赘. 2.当电脑打不看的情况下: 2.1 用光盘安装系统,这个有好多教程,不在累赘. 2.2 用U盘安装方法: 2.2 ...
- Python实现CART(基尼指数)
Python实现CART(基尼指数) 运行环境 Pyhton3 treePlotter模块(画图所需,不画图可不必) matplotlib(如果使用上面的模块必须) 计算过程 st=>start ...
- 分布式架构--第一篇--项目拆分(maven命令生成多模块项目)
预览生成的项目结构: ying-yue-parent // 顶级总编译控制模块 ying-yue-lib // jar模块 ying-yue-model // 模型对象模块 ying-yue-dao ...