资源简介
对于Thrift服务化的改造,主要是客户端,可以从如下几个方面进行:
1.服务端的服务注册,客户端自动发现,无需手工修改配置,这里我们使用zookeeper,但由于zookeeper本身提供的客户端使用较为复杂,因此采用curator-recipes工具类进行处理服务的注册与发现。
2.客户端使用连接池对服务调用进行管理,提升性能,这里我们使用Apache Commons项目commons-pool,可以大大减少代码的复杂度。
3.关于Failover/LoadBalance,由于zookeeper的watcher,当服务端不可用是及时通知客户端,并移除不可用的服务节点,而LoadBalance有很多算法,这里我们采用随机加权方式,也是常有的负载算法,至于其他的算法介绍参考:常见的负载均衡的基本算法。
代码片段和文件信息
package cn.slimsmart.thrift.rpc;
import java.net.InetSocketAddress;
import org.apache.commons.pool.basePoolableobjectFactory;
import org.apache.thrift.TServiceClient;
import org.apache.thrift.TServiceClientFactory;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TframedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import cn.slimsmart.thrift.rpc.zookeeper.ThriftServerAddressProvider;
/**
* 连接池thrift-client for spring
*/
public class ThriftClientPoolFactory extends basePoolableobjectFactory {
private final ThriftServerAddressProvider serverAddressProvider;
private final TServiceClientFactory clientFactory;
private PoolOperationCallBack callback;
protected ThriftClientPoolFactory(ThriftServerAddressProvider addressProvider TServiceClientFactory clientFactory) throws Exception {
this.serverAddressProvider = addressProvider;
this.clientFactory = clientFactory;
}
protected ThriftClientPoolFactory(ThriftServerAddressProvider addressProvider TServiceClientFactory clientFactory
PoolOperationCallBack callback) throws Exception {
this.serverAddressProvider = addressProvider;
this.clientFactory = clientFactory;
this.callback = callback;
}
static interface PoolOperationCallBack {
// 销毁client之前执行
void destroy(TServiceClient client);
// 创建成功是执行
void make(TServiceClient client);
}
public void destroyobject(TServiceClient client) throws Exception {
if (callback != null) {
try {
callback.destroy(client);
} catch (Exception e) {
//
}
}
TTransport pin = client.getInputProtocol().getTransport();
pin.close();
}
public boolean validateobject(TServiceClient client) {
TTransport pin = client.getInputProtocol().getTransport();
return pin.isOpen();
}
@Override
public TServiceClient makeobject() throws Exception {
InetSocketAddress address = serverAddressProvider.selector();
TSocket tsocket = new TSocket(address.getHostName() address.getPort());
TTransport transport = new TframedTransport(tsocket);
TProtocol protocol = new TBinaryProtocol(transport);
TServiceClient client = this.clientFactory.getClient(protocol);
transport.open();
if (callback != null) {
try {
callback.make(client);
} catch (Exception e) {
//
}
}
return client;
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 2578 2015-03-07 21:36 src\main\java\cn\slimsmart\thrift\rpc\ThriftClientPoolFactory.java
文件 419 2015-03-07 16:41 src\main\java\cn\slimsmart\thrift\rpc\ThriftException.java
文件 3442 2015-03-07 23:34 src\main\java\cn\slimsmart\thrift\rpc\ThriftServiceClientProxyFactory.java
文件 4633 2015-03-08 00:23 src\main\java\cn\slimsmart\thrift\rpc\ThriftServiceServerFactory.java
文件 2338 2015-03-08 00:25 src\main\java\cn\slimsmart\thrift\rpc\zookeeper\FixedAddressProvider.java
文件 599 2015-03-07 22:45 src\main\java\cn\slimsmart\thrift\rpc\zookeeper\ThriftServerAddressProvider.java
文件 6033 2015-03-08 00:26 src\main\java\cn\slimsmart\thrift\rpc\zookeeper\ThriftServerAddressProviderZookeeper.java
文件 468 2015-03-07 17:05 src\main\java\cn\slimsmart\thrift\rpc\zookeeper\ThriftServerAddressRegister.java
文件 1811 2015-03-07 23:07 src\main\java\cn\slimsmart\thrift\rpc\zookeeper\ThriftServerAddressRegisterZookeeper.java
文件 1662 2015-03-08 00:41 src\main\java\cn\slimsmart\thrift\rpc\zookeeper\ThriftServerIpLocalNetworkResolve.java
文件 474 2015-03-07 16:49 src\main\java\cn\slimsmart\thrift\rpc\zookeeper\ThriftServerIpResolve.java
文件 2536 2015-03-07 23:09 src\main\java\cn\slimsmart\thrift\rpc\zookeeper\ZookeeperFactory.java
文件 100 2015-03-07 22:19 src\main\resources\EchoSerivce.thrift
文件 1867 2015-03-08 00:07 src\test\java\cn\slimsmart\thrift\rpc\demo\Client.java
文件 33241 2015-03-07 22:20 src\test\java\cn\slimsmart\thrift\rpc\demo\EchoSerivce.java
文件 255 2015-03-07 22:29 src\test\java\cn\slimsmart\thrift\rpc\demo\EchoSerivceImpl.java
文件 392 2015-03-07 23:25 src\test\java\cn\slimsmart\thrift\rpc\demo\Server.java
文件 337 2015-03-01 21:18 src\test\resources\log4j.properties
文件 2558 2015-03-08 00:20 src\test\resources\spring-context-thrift-client.xm
文件 2738 2015-03-08 00:23 src\test\resources\spring-context-thrift-server.xm
文件 1419 2015-03-07 16:23 pom.xm
文件 132 2015-03-07 22:19 thrift.bat
文件 4889088 2015-03-07 13:14 thrift-0.9.2.exe
目录 0 2015-03-07 21:29 src\main\java\cn\slimsmart\thrift\rpc\zookeeper
目录 0 2015-03-07 22:51 src\test\java\cn\slimsmart\thrift\rpc\demo
目录 0 2015-03-07 22:19 src\main\java\cn\slimsmart\thrift\rpc
目录 0 2015-03-07 21:39 src\test\java\cn\slimsmart\thrift\rpc
目录 0 2015-03-07 15:44 src\main\java\cn\slimsmart\thrift
目录 0 2015-03-07 21:39 src\test\java\cn\slimsmart\thrift
目录 0 2015-03-07 15:44 src\main\java\cn\slimsmart
............此处省略13个文件信息
评论
共有 条评论