如何配置Lettuce Redis集群异步连接池

2024-05-26

我正在配置我的生菜重新分配池。当我按照官方文档配置时,连接池无法正常初始化,无法获取连接。官方文档指出:

RedisClusterClient clusterClient = 
RedisClusterClient.create(RedisURI.create(host, port));

AsyncPool<StatefulRedisConnection<String, String>> pool =             AsyncConnectionPoolSupport.createBoundedObjectPool(        () -> clusterClient.connectAsync(StringCodec.UTF8),     BoundedPoolConfig.create());
// execute work
CompletableFuture<String> setResult = pool.acquire().thenCompose(connection -> {        
    RedisAsyncCommands<String, String> async = connection.async();    
    async.set("key", "value");
    return async.async.set("key2", "value2").whenComplete((s, throwable) -> pool.release(c));
});

// terminating
pool.closeAsync();

// after pool completion
client.shutdownAsync();

这个配置在我的环境中不起作用。然后我添加 minIdle 配置:

final BoundedPoolConfig.Builder builder = BoundedPoolConfig.builder();
builder.minIdle(9);

它一开始是有效的,但是当我循环连接池并多次发送命令时,会抛出以下异常:

java.util.concurrent.ExecutionException: java.lang.IllegalStateException:             AsyncPool is closed   at     java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:357) at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1895)

这是我的所有代码:

private String passwd = "xxxxx";
private String ip = "10.0.0.204";
;

@Bean
@Scope("singleton")
public ClientResources clientResources() {
    final DefaultClientResources defaultClientResources = DefaultClientResources.builder()
                                                                                .ioThreadPoolSize(4)
                                                                                .computationThreadPoolSize(4)
                                                                                .build();

    return defaultClientResources;
}

@Bean(destroyMethod = "shutdown")
@Scope("singleton")
public RedisClusterClient clusterClient(ClientResources clientResources) {
    final String ip = "10.0.0.204";
    final String passwd = "dingXiang123";
    final RedisURI redisURI1 = RedisURI.Builder.redis(ip, 7001).withPassword(passwd).build();
    final RedisURI redisURI2 = RedisURI.Builder.redis(ip, 7002).withPassword(passwd).build();
    final RedisURI redisURI3 = RedisURI.Builder.redis(ip, 7003).withPassword(passwd).build();
    final RedisURI redisURI4 = RedisURI.Builder.redis(ip, 7004).withPassword(passwd).build();
    final RedisURI redisURI5 = RedisURI.Builder.redis(ip, 7005).withPassword(passwd).build();
    final RedisURI redisURI6 = RedisURI.Builder.redis(ip, 7006).withPassword(passwd).build();
    RedisClusterClient clusterClient = null;
    try {
        final List<RedisURI> redisURIS = Arrays.asList(redisURI1, redisURI2, redisURI3, redisURI4, redisURI5, redisURI6);
        clusterClient = RedisClusterClient.create(clientResources, redisURIS);
        ClusterTopologyRefreshOptions topologyRefreshOptions = ClusterTopologyRefreshOptions.builder()
                                                                                            .enableAdaptiveRefreshTrigger(ClusterTopologyRefreshOptions.RefreshTrigger.MOVED_REDIRECT, ClusterTopologyRefreshOptions.RefreshTrigger.PERSISTENT_RECONNECTS)
                                                                                            //连接池refresh超时时间
                                                                                            .adaptiveRefreshTriggersTimeout(Duration.ofMinutes(3))
                                                                                            .build();


        clusterClient.setOptions(ClusterClientOptions.builder()
                                                     .topologyRefreshOptions(topologyRefreshOptions)
                                                     .autoReconnect(true)
                                                     .pingBeforeActivateConnection(true)
                                                     .build());

        final RedisAdvancedClusterAsyncCommands<String, String> async = clusterClient.connect().async();
        final RedisFuture<String> set = async.set("aa", "aaaaa");
        set.get();
        log.info("客户端初始化成功");
        return clusterClient;
    } catch (Exception e) {
        log.error("lettce客户端初始化失败,{}", e);
        if (clusterClient != null) {
            clusterClient.shutdown();
        }
    }

    return null;
}

/**
 * 初始化异步的 Cluter 模式链接池
 *
 * @param clusterClient
 * @return
 */
@Bean()
@DependsOn("clusterClient")
@Scope("singleton")
public BoundedAsyncPool<StatefulRedisClusterConnection<String, String>> lettucePool(RedisClusterClient clusterClient) {
    final BoundedPoolConfig.Builder builder = BoundedPoolConfig.builder();
    builder.minIdle(9);
    final BoundedPoolConfig boundedPoolConfig = builder.build();
    final BoundedAsyncPool<StatefulRedisClusterConnection<String, String>> lettucePool = AsyncConnectionPoolSupport.createBoundedObjectPool(
            () -> clusterClient.connectAsync(StringCodec.UTF8)
            , boundedPoolConfig
    );


    log.info("连接池初始化成功");
    return lettucePool;
}

/**
 * 从连接池获取链接
 *
 * @param lettucePool
 */
@Bean
@DependsOn("lettucePool")
public CompletableFuture<StatefulRedisClusterConnection<String, String>> clusterAsync(BoundedAsyncPool<StatefulRedisClusterConnection<String, String>> lettucePool) {

    final CompletableFuture<StatefulRedisClusterConnection<String, String>> acquire = lettucePool.acquire();
    return acquire;
}
  1. 你又遇到这个问题了吗,你是怎么解决的?

  2. 另外一点是,我不太喜欢redisTemplate来操作Lettuce API,所以我正在寻找原生Lettuce集群池的配置解决方案。

  3. 你之前做过原集群池的配置或者Api的使用吗,或者看过详细的Demo文档吗,如果有请推荐给我(当然我也看了官方文档,我可能需要一个Demo申请学习)


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何配置Lettuce Redis集群异步连接池 的相关文章

随机推荐

  • Windows 7 下 VB 6 中的“用户定义类型未定义”错误

    我使用的是 Windows 7 我的项目是 VB 6 0 我在执行程序时遇到错误 它显示错误 未定义用户定义类型 这是我的代码 Private Sub Toolbar1 ButtonClick ByVal Button As MSComct
  • 在 GNU C++ 编译器中运行具有多个源文件的程序

    我在 Windows 7 操作系统上使用 DEV GNU c 编译器 我需要知道如何编译具有多个源文件的程序 这是例子 FILE1 void f1 printf this is another file under same program
  • 在 Windows 7 上安装 Python Fabric 时出现问题

    我正在尝试使用以下指南在 Windows 7 上安装 Python Fabric在 Windows 上安装 Python 和 Fabric http www jonnyreeves co uk 2011 08 getting python
  • 自动完成功能在特定层次结构的 XML 文件中不起作用

    特别是 XML 节点层次结构 例如 DrawerLayout gt RelativeLayout gt ImageButton 自动完成功能无法按预期工作 建议列表包含无效项目 例如 android src里面没有显示ImageButton
  • 为什么 event.stopPropagation() 不会阻止

    SO 上也提出了与此类似的问题 但他们要么只关心解决其具体实现 要么没有明确提出这个问题 此外 所有答案都没有真正解决这个问题 给出以下示例 document querySelector span addEventListener clic
  • 最低共同祖先算法

    所以我一直在研究实现最低共同祖先算法 我研究了许多不同的算法 主要是 Trajan 解决方案的变体或 RMQ 的变体 我正在使用非二叉树 我的树经常会在查询之间发生变化 因此预处理不一定值得 树的节点数不应超过 50 75 个 我想知道的是
  • MAMP Pro mysql 无法启动

    我遇到问题无法找到解决此问题的方法 我收到这个错误 2017 01 11 23 58 25 7fffbac563c0 InnoDB Operating system error number 2 in a file operation In
  • 如何通过双击图标来执行JAVA程序?

    我写了一个java程序 现在我想在没有 IDE Eclipse 等的情况下打开我的控制台 java 应用程序 只需双击桌面上的可执行版本即可 我已将 java 项目导出为 Runnable JAR 文件 但无法打开 当我尝试使用cmd打开应
  • 适用于真正复杂查询的 ORM 解决方案

    在这样复杂的情况下 人们可以 应该使用任何 ORM 解决方案吗 这可以用以下方法完成吗Propel or Doctrine 目前我正在使用 Propel 所以如果有 Propel 解决方案 我会很高兴 如果我使用 propel 进行直接查询
  • REST API 响应中的校验和

    发送带有响应内容的校验和是个好主意吗 如果是这样 计算校验和的最常见方法是什么 Example HTTP 1 1 200 OK Date Thu 30 Jun 2011 21 32 20 GMT Server Apache Connecti
  • 苹果的属性列表(plist)在C++中的实现

    我的任务是在 C 应用程序中读取 Apple 的属性列表文件 主要关注 OS X 中指定的 xml 类型 plist 文件 它模仿 xml 类型实现 Apple 对其属性列表的实现描述如下 http developer apple com
  • 计算热图颜色

    我正在制作一个由 HTML 表格组成的热图 该表包含n细胞并有一个lowest值和一个highest值 最高值始终高于最低值 每个细胞都有一个cell价值 所有这些值都是整数 具有最低值的单元格应为浅蓝色 缩放到具有最高值的单元格为深红色
  • Rails (PostgreSQL) 中文本列的默认大小

    如果我在迁移中有这个 t text body 我可以容纳多少文字 body 如果相关的话我正在使用 PostgreSQL 直接来自PostgreSQL 文档 http www postgresql org docs 8 4 static d
  • 自定义错误处理程序抛出错误:无法读取未定义的属性“get”(注入器)

    我正在 Angular 4 中构建自定义错误处理程序 以使用错误拦截器处理不同类型的应用程序错误 创建一个基类 应用程序错误 ts 和其他类 例如处理 403 错误创建类拒绝访问 ts 扩展了这个基类 在基类中注入了一个服务toastrSe
  • 用逗号分割字符串到新行

    我有一个像这样的字符串 This is great day tomorrow is a better day the day after is a better day the day after the day after that is
  • 将误差线添加到多条线上以在 R 中的绘图上显示标准差

    我有一个包含许多不同线条的图 我想为每条线上的每个点添加误差线 df lt matrix runif 25 5 5 plot 1 5 seq 0 1 1 4 type n mapply lines as data frame df col
  • .onLoad 在渲染完成之前调用吗?

    我想在页面加载后调用一些 JS 这可能会涉及延迟 因此我希望首先加载页面 以便显示内容 但似乎调用了 onLoad 处理程序中的代码before渲染完成 是否有更好的事件可以使用 该事件在页面 完成 时触发 澄清一下 我想在页面呈现在屏幕上
  • 使用 stl sort 对表进行排序

    我有一个巨大的表 约 50Gb 格式为 i j k 来自稀疏矩阵 存储为 uint32 t idx1 idx2 float vals uint32 t tablesize 我想使用给定的比较函数 即 idx1 和 idx2 的函数 对其进行
  • 卷积神经网络 (CNN) 输入形状

    我是 CNN 的新手 我有一个关于 CNN 的问题 我对 CNN 特别是 Keras 的输入形状有点困惑 我的数据是不同时隙的二维数据 比方说10X10 因此 我有 3D 数据 我将把这些数据输入到我的模型中来预测即将到来的时间段 所以 我
  • 如何配置Lettuce Redis集群异步连接池

    我正在配置我的生菜重新分配池 当我按照官方文档配置时 连接池无法正常初始化 无法获取连接 官方文档指出 RedisClusterClient clusterClient RedisClusterClient create RedisURI