Code Monkey home page Code Monkey logo

thriftj's Introduction

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

thriftj's People

Contributors

cyfonly avatar

Watchers

James Cloos avatar sky-zy avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.