【问题标题】:org.springframework.data.redis.RedisConnectionFailureException: Could not get a resource from the poolorg.springframework.data.redis.RedisConnectionFailureException:无法从池中获取资源
【发布时间】:2021-01-11 04:43:31
【问题描述】:

我的 cassandra docker-compose 文件:

version: '2'
services:
  redis-node-0:
    image: docker.io/bitnami/redis-cluster:latest
    ports:
      - "6370:6379"
    environment:
      - 'REDIS_PASSWORD=pass'
      - 'REDIS_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5'

  redis-node-1:
    image: docker.io/bitnami/redis-cluster:latest
    ports:
      - "6371:6379"
    environment:
      - 'REDIS_PASSWORD=pass'
      - 'REDIS_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5'

  redis-node-2:
    image: docker.io/bitnami/redis-cluster:latest
    ports:
      - "6372:6379"
    environment:
      - 'REDIS_PASSWORD=pass'
      - 'REDIS_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5'

  redis-node-3:
    image: docker.io/bitnami/redis-cluster:latest
    ports:
      - "6373:6379"
    environment:
      - 'REDIS_PASSWORD=pass'
      - 'REDIS_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5'

  redis-node-4:
    image: docker.io/bitnami/redis-cluster:latest
    ports:
      - "6374:6379"
    environment:
      - 'REDIS_PASSWORD=pass'
      - 'REDIS_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5'

  redis-node-5:
    image: docker.io/bitnami/redis-cluster:latest
    ports:
      - "6375:6379"
    environment:
      - 'REDIS_PASSWORD=pass'
      - 'REDIS_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5'

  redis-cluster-init:
    image: docker.io/bitnami/redis-cluster:latest
    depends_on:
      - redis-node-0
      - redis-node-1
      - redis-node-2
      - redis-node-3
      - redis-node-4
      - redis-node-5
    environment:
      - 'REDISCLI_AUTH=pass'
      - 'REDIS_CLUSTER_REPLICAS=1'
      - 'REDIS_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5'
      - 'REDIS_CLUSTER_CREATOR=yes'

我有以下 java 配置:

String clusterNodes = "localhost:6370,localhost:6371,localhost:6372,localhost:6373,localhost:6374,localhost:6375";
            List<RedisNode> redisNodes = Arrays.stream(clusterNodes
                    .split(","))
                    .map(s -> {
                        String[] singleNode = s.split(":");
                        return new RedisNode(singleNode[0], Integer.parseInt(singleNode[1]));
                    }).collect(Collectors.toList());

            RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
            redisClusterConfiguration.setPassword("pass");
            redisClusterConfiguration.setClusterNodes(redisNodes);
            JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisClusterConfiguration);
            jedisConnectionFactory.afterPropertiesSet();
            beanFactory.registerSingleton("jedisConnectionFactory", jedisConnectionFactory);

当我运行应用程序时 - 我看到以下错误:

    [java] 23:35:30.094 WARN [main] o.e.jetty.webapp.WebAppContext (WebAppContext.java:554): Failed startup of context o.e.j.w.WebAppContext@441016d6{/,[file:///D:/work/onsolve/mir/acadia/ant-build/****/, jar:file:///***/ant-build
/****/WEB-INF/lib/from-ivy-swagger-ui-3.23.0.jar!/META-INF/resources],UNAVAILABLE} org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.mir3.sawtooth.service.redis.RedisConnector#0': Invocation of init method failed
; nested exception is org.springframework.data.redis.RedisConnectionFailureException: Could not get a resource from the pool; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
     [java]     at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:160)
     [java]     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:416)
     [java]     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1788)
     [java]     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595)
     [java]     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
     [java]     at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
     [java]     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
     [java]     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
     [java]     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
     [java]     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879)
     [java]     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
     [java]     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
     [java]     at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:401)
     [java]     at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:292)
     [java]     at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:103)
     [java]     at org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized(ContextHandler.java:890)
     [java]     at org.eclipse.jetty.servlet.ServletContextHandler.callContextInitialized(ServletContextHandler.java:558)
     [java]     at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:853)
     [java]     at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:370)
     [java]     at org.eclipse.jetty.webapp.WebAppContext.startWebapp(WebAppContext.java:1497)
     [java]     at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1459)
     [java]     at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:785)
     [java]     at org.eclipse.jetty.servlet.ServletContextHandler.doStart(ServletContextHandler.java:287)
     [java]     at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:545)
     [java]     at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
     [java]     at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:138)
     [java]     at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:117)
     [java]     at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:113)
     [java]     at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
     [java]     at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:138)
     [java]     at org.eclipse.jetty.server.Server.start(Server.java:419)
     [java]     at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:108)
     [java]     at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:113)
     [java]     at org.eclipse.jetty.server.Server.doStart(Server.java:386)
     [java]     at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
     [java]     at com.mir3.cure.framework.WebAppRunner.startServer(WebAppRunner.java:127)
     [java]     at com.mir3.acadia.Main.main(Main.java:63)
     [java] Caused by: org.springframework.data.redis.RedisConnectionFailureException: Could not get a resource from the pool; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
     [java]     at org.springframework.data.redis.connection.jedis.JedisExceptionConverter.convert(JedisExceptionConverter.java:67)
     [java]     at org.springframework.data.redis.connection.jedis.JedisExceptionConverter.convert(JedisExceptionConverter.java:41)
     [java]     at org.springframework.data.redis.PassThroughExceptionTranslationStrategy.translate(PassThroughExceptionTranslationStrategy.java:44)
     [java]     at org.springframework.data.redis.FallbackExceptionTranslationStrategy.translate(FallbackExceptionTranslationStrategy.java:42)
     [java]     at org.springframework.data.redis.connection.jedis.JedisClusterConnection.convertJedisAccessException(JedisClusterConnection.java:760)
     [java]     at org.springframework.data.redis.connection.jedis.JedisClusterStringCommands.convertJedisAccessException(JedisClusterStringCommands.java:556)
     [java]     at org.springframework.data.redis.connection.jedis.JedisClusterStringCommands.set(JedisClusterStringCommands.java:120)
     [java]     at org.springframework.data.redis.connection.DefaultedRedisConnection.set(DefaultedRedisConnection.java:274)
     [java]     at org.springframework.data.redis.connection.DefaultStringRedisConnection.set(DefaultStringRedisConnection.java:946)
     [java]     at org.springframework.data.redis.core.DefaultValueOperations$3.inRedis(DefaultValueOperations.java:240)
     [java]     at org.springframework.data.redis.core.AbstractOperations$ValueDeserializingRedisCallback.doInRedis(AbstractOperations.java:59)
     [java]     at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:224)
     [java]     at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:184)
     [java]     at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:95)
     [java]     at org.springframework.data.redis.core.DefaultValueOperations.set(DefaultValueOperations.java:236)
     [java]     at com.mir3.sawtooth.service.redis.RedisConnector.init(RedisConnector.java:38)
     [java]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     [java]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
     [java]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
     [java]     at java.lang.reflect.Method.invoke(Method.java:498)
     [java]     at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:389)
     [java]     at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:333)
     [java]     at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:157)
     [java]     ... 36 common frames omitted
     [java] Caused by: redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
     [java]     at redis.clients.util.Pool.getResource(Pool.java:53)
     [java]     at redis.clients.jedis.JedisPool.getResource(JedisPool.java:226)
     [java]     at redis.clients.jedis.JedisSlotBasedConnectionHandler.getConnectionFromSlot(JedisSlotBasedConnectionHandler.java:66)
     [java]     at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:116)
     [java]     at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:141)
     [java]     at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:141)
     [java]     at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:141)
     [java]     at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:141)
     [java]     at redis.clients.jedis.JedisClusterCommand.runBinary(JedisClusterCommand.java:60)
     [java]     at redis.clients.jedis.BinaryJedisCluster.set(BinaryJedisCluster.java:77)
     [java]     at org.springframework.data.redis.connection.jedis.JedisClusterStringCommands.set(JedisClusterStringCommands.java:118)
     [java]     ... 52 common frames omitted
     [java] Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: connect timed out
     [java]     at redis.clients.jedis.Connection.connect(Connection.java:207)
     [java]     at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:93)
     [java]     at redis.clients.jedis.BinaryJedis.connect(BinaryJedis.java:1767)
     [java]     at redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:106)
     [java]     at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:868)
     [java]     at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:435)
     [java]     at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:363)
     [java]     at redis.clients.util.Pool.getResource(Pool.java:49)
     [java]     ... 62 common frames omitted
     [java] Caused by: java.net.SocketTimeoutException: connect timed out
     [java]     at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
     [java]     at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
     [java]     at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
     [java]     at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
     [java]     at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
     [java]     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
     [java]     at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
     [java]     at java.net.Socket.connect(Socket.java:589)
     [java]     at redis.clients.jedis.Connection.connect(Connection.java:184)
     [java]     ... 69 common frames omitted

如何解决?

附言

我使用 spring-data-redis 2.1.10.Release

更新

根据 Andreas 在评论中的回复,我尝试修改撰写文件:

version: '2'
services:
  redis-node-0:
    image: docker.io/bitnami/redis-cluster:6.0-debian-10
    volumes:
      - redis-cluster_data-0:/bitnami/redis/data
    environment:
      - 'REDIS_PASSWORD=se'
      - 'REDIS_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5'
    network_mode: 'host'

  redis-node-1:
    image: docker.io/bitnami/redis-cluster:6.0-debian-10
    volumes:
      - redis-cluster_data-1:/bitnami/redis/data
    environment:
      - 'REDIS_PASSWORD=se'
      - 'REDIS_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5'
    network_mode: 'host'

  redis-node-2:
    image: docker.io/bitnami/redis-cluster:6.0-debian-10
    volumes:
      - redis-cluster_data-2:/bitnami/redis/data
    environment:
      - 'REDIS_PASSWORD=se'
      - 'REDIS_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5'
    network_mode: 'host'

  redis-node-3:
    image: docker.io/bitnami/redis-cluster:6.0-debian-10
    volumes:
      - redis-cluster_data-3:/bitnami/redis/data
    environment:
      - 'REDIS_PASSWORD=se'
      - 'REDIS_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5'
    network_mode: 'host'

  redis-node-4:
    image: docker.io/bitnami/redis-cluster:6.0-debian-10
    volumes:
      - redis-cluster_data-4:/bitnami/redis/data
    environment:
      - 'REDIS_PASSWORD=se'
      - 'REDIS_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5'
    network_mode: 'host'

  redis-node-5:
    image: docker.io/bitnami/redis-cluster:6.0-debian-10
    volumes:
      - redis-cluster_data-5:/bitnami/redis/data
    environment:
      - 'REDIS_PASSWORD=se'
      - 'REDIS_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5'
    network_mode: 'host'

  redis-cluster-init:
    image: docker.io/bitnami/redis-cluster:6.0-debian-10
    depends_on:
      - redis-node-0
      - redis-node-1
      - redis-node-2
      - redis-node-3
      - redis-node-4
      - redis-node-5
    environment:
      - 'REDISCLI_AUTH=se'
      - 'REDIS_CLUSTER_REPLICAS=1'
      - 'REDIS_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5'
      - 'REDIS_CLUSTER_CREATOR=yes'
    network_mode: 'host'

volumes:
  redis-cluster_data-0:
    driver: local
  redis-cluster_data-1:
    driver: local
  redis-cluster_data-2:
    driver: local
  redis-cluster_data-3:
    driver: local
  redis-cluster_data-4:
    driver: local
  redis-cluster_data-5:
    driver: local

但错误还是一样

【问题讨论】:

  • 后退一步并仅使用 redis-cli 连接到您的集群节点之一也失败了,但是在容器内也可以工作。我认为这是由于这句话:Currently Redis Cluster does not support NATted environments and in general environments where IP addresses or TCP ports are remapped. ... In order to make Docker compatible with Redis Cluster you need to use the host networking mode of Docker.redis.io/topics/cluster-tutorial
  • @Andreas 我应该为撰写中的每个服务添加 network_mode: 'host' 吗?我添加到每个服务,但它没有帮助

标签: java spring redis docker-compose redis-cluster


【解决方案1】:

更新

由于您是在 Windows 上运行 docker,因此值得一读 this limitation of Windows containers for published ports

如果您使用的是 Windows 10 18.09 之前的版本,已发布 Windows 容器上的端口存在环回问题 本地主机。您只能使用从主机访问容器端点 容器的 IP 和端口。在 Windows 10 18.09 中,容器可以工作 在本地主机上发布端口。

您可以尝试使用容器的 ip 和端口。使用以下命令获取容器 ip -

docker inspect -f "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" container_name_or_id

然后验证您是否能够 ping 容器的 ip。如果 ping 有效,您就可以开始了。

原答案

您的docker-compose 为我工作没有任何问题,在您的java 配置中设置密码做了一些小改动,否则会抛出NOAUTH Authentication required。我用过 spring-data-redis-2.3.3 -

redisClusterConfiguration.setPassword("pass");

下面是我的pom.xml 的样子 -

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.springboot</groupId>
    <artifactId>redis-integration</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>redis-integration</name>
    <description>Spring boot integration with Redis Server Sentinels</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

请查看this GitHub project,其中包含您的docker-compose 和java 配置。从根文件夹运行docker-compose up --build,然后运行mvn spring-boot:run

注意:我已经在 Ubuntu 18.04.5 LTS 上试过这个

【讨论】:

  • 感谢密码 - 这只是一个问题 - 而不是代码。您使用哪个操作系统?我使用 Windows,这可能很重要
  • 您的主机绑定似乎有问题。你能给我看看docker ps的截图吗?
  • 对。问题出在Windows上。我使用 Ubuntu。
  • 更新了我的答案。
  • 我应该用 smth 替换 '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' 吗?
【解决方案2】:

将属性 spring.redis.ssltrue 更改为 false 在本地 Windows 操作系统(版本 10.0.18363 Build 18363)。

【讨论】:

    猜你喜欢
    • 2012-10-14
    • 2014-03-30
    • 2015-03-02
    • 2018-03-18
    • 1970-01-01
    • 2014-06-08
    • 2017-09-15
    • 2019-10-27
    • 1970-01-01
    相关资源
    最近更新 更多