ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

在Spring Boot 上配置Redis Cluster出现的异常

2020-03-02 22:02:08  阅读:7866  来源: 互联网

标签:engine java Spring Redis 192.168 Cluster 22.130 org junit


〇、背景

  已在虚拟机的docker中搭建好了6个节点的redis 集群:Docker Redis 5.0集群搭建

  现在在宿主机以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
View Code

 

4.解决

因为spring-boot-starter-data-redis默认的redis客户端是使用lettuce,而上面我配置是jedis,所以LettuceConnectionFactory才会报错。

把配置中的jedis改为lettuce

 

#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

 

 

二、异常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

标签:engine,java,Spring,Redis,192.168,Cluster,22.130,org,junit
来源: https://www.cnblogs.com/Drajun/p/12398339.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有