我的卡桑德拉码头-撰写文件:
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发布
更新
基于评论中的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
但是错误还是一样的
发布于 2020-09-29 10:29:45
更新
因为您是在Windows上运行docker,所以值得一读对已发布端口的Windows容器的限制。
如果您正在使用Windows 10 18.09之前的版本,则Windows容器上已发布的端口存在回本地主机的问题。您只能使用容器的IP和端口从主机到达容器端点。在Windows 10 18.09中,容器使用本地主机上发布的端口。
您可以尝试使用容器的ip和端口。使用下面的命令获取容器ip -
docker inspect -f "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" container_name_or_id
然后验证您是否能够打开容器的ip。如果叮当有效,你就可以走了。
原始答案
您的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>
请查看这个GitHub项目,其中有您的docker-compose
和java配置。运行docker-compose up --build
,然后从根文件夹运行mvn spring-boot:run
。
注意事项:我在Ubuntu18.04.5LTS上尝试过这个
发布于 2021-01-12 12:53:52
在本地Windows中,将属性spring.redis.ssl从true更改为false对我来说很好(Version10.0.18363Build 18363)。
https://stackoverflow.com/questions/64054079
复制相似问题