Redis 连接错误/连接池配置错误redisConnectionFactory

项目启动失败,错误报告指向Redis连接池配置错误。最终确定是因为缺少连接池依赖导致配置失败。
部署运行你感兴趣的模型镜像

Redis 连接错误/连接池配置错误

【说明】:项目采用Redia在redis方面采用RedisTemplate进行方法调用,在配置中采用RedisConnectionFactory和redis连接池

【错误现象】
在这里插入图片描述
在这里插入图片描述

项目启动不开

Error starting ApplicationContext. To display the conditions report re-run your application with ‘debug’ enabled.
2020-11-11 17:53:58.782 ERROR 18680 — [ main] o.s.boot.SpringApplication : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘userController’: Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘userServiceImpl’: Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘redisService’: Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘redisTemplate’ defined in class path resource [com/neusiri/config/RedisConfig.class]: Unsatisfied dependency expressed through method ‘redisTemplate’ parameter 0; nested exception is 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
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:324) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1411) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]

【错误分析思路】

在通过错误报告中可以一点一点寻找,开始错误提示为Controller出错,但是查看错误原因[往后找]其实并不是在controller层,而是在RedisConfig.class中,再往下看错误可能出现在RedisConnectionFactory中,因此推断可能是在配置文件中redis的配置错误,但是检查后发现配置文件中并没有错误,因此推断问题可能出现在pom文件中依赖没有引入,通过问题可能出现在RedisConnectionFactory中推断一定是连接上出了问题因此查看连接的依赖。
最后发现未引入连接池依赖,因为在配置文件中redis是通过连接池配置的,因此没引入连接池redis配置不成功。

【解决方法】

引入连接池依赖

        <!--redis依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <!--连接池依赖-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>

【redis依赖】-pom文件
在这里插入图片描述

【配置文件中redis配置信息】

#配置redis信息
#redis服务器地址
spring.redis.host=localhost
#redis服务器端口
spring.redis.port=6379
#连接池最大连接数(使用负数表示没有限制)默认8
spring.redis.lettuce.pool.max-active=100
#连接池最大阻塞等待时间(使用负值表示没有限制)默认-1
spring.redis.lettuce.pool.max-wait=PT10S
#连接池最大空闲连接数(使用负值表示没有限制)默认8
spring.redis.lettuce.pool.max-idle=30
#连接池最小空闲连接数(使用负值表示没有限制)默认0
spring.redis.lettuce.pool.min-idle=1
#连接超时时间
spring.redis.timeout=PT10S

【注】刷新Maven=>重启

您可能感兴趣的与本文相关的镜像

Wan2.2-T2V-A5B

Wan2.2-T2V-A5B

文生视频
Wan2.2

Wan2.2是由通义万相开源高效文本到视频生成模型,是有​50亿参数的轻量级视频生成模型,专为快速内容创作优化。支持480P视频生成,具备优秀的时序连贯性和运动推理能力

前提 redis集群每个节点可达可联通 , redisTemplate.opsForValues.set()操作是成功的 , 但是就是get不到数据 @Bean(name = "redisTemplate") public RedisTemplate<String, Object> redisTemplate(@Qualifier("redissonClient") RedissonClient redissonClient) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(new RedissonConnectionFactory(redissonClient)); // 统一使用 UTF-8 字符串序列化器 StringRedisSerializer stringSerializer = new StringRedisSerializer(StandardCharsets.UTF_8); redisTemplate.setKeySerializer(stringSerializer); redisTemplate.setHashKeySerializer(stringSerializer); redisTemplate.setValueSerializer(stringSerializer); redisTemplate.setHashValueSerializer(stringSerializer); redisTemplate.setEnableTransactionSupport(false); // 初始化 redisTemplate.afterPropertiesSet(); return redisTemplate; } // JSON 序列器 private RedisSerializer<String> keySerializer(){ return new StringRedisSerializer(); } private RedisSerializer<String> valueSerializer(){ return new StringRedisSerializer(); } // private RedisSerializer<Object> valueSerializer(){ // return new GenericJackson2JsonRedisSerializer(); // } @Bean @Primary public RedisConnectionFactory redisConnectionFactory(RedissonClient redissonClient) { return new RedissonConnectionFactory(redissonClient); } /** * 多服务系统配置 */ @Bean(name = "redissonClient") @Primary public RedissonClient clusterRedissonClient(){ Config config = new Config(); config.useClusterServers() .addNodeAddress( "redis://192.168.126.100:6380","redis://192.168.126.100:6381","redis://192.168.126.100:6382", "redis://192.168.126.101:6380","redis://192.168.126.101:6381","redis://192.168.126.101:6382", "redis://192.168.126.102:6380","redis://192.168.126.102:6381","redis://192.168.126.102:6382") .setPassword("02171019") .setScanInterval(5000) //置集群状态扫描间隔时间 , 单位为毫秒 .setRetryAttempts(3) //命令重试次数 .setRetryInterval(1500) //命令重试间隔 .setFailedSlaveCheckInterval(30000); //设置从节点重连间隔 config.setCodec(StringCodec.INSTANCE); //数据不会被redis额外编码 return Redisson.create(config); } @Test @LoggerAnnotation public void A() throws InterruptedException { // 插入单条数据 redisTemplate.opsForValue().set("key1", "你好"); System.out.println(redisTemplate.opsForValue().get("key1")); // 插入单条数据(存在有效期) System.out.println("-----------------"); redisTemplate.opsForValue().set("key2", "噢 , 你好", 1, TimeUnit.MINUTES);//向redis里存入数据和设置缓存时间 System.out.println(redisTemplate.hasKey("key2")); System.out.println(redisTemplate.opsForValue().get("key2")); System.out.println(redisTemplate.hasKey("key2"));//检查key是否存在,返回boolean值 System.out.println(redisTemplate.opsForValue().get("key2")); System.out.println("-----------------"); RBucket<String> bucket = redissonClient.getBucket("key234"); bucket.set("你好呀",10,TimeUnit.MINUTES); String value = bucket.get(); System.out.println(value); } 以上就是代码部分 , 请帮我排除问题所在 , 并且告诉我解决方式 , 并且上文条件作为这个session之后问题的条件
10-29
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值