在Spring Boot 上配置Redis Cluster出現的異常

〇、背景

  已在虛擬機(CentOS 7,在本文也稱其爲宿主機——即docker容器的宿主機)的docker中搭建好了6個節點的redis (host網絡模式)集羣:Docker Redis 5.0集羣搭建html

  如今在本機(Windows 7)以Spring Boot環境鏈接集羣java

 

1、異常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
View Code

 

4.解決

step(1) node

  由於spring-boot-starter-data-redis默認的redis客戶端是使用lettuce,而上面我配置是jedis
redis

  把配置中的jedis改成lettuce(固然選擇 在pom文件中排除lettuce依賴,而使用jedis)spring

#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集羣節點docker

  若ping不通,則需在本機添加到docker容器——redis集羣節點的路由apache

  windows命令行窗口下使用命令:route -p add 目標網絡號 MASK 子網掩碼 網關windows

  例如:個人CentOS7ip配置以下:網絡

       

 

  而且個人redis配置時使用了host網絡模式(即該容器共享了宿主機的網絡命名空間,直接暴露在公共網絡中,即經過宿主機ip+端口可訪問該容器)。dom

  因此在本機上加上路由信息: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、異常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

相關文章
相關標籤/搜索