〇、背景
已在虚拟机(CentOS 7,在本文也称其为宿主机——即docker容器的宿主机)的docker中搭建好了6个节点的redis (host网络模式)集群:Docker Redis 5.0集群搭建
现在在本机(Windows 7)以Spring Boot环境连接集群
一、异常1:无法连接集群
1.配置:
1 #rediss
2 spring.redis.cluster.nodes=192.168.22.130:7001,192.168.22.130:7002,192.168.22.130:7003,192.168.22.130:7004,192.168.22.130:7005,192.168.22.130:7006
3 spring.redis.timeout=60000
4 spring.redis.jedis.pool.max-wait=-1
5 spring.redis.jedis.pool.max-active=300
6 spring.redis.jedis.pool.max-idle=100
7 spring.redis.jedis.pool.min-idle=20
2.报错:
org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisException:
Cannot retrieve initial cluster partitions from initial URIs [RedisURI [host='192.168.22.130', port=7001], RedisURI [host='192.168.22.130', port=7002], RedisURI [host='192.168.22.130', port=7003],
RedisURI [host
='192.168.22.130', port=7004], RedisURI [host='192.168.22.130', port=7005], RedisURI [host='192.168.22.130', port=7006]]
at
org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getNativeConnection(LettuceConnectionFactory.java:1199)
3.报错具体信息:
2020-02-29 23:30:11.809 WARN 24336 --- [ioEventLoop-4-3]
i.l.c.c.topology.ClusterTopologyRefresh : Unable to connect to
[192.168.22.130:7003]: connection timed out: /192.168.22.130:7003
2020-02-29 23:30:11.809 WARN 24336 --- [ioEventLoop-4-4]
i.l.c.c.topology.ClusterTopologyRefresh : Unable to connect to
[192.168.22.130:7004]: connection timed out: /192.168.22.130:7004
2020-02-29 23:30:11.810 WARN 24336 --- [ioEventLoop-4-2]
i.l.c.c.topology.ClusterTopologyRefresh : Unable to connect to
[192.168.22.130:7002]: connection timed out: /192.168.22.130:7002
2020-02-29 23:30:11.809 WARN 24336 --- [ioEventLoop-4-1]
i.l.c.c.topology.ClusterTopologyRefresh : Unable to connect to
[192.168.22.130:7001]: connection timed out: /192.168.22.130:7001
2020-02-29 23:30:11.824 WARN 24336 --- [ioEventLoop-4-1]
i.l.c.c.topology.ClusterTopologyRefresh : Unable to connect to
[192.168.22.130:7005]: connection timed out: /192.168.22.130:7005
2020-02-29 23:30:11.824 WARN 24336 --- [ioEventLoop-4-2]
i.l.c.c.topology.ClusterTopologyRefresh : Unable to connect to
[192.168.22.130:7006]: connection timed out: /192.168.22.130:7006
org.springframework.data.redis.RedisConnectionFailureException: Unable to
connect to Redis; nested exception is io.lettuce.core.RedisException:
Cannot retrieve initial cluster partitions from initial URIs [RedisURI
[host='192.168.22.130', port=7001], RedisURI [host='192.168.22.130',
port=7002], RedisURI [host='192.168.22.130', port=7003], RedisURI
[host='192.168.22.130', port=7004], RedisURI [host='192.168.22.130',
port=7005], RedisURI [host='192.168.22.130', port=7006]]
at
org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory
$SharedConnection.getNativeConnection(LettuceConnectionFactory.java:1199)
at
org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory
$SharedConnection.getConnection(LettuceConnectionFactory.java:1178)
at
org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory
.getClusterConnection(LettuceConnectionFactory.java:372)
at
org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory
.getConnection(LettuceConnectionFactory.java:349)
at
org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection
(RedisConnectionUtils.java:134)
at
org.springframework.data.redis.core.RedisConnectionUtils.getConnection
(RedisConnectionUtils.java:97)
at
org.springframework.data.redis.core.RedisConnectionUtils.getConnection
(RedisConnectionUtils.java:84)
at org.springframework.data.redis.core.RedisTemplate.execute
(RedisTemplate.java:215)
at org.springframework.data.redis.core.RedisTemplate.execute
(RedisTemplate.java:188)
at org.springframework.data.redis.core.RedisTemplate.randomKey
(RedisTemplate.java:921)
at
com.drajun.rushbuying.RushBuyingApplicationTests.getRedisKeyTest
(RushBuyingApplicationTests.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke
(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod
(ReflectionUtils.java:675)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed
(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain
$ValidatingInvocation.proceed(InvocationInterceptorChain.java:125)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept
(TimeoutExtension.java:132)
at
org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMetho
d(TimeoutExtension.java:124)
at
org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod
(TimeoutExtension.java:74)
at org.junit.jupiter.engine.execution.ExecutableInvoker
$ReflectiveInterceptorCall.lambda$ofVoidMethod$0
(ExecutableInvoker.java:115)
at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda
$invoke$0(ExecutableInvoker.java:105)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain
$InterceptedInvocation.proceed(InvocationInterceptorChain.java:104)
at
org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed
(InvocationInterceptorChain.java:62)
at
org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvo
ke(InvocationInterceptorChain.java:43)
at
org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke
(InvocationInterceptorChain.java:35)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke
(ExecutableInvoker.java:104)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke
(ExecutableInvoker.java:98)
at
org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda
$invokeTestMethod$6(TestMethodTestDescriptor.java:202)
at
org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute
(ThrowableCollector.java:73)
at
org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMet
hod(TestMethodTestDescriptor.java:198)
at
org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute
(TestMethodTestDescriptor.java:135)
at
org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute
(TestMethodTestDescriptor.java:69)
at
org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda
$executeRecursively$5(NodeTestTask.java:135)
at
org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute
(ThrowableCollector.java:73)
at
org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda
$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around
(Node.java:135)
at
org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda
$executeRecursively$8(NodeTestTask.java:123)
at
org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute
(ThrowableCollector.java:73)
at
org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursi
vely(NodeTestTask.java:122)
at
org.junit.platform.engine.support.hierarchical.NodeTestTask.execute
(NodeTestTask.java:80)
at java.util.ArrayList.forEach(ArrayList.java:1249)
at
org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestE
xecutorService.invokeAll
(SameThreadHierarchicalTestExecutorService.java:38)
at
org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda
$executeRecursively$5(NodeTestTask.java:139)
at
org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute
(ThrowableCollector.java:73)
at
org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda
$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around
(Node.java:135)
at
org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda
$executeRecursively$8(NodeTestTask.java:123)
at
org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute
(ThrowableCollector.java:73)
at
org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursi
vely(NodeTestTask.java:122)
at
org.junit.platform.engine.support.hierarchical.NodeTestTask.execute
(NodeTestTask.java:80)
at java.util.ArrayList.forEach(ArrayList.java:1249)
at
org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestE
xecutorService.invokeAll
(SameThreadHierarchicalTestExecutorService.java:38)
at
org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda
$executeRecursively$5(NodeTestTask.java:139)
at
org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute
(ThrowableCollector.java:73)
at
org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda
$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around
(Node.java:135)
at
org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda
$executeRecursively$8(NodeTestTask.java:123)
at
org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute
(ThrowableCollector.java:73)
at
org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursi
vely(NodeTestTask.java:122)
at
org.junit.platform.engine.support.hierarchical.NodeTestTask.execute
(NodeTestTask.java:80)
at
org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestE
xecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
at
org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.ex
ecute(HierarchicalTestExecutor.java:57)
at
org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.exec
ute(HierarchicalTestEngine.java:51)
at org.junit.platform.launcher.core.DefaultLauncher.execute
(DefaultLauncher.java:229)
at org.junit.platform.launcher.core.DefaultLauncher.lambda
$execute$6(DefaultLauncher.java:197)
at
org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams
(DefaultLauncher.java:211)
at org.junit.platform.launcher.core.DefaultLauncher.execute
(DefaultLauncher.java:191)
at org.junit.platform.launcher.core.DefaultLauncher.execute
(DefaultLauncher.java:128)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs
(JUnit5IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.IdeaTestRunner
$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at
com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart
(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main
(JUnitStarter.java:70)
Caused by: io.lettuce.core.RedisException: Cannot retrieve initial cluster
partitions from initial URIs [RedisURI [host='192.168.22.130', port=7001],
RedisURI [host='192.168.22.130', port=7002], RedisURI
[host='192.168.22.130', port=7003], RedisURI [host='192.168.22.130',
port=7004], RedisURI [host='192.168.22.130', port=7005], RedisURI
[host='192.168.22.130', port=7006]]
at io.lettuce.core.cluster.RedisClusterClient.loadPartitions
(RedisClusterClient.java:859)
at
io.lettuce.core.cluster.RedisClusterClient.initializePartitions
(RedisClusterClient.java:813)
at io.lettuce.core.cluster.RedisClusterClient.getPartitions
(RedisClusterClient.java:823)
at
org.springframework.data.redis.connection.lettuce.ClusterConnectionProvide
r.getConnectionAsync(ClusterConnectionProvider.java:92)
at
org.springframework.data.redis.connection.lettuce.ClusterConnectionProvide
r.getConnectionAsync(ClusterConnectionProvider.java:40)
at
org.springframework.data.redis.connection.lettuce.LettuceConnectionProvide
r.getConnection(LettuceConnectionProvider.java:53)
at
org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory
$SharedConnection.getNativeConnection(LettuceConnectionFactory.java:1197)
... 73 more
Caused by: io.lettuce.core.RedisConnectionException: Unable to establish a
connection to Redis Cluster at [RedisURI [host='192.168.22.130',
port=7001], RedisURI [host='192.168.22.130', port=7002], RedisURI
[host='192.168.22.130', port=7003], RedisURI [host='192.168.22.130',
port=7004], RedisURI [host='192.168.22.130', port=7005], RedisURI
[host='192.168.22.130', port=7006]]
at io.lettuce.core.cluster.topology.AsyncConnections.get
(AsyncConnections.java:89)
at
io.lettuce.core.cluster.topology.ClusterTopologyRefresh.loadViews
(ClusterTopologyRefresh.java:76)
at io.lettuce.core.cluster.RedisClusterClient.doLoadPartitions
(RedisClusterClient.java:865)
at io.lettuce.core.cluster.RedisClusterClient.loadPartitions
(RedisClusterClient.java:838)
... 79 more
4.解决
step(1)
因为spring-boot-starter-data-redis默认的redis客户端是使用lettuce,而上面我配置是jedis。
把配置中的jedis改为lettuce(当然选择 在pom文件中排除lettuce依赖,而使用jedis)
#rediss
spring.redis.cluster.nodes=192.168.22.130:7001,192.168.22.130:7002,192.168.22.130:7003,192.168.22.130:7004,192.168.22.130:7005,192.168.22.130:7006
spring.redis.timeout=60000
spring.redis.lettuce.pool.max-wait=-1
spring.redis.lettuce.pool.max-active=300
spring.redis.lettuce.pool.max-idle=100
spring.redis.lettuce.pool.min-idle=20
step(2) 注意在本机上看看能不能ping通所配置的redis集群节点
若ping不通,则需在本机添加到docker容器——redis集群节点的路由
windows命令行窗口下使用命令:route -p add 目标网络号 MASK 子网掩码 网关
例如:我的CentOS7ip配置如下:
并且我的redis配置时使用了host网络模式(即该容器共享了宿主机的网络命名空间,直接暴露在公共网络中,即通过宿主机ip+端口可访问该容器)。
所以在本机上加上路由信息:route -p add 192.168.22.0 MASK 255.255.255.0 172.17.0.2
再ping,便连通了。
step(3) 宿主机端口开放
注意在在本机上不仅要通过ip连得上虚拟机,并且得连接得到具体的端口(redis节点的端口)
在CentOS7虚拟机上使用 firewall-cmd --list-ports 命令查看防火墙开放的端口
若没有redis节点的端口,则使用 firewall-cmd --zone=public --add-port=80/tcp --permanent 命令开放redis各节点端口
使用 firewall-cmd --reload 重启防火墙
二、异常2:找不到类GenericObjectPoolConfig
1.报错
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisConnectionFactory' defined in class path resource
[org/springframework/boot/autoconfigure/data/redis/LettuceConnectionConfiguration.class]: Bean instantiation via factory method failed;
nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory]: Factory method
'redisConnectionFactory' threw exception;
nested exception is java.lang.NoClassDefFoundError: org
/apache/commons/pool2/impl/GenericObjectPoolConfig
2.解决
添加连接池依赖
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.8.0</version>
</dependency>
https://www.cnblogs.com/Drajun/p/12359277.html
来源:oschina
链接:https://my.oschina.net/u/4262079/blog/4437820