Share Netty NioEventLoopGroup between Lettuce & AsyncHttpClient

723 views Asked by At

My application is using Netty 4.1.6.Final and Lettuce 4.3.0, Async Http Client 2.1.0

Lettuce 4.3.0 also relies on netty 4.1.6.Final. Async Http Client 4.3.0 also relies on netty 4.1.4.Final.

Now I see Lettuce creates its thread pool. Async Http Client also creates its thread pool. And my application creates another thread pool for Netty.

Is it possible to share the same NioEventLoopGroup cross all the component to reduce thread number?

1

There are 1 answers

0
Mr.Wang from Next Door On BEST ANSWER

Async Http Client

AsyncHttpClient httpClient = new DefaultAsyncHttpClient(new DefaultAsyncHttpClientConfig.Builder().setEventLoopGroup(myEventGroup).build())

Lettuce

Add RedisEventLoopGroupProvider class

public class RedisEventLoopGroupProvider extends DefaultEventLoopGroupProvider {


    public RedisEventLoopGroupProvider() {
        super(3);
        // TODO Auto-generated constructor stub
    }

    @SuppressWarnings("unchecked")
    @Override
    public <T extends EventLoopGroup> T allocate(Class<T> type) {
        if (NioEventLoopGroup.class.equals(type)) {
            return (T)myEventGroup;
        }
        return super.allocate((Class<T>)type);
    }

}

Initialize in this way

RedisClient redisClient = RedisClient.create( DefaultClientResources.builder()
            .eventLoopGroupProvider(new RedisEventLoopGroupProvider())
            .ioThreadPoolSize(1)
            .computationThreadPoolSize(3)
            .build(), Environment.REDIS_CONNECTION);