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

〇、背景

  已在虚拟机的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=3006 spring.redis.jedis.pool.max-idle=1007 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.解决

因为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

原文地址:https://www.cnblogs.com/Drajun/p/12398339.html

时间: 2024-08-29 20:16:23

在Spring Boot 上配置Redis Cluster出现的异常的相关文章

Spring Boot 2.x Redis多数据源配置(jedis,lettuce)

Spring Boot 2.x Redis多数据源配置(jedis,lettuce) 96 不敢预言的预言家 0.1 2018.11.13 14:22* 字数 65 阅读 727评论 0喜欢 2 多数据源最终表现其实就是 redis connection factory 不同 springboot 默认的redis配置维护了一套 connection factory 自己维护一套 connection factory 即可实现 application.yml spring: redis: # 默

玩转spring boot——properties配置

前言 在以往的java开发中,程序员最怕大量的配置,是因为配置一多就不好统一管理,经常出现找不到配置的情况.而项目中,从开发测试环境到生产环境,往往需要切换不同的配置,如测试数据库连接换成生产数据库连接,若有一处配错或遗漏,就会带来不可挽回的损失.正因为这样,spring boot给出了非常理想的解决方案——application.properties.见application-properties的官方文档:http://docs.spring.io/spring-boot/docs/curr

Spring Boot 揭秘与实战 附录 - Spring Boot 公共配置

Spring Boot 公共配置,配置 application.properties/application.yml 文件中. 摘自:http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html # =================================================================== # COMMON SPRING

Spring Boot自动配置原理(转)

第3章 Spring Boot自动配置原理 3.1 SpringBoot的核心组件模块 首先,我们来简单统计一下SpringBoot核心工程的源码java文件数量: 我们cd到spring-boot-autoconfigure工程根目录下.执行 $ tree | grep -c .java$ 模块 java文件数 spring-boot 551 spring-boot-actuator 423 spring-boot-autoconfigure 783 spring-boot-devtools

spring boot 中使用redis session

spring boot 默认的httpsession是存在内存中.这种默认方式有几个缺点:1.当分布式部署时,存在session不一致的问题:2.当服务重启时session就会丢失,这时候用户就需要重新登陆,可能导致用户数据丢失.通常会使用redis来保存session. 在spring boot中利用redis来保存session是非常简单.只需要简单的几步就可以了.可以参考官方教程.https://docs.spring.io/spring-session/docs/current/refe

spring boot 环境配置(profile)切换

Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 spring boot 连接Mysql spring boot配置druid连接池连接mysql spring boot集成mybatis(1) spring boot集成mybatis(2) – 使用pagehelper实现分页 spring boot集成mybatis(3) – mybatis ge

Spring Boot 属性配置和使用

spring Boot 允许通过外部配置让你在不同的环境使用同一应用程序的代码,简单说就是可以通过配置文件来注入属性或者修改默认的配置. Spring Boot 系列 Spring Boot 入门 Spring Boot 属性配置和使用 Spring Boot 集成MyBatis Spring Boot 静态资源处理 Spring Boot - 配置排序依赖技巧 Spring Boot - DevTools 介绍 Spring Boot 支持多种外部配置方式 这些方式优先级如下: 命令行参数 来

Spring Boot 属性配置和使用(转)

Spring Boot 属性配置和使用 Spring Boot 允许通过外部配置让你在不同的环境使用同一应用程序的代码,简单说就是可以通过配置文件来注入属性或者修改默认的配置. Spring Boot 入门 请看:http://blog.csdn.net/isea533/article/details/50278205 Spring Boot 支持多种外部配置方式 这些方式优先级如下: 命令行参数 来自java:comp/env的JNDI属性 Java系统属性(System.getPropert

springboot(十七):使用Spring Boot上传文件

上传文件是互联网中常常应用的场景之一,最典型的情况就是上传头像等,今天就带着带着大家做一个Spring Boot上传文件的小案例. 1.pom包配置 我们使用Spring Boot最新版本1.5.9.jdk使用1.8.tomcat8.0. <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId>