Redis Lettuce客户端异步连接池详解
前言
异步/非阻塞编程模型需要非阻塞API才能获得Redis连接。阻塞的连接池很容易导致阻塞事件循环并阻止您的应用程序进行处理的状态。Lettuce带有异步,非阻塞池实现,可与Lettuces异步连接方法一起使用。它不需要其他依赖项。Lettuce提供异步连接池支持。它需要一个Supplier
用于异步连接到任何受支持类型的连接。AsyncConnectionPoolSupport
将创建BoundedAsyncPool
。池可以分配包装的或直接的连接。
Jar引入
// redisimplementation 'io.lettuce:lettuce-core:5.2.2.RELEASE'implementation 'org.apache.commons:commons-pool2:2.8.0'
基本使用
RedisClient client = RedisClient.create();// 创建异步连接池
AsyncPool<StatefulRedisConnection<String, String>> pool = AsyncConnectionPoolSupport.createBoundedObjectPool(() -> client.connectAsync(StringCodec.UTF8, RedisURI.create(host, port)),
// 使用默认的连接池配置
BoundedPoolConfig.create());// 从连接池中获取连接
CompletableFuture<StatefulRedisConnection<String, String>> con = pool.acquire();// 异步执行setex命令并返回结果
CompletionStage<String> result = con.thenCompose(connection -> connection.async().setex("test", 30, "test")// 释放连接池获取的连接.whenComplete((s, throwable) -> pool.release(connection)));// 关闭连接池
pool.closeAsync();// 关闭client
client.shutdownAsync();
cluster使用
创建redis的Host和port数据类
public class RedisNode {private String node;private int port;// 下面省略set,get和构造函数
}
初始化client和连接池
public class redisClusterAsyncPool {// ClientResources should only be created once per appprivate val clientResources = DefaultClientResources.create()private void initclusterPool {// 初始化redis cluster RedisURIList<RedisURI> nodes = new ArrayList<>();redisNodes.forEach(node -> {nodes.add(RedisURI.builder().withHost(node.getHost()).withPort(node.getPort()).withTimeout(Duration.ofMillis(timeOut)).withPassword(password).build());});// 初始化redis cluster clientRedisClusterClient clusterClient = RedisClusterClient.create(clientResources, nodes)// 设置failover(集群故障转移)时的集群topology刷新机制ClusterTopologyRefreshOptions topologyRefreshOptions = ClusterTopologyRefreshOptions.builder().enableAdaptiveRefreshTrigger(// Refresh cluster topology when Redis responds with a MOVED redirectionClusterTopologyRefreshOptions.RefreshTrigger.MOVED_REDIRECT,// Refresh cluster topology when Redis Connections to a particular host run into// persistent reconnects (more than one attempt).ClusterTopologyRefreshOptions.RefreshTrigger.PERSISTENT_RECONNECTS)// Set the timeout for adaptive topology updates..adaptiveRefreshTriggersTimeout(Duration.ofMinutes(3)).build();// Set the ClusterClientOptions for the client.clusterClient.setOptions(// Sets the ClusterTopologyRefreshOptions for detailed control of topology updates.ClusterClientOptions.builder().topologyRefreshOptions(topologyRefreshOptions).autoReconnect(true) // set auto reconnection: true.pingBeforeActivateConnection(true).build())// 初始化cluster partitions,如果不初始化,在从连接吃获取连接的时会抛异常clusterClient.partitions// 初始化连接池配置BoundedAsyncPool<StatefulRedisClusterConnection<String, String>> pool = AsyncConnectionPoolSupport.createBoundedObjectPool({ redisCluster?.connectAsync(StringCodec.UTF8) },BoundedPoolConfig.builder().maxIdle(poolConfig.maxIdle).maxTotal(poolConfig.maxTotal).minIdle(poolConfig.minIdle).build())// 从连接池中获取连接CompletableFuture<StatefulRedisClusterConnection<String, String>> con = pool.acquire();// 使用方法和基本使用一样// ...}
}
Redis Lettuce客户端异步连接池详解相关推荐
- python requests 异步调用_构建高效的python requests长连接池详解
前文: 最近在搞全网的CDN刷新系统,在性能调优时遇到了requests长连接的一个问题,以前关注过长连接太多造成浪费的问题,但因为系统都是分布式扩展的,针对这种各别问题就懒得改动了. 现在开发的缓存 ...
- Spring Boot 使用 Druid 连接池详解
Spring Boot 使用 Druid 连接池详解 Alibaba Druid 是一个 JDBC 组件库,包含数据库连接池.SQL Parser 等组件,被大量业务和技术产品使用或集成,经历过严苛的 ...
- Mysql连接池详解——原理部分
引言 为什么要使用连接池 线程池如何配合连接池使用, 连接池和线程池数量 不能根据经验值直接设置,需要根据io同步的具体时间去测试得到最优的值 同步连接池和异步连接池的区别 连接池的扩展 一.池化技术 ...
- jedis连接池详解(Redis)
转自:http://tianxingzhe.blog.51cto.com/3390077/1684306 原子性(atomicity): 一个事务是一个不可分割的最小工作单位,事务中包括的诸操作要么都 ...
- java连接池详解与自定义es连接池
目录 1 版本选择 2 依赖选择 3 使用commons-pool构造连接池 3.1 pom.xml 3.2 对象池类 GenericObjectPool普通对象池 GenericKeyedObjec ...
- Hyperf连接池详解
开头语 Hyperf官网介绍了为什么要使用连接池及连接池的简单案例,但是对新手却不太友好,不过我还是想基于官方文档,来做一篇更深的讲解. 先来看看官方文档对于连接池的介绍 为什么要使用连接池 当并发量 ...
- 11.定时任务定时线程池详解
3.1 新增定时任务池 11.定时任务&定时线程池详解 当我们不用任务框架时,我们想自己写一个定时任务时,我们能想起那个工具类呢?Timer ?还有吗?不知道了,下面我们要讲下Schedu ...
- 干货 | Tomcat 连接数与线程池详解
转载自 干货 | Tomcat 连接数与线程池详解 前言 在使用tomcat时,经常会遇到连接数.线程数之类的配置问题,要真正理解这些概念,必须先了解Tomcat的连接器(Connector). 在 ...
- redis cluster管理工具redis-trib.rb详解
redis cluster管理工具redis-trib.rb详解 redis-trib.rb是redis官方推出的管理redis集群的工具,集成在redis的源码src目录下,是基于redis提供的集 ...
最新文章
- ORACLE数据库系统结构
- ORA-03135 ,ORA-02050到底什么原因?
- Elasticlunr.js 简单介绍
- 复用io selectors模块
- 网站301重定向 解决方法
- select、poll、epoll 比较
- 斯特林数-斯特林反演
- JEECMS站群管理系统-- 标签的配置流程
- 如何使用jquery ,浏览器窗口滚动到一定距离,显示div中的内容
- 这才是晕了哟,dos之家上全是graybird???????????
- 程序员“好为人师”有错吗?
- python人名统计_「姓名分析」Python|美国婴儿姓名分析 - seo实验室
- vue项目,解决ie缓存问题
- 21届本科大数据菜鸡:我是怎么在互联网寒冬拿到腾讯、华为、京东、美团、快手等大厂offer的?
- 手把手教你选购电脑组件
- 刷题、面试必备网站(java)
- 保姆级-天翼网关TEWA-700G、TEWA-1000E/G等系列光猫获取超级密码
- 中国App增长联盟,和优秀的创始人玩着办大事!
- STM32 JTAG SWD (PB3 PB4用作普通IO)及启动模式
- java字符串与数值型相互转换