Git Product home page Git Product logo

thrift-client-pool-java's Introduction

thrift-client-pool-java

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)

Usage

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

ThriftClientFactory

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.

ThriftClientPool

  • void setServices(List);

Dynamically change backend services, all new client get from getClient() will using new services.

Know issues

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

See also

A better pure connection pool with shard

thrift-client-pool-java's People

Contributors

perlmonk avatar xhwskhizein avatar

Watchers

 avatar  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.