A Thrift Client pool for Java
- raw and TypeSafe TServiceClient pool
- Multi Backend Servers support
- Backend Servers replace on the fly
- Backend route by hash or any other algorithm
- java.io.Closeable resources (for try with resources)
- Ease of use
- jdk 1.8 only (1.7 is not okay without code modification)
Add to your pom.xml
<dependency>
<groupId>com.wealoha</groupId>
<artifactId>thrift-client-pool-java</artifactId>
<version>1.0</version>
</dependency>
// get a pool
PoolConfig config = new PoolConfig();
config.setFailover(true); // optional
config.setTimeout(1000); // optional
// PoolConfig is a instance of GenericObjectPoolConfig
config.setMinIdle(3);
config.setMaxTotal(30);
ThriftClientPool<TestThriftService.Client> pool = new ThriftClientPool<>(
serverList,
e -> new YourThriftService.Client(new TFramedTransport(new TBinaryProtocol(e))), // ❶
config);
// or pre jdk1.8
ThriftClientPool<TestThriftService.Client> pool = new ThriftClientPool<>(serverList,
new ThriftClientFactory() { // ❶
@Override
public TServiceClient createClient(TTransport transport) {
return new YourThriftService.Client(new TFramedTransport(new TBinaryProtocol(transport)));
}
}, config);
// just call thrift
try {
Iface iFace = pool.iface(); // ❷
String response = iFace.echo("Hello!"); // ❸
logger.info("get response: {}", response);
} catch (Throwable e) {
logger.error("call echo fail", e);
}
// or call like this
try (ThriftClient<Client> thriftClient = pool.getClient()) { // ❹
// get Iface generated by Thrift
Iface iFace = thriftClient.iFace(); // ❺
String response = iFace.echo("Hello!");
logger.info("get response: {}", response);
response = iFace.echo("Hello again!");
logger.info("get response: {}", response);
// finish must be called at last
thriftClient.finish(); // ❻
} catch (TException e) {
logger.error("call echo fail", e);
}
// shard support
List<ServiceInfo> serviceList = Arrays.asList( //
new ServiceInfo("127.0.0.1", 9090), //
new ServiceInfo("127.0.0.1", 9091), //
new ServiceInfo("127.0.0.1", 9092), //
new ServiceInfo("127.0.0.1", 9093), //
new ServiceInfo("127.0.0.1", 9094));
ShardedThriftClientPool<Integer, Client> shardedPool = new ShardedThriftClientPool<>(
serviceList, //
key -> key, // hash function
servers -> { // shard function
return Arrays.asList( //
Arrays.asList(servers.get(0), servers.get(1)), //
Arrays.asList(servers.get(2), servers.get(3)), //
Arrays.asList(servers.get(4)));
}, //
servers -> new ThriftClientPool<>(servers, Client::new, config));
Integer key = 10;
ThriftClientPool<Client> pool = shardedPool.getShardedPool(key);
// Use pool as previous examples.
- ❶ return your service Client(an IFace impl and TServiceClient subclass) generated by Thrift
- ❷ obtain it from pool
- ❸ call your service
- ❹ another way is getting a wrapped client using try with resources
- ❺ get Iface from wrapped client
- ❻ when using getClient(), finish must be called if non Exception throw in interacting
Only one interface need to impl with few lines, return one YourThriftService.Client in createClient(TTransport) generated by thrift tools. Pool will handle all the rest.
- void setServices(List);
Dynamically change backend services, all new client get from getClient() will using new services.
If you encourage this exception, please check that Iface match Pool's Client type.
java.lang.ClassCastException: com.sun.proxy.$Proxy3 cannot be cast to xx.xx.Iface