ThriftJ

A failover and load balance Thrift client with pooled connections

License

License

GroupId

GroupId

com.github.cyfonly
ArtifactId

ArtifactId

thriftj
Last Version

Last Version

1.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

ThriftJ
A failover and load balance Thrift client with pooled connections
Project URL

Project URL

https://github.com/cyfonly/ThriftJ
Source Code Management

Source Code Management

https://github.com/cyfonly/ThriftJ

Download thriftj

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.cyfonly/thriftj/ -->
<dependency>
    <groupId>com.github.cyfonly</groupId>
    <artifactId>thriftj</artifactId>
    <version>1.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.cyfonly/thriftj/
implementation 'com.github.cyfonly:thriftj:1.0.1'
// https://jarcasting.com/artifacts/com.github.cyfonly/thriftj/
implementation ("com.github.cyfonly:thriftj:1.0.1")
'com.github.cyfonly:thriftj:jar:1.0.1'
<dependency org="com.github.cyfonly" name="thriftj" rev="1.0.1">
  <artifact name="thriftj" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.cyfonly', module='thriftj', version='1.0.1')
)
libraryDependencies += "com.github.cyfonly" % "thriftj" % "1.0.1"
[com.github.cyfonly/thriftj "1.0.1"]

Dependencies

compile (5)

Group / Artifact Type Version
org.apache.thrift : libthrift jar 0.6.1
com.google.guava : guava jar 20.0
org.apache.commons : commons-pool2 jar 2.4.2
org.javassist : javassist jar 3.18.0-GA
org.slf4j : slf4j-api jar 1.7.22

test (1)

Group / Artifact Type Version
org.slf4j : slf4j-log4j12 jar 1.7.22

Project Modules

There are no modules declared in this project.

design
License Built with Maven
负载均衡和高可用的池化 Thrift Client

Features

  1. 链式调用API,简洁直观
  2. 完善的默认配置,无需担心调用时配置不全导致抛错
  3. 池化连接对象,高效管理连接的生命周期
  4. 异常服务自动隔离与恢复
  5. 多种可配置的负载均衡策略,支持随机、轮询、权重和哈希
  6. 多种可配置的服务级别,并自动根据服务级别进行服务降级

Architectural Design

design

Usage

1. Maven

<dependency>
    <groupId>com.github.cyfonly</groupId>
    <artifactId>thriftj</artifactId>
    <version>1.0.1</version>
</dependency>
<dependency>
    log4j or logback, and so on...
</dependency>

2. 调用

除 servers 必须配置外,其他配置均为可选(使用默认配置)

//Thrift server 列表
private static final String servers = "127.0.0.1:10001,127.0.0.1:10002";

//TTransport 验证器
ConnectionValidator validator = new ConnectionValidator() {
    @Override
    public boolean isValid(TTransport object) {
        return object.isOpen();
    }
};

//连接对象池配置
GenericKeyedObjectPoolConfig poolConfig = new GenericKeyedObjectPoolConfig();

//failover 策略
FailoverStrategy failoverStrategy = new FailoverStrategy();

//构造 ThriftClient 对象并配置
final ThriftClient thriftClient = new ThriftClient();
thriftClient.servers(servers)
            .loadBalance(Constant.LoadBalance.RANDOM)
            .connectionValidator(validator)
            .poolConfig(poolConfig)
            .failoverStrategy(failoverStrategy)
            .connTimeout(5)
            .backupServers("")
            .serviceLevel(Constant.ServiceLevel.NOT_EMPTY)
            .start();
            
//打印从 ThriftClient 获取到的可用服务列表
List<ThriftServer> servers = thriftClient.getAvailableServers();
for(ThriftServer server : servers){
    System.out.println(server.getHost() + ":" + server.getPort());
}

//服务调用
if(servers.size()>0){
    try{
		    TestThriftJ.Client client = thriftClient.iface(TestThriftJ.Client.class);
		    QryResult result = client.qryTest(1);
		    System.out.println("result[code=" + result.code + " msg=" + result.msg + "]");
	  }catch(Throwable t){
		    logger.error("-------------exception happen", t);
	  }
}

Demo

see https://github.com/cyfonly/ThriftJ/tree/master/src/test

License

基于 Apache License 2.0 发布。有关详细信息,请参阅 LICENSE

Versions

Version
1.0.1
1.0.0