【学习之路】spring boot 整合redis 报错

本文介绍了在Spring Boot项目中整合Redis时遇到的问题,包括报错信息和之前采用的错误配置。通过分享解决方法,帮助读者理解并避免此类问题,同时也欢迎读者留言交流更好的实践建议。

一. 踩坑原因

报错内容:

2022-04-27 17:34:24.004  WARN 2159230 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'redisController' defined in URL [jar:file:/usr/local/sbin/demo-web-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/com/dx/demoweb/controller/RedisController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.springframework.data.redis.core.RedisTemplate<?, ?>' available: expected single matching bean but found 2: redisTemplate,stringRedisTemplate
2022-04-27 17:34:24.067  INFO 2159230 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2022-04-27 17:34:24.102  INFO 2159230 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-04-27 17:34:24.148 ERROR 2159230 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.dx.demoweb.controller.RedisController required a single bean, but 2 were found:
        - redisTemplate: defined by method 'redisTemplate' in class path resource [org/springframework/boot/autoconfigure/data/redis/RedisAutoConfiguration.class]
        - stringRedisTemplate: defined by method 'stringRedisTemplate' in class path resource [org/springframework/boot/autoconfigure/data/redis/RedisAutoConfiguration.class]


Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

2022-04-27 17:34:25.085  WARN 2159230 --- [       Thread-3] c.a.n.common.http.HttpClientBeanHolder   : [HttpClientBeanHolder] Start destroying common HttpClient
2022-04-27 17:34:25.086  WARN 2159230 --- [       Thread-3] c.a.n.common.http.HttpClientBeanHolder   : [HttpClientBeanHolder] Destruction of the end

二. 踩坑之前

我之前用的构造函数的方式注入


    private RedisTemplate redisTemplate;

    @Autowired
    RedisController(RedisTemplate redisTemplate){
        this.redisTemplate = redisTemplate;
    }

二. 解决方法

我的解决方式:

    @Autowired
    @Qualifier("redisTemplate")
    private RedisTemplate redisTemplate;

PS

希望对看到本文的人有所帮助,如果你有更好的建议或思路欢迎留言。

<think>好的,用户现在想查找Redis相关错误的解决方案。首先,我需要回顾之前提供的引用内容,看看有哪些相关的报错和解决方法可以整合。 引用[1]提到了启动Redis的三种方式,包括默认端口、指定端口和配置文件启动。用户可能遇到启动时的端口冲突或配置错误,需要检查端口是否被占用,或者配置文件是否正确。比如如果使用配置文件启动,应该确保路径正确,配置参数无误。 引用[2]涉及安装Redis时的编译错误,缺少hiredis库。这可能是因为依赖没有正确安装,用户需要进入deps目录手动编译这些依赖,比如运行make hiredis jemalloc linenoise lua,然后再整体编译Redis。 引用[3]是Spring Boot整合Redis时的版本兼容问题。用户如果使用较高版本的Spring Boot可能会遇到类不兼容的情况,解决方法包括降级Spring Boot版本或者更新Redis客户端依赖,比如使用Lettuce替代Jedis,并确保版本匹配。 引用[4]提到Podman运行Redis时的SELinux权限问题。解决方案涉及临时或永久关闭SELinux,或者调整策略以允许容器访问所需资源。这可能涉及到setenforce命令或修改SELinux配置文件。 接下来,我需要将这些信息组织成用户容易理解的步骤。常见错误可能包括启动失败、依赖缺失、版本冲突和权限问题。需要分门别类,给出具体的解决步骤,比如针对启动问题,检查端口和配置文件;安装问题解决依赖编译;整合应用时的版本调整;容器环境下的SELinux设置。 另外,用户可能需要知道如何验证解决方案是否有效,比如如何检查Redis是否成功启动,如何测试连接,以及如何确认版本兼容性。同时,提醒用户修改配置或降级版本时的注意事项,比如备份配置文件,了解版本变更可能带来的影响。 最后,生成相关问题,帮助用户进一步排查可能遇到的其他问题,比如性能调优、持久化配置、高可用方案等。确保回答结构清晰,步骤明确,参考引用内容但不过度依赖,以解决用户的实际问题为导向。</think>### Redis报错解决方案整理 #### 一、Redis启动报错 1. **端口冲突** - 现象:`Could not create server TCP listening socket *:6379: bind: Address already in use` - 解决方法: - 终止占用端口的进程:`kill -9 $(lsof -t -i:6379)` - 指定其他端口启动:`redis-server --port 8888` - 通过配置文件启动(需修改`port`字段)[^1] 2. **配置文件错误** - 现象:`FATAL CONFIG FILE ERROR` - 解决方法: - 检查配置文件路径:`./src/redis-server /path/to/redis.conf` - 验证配置文件语法:`redis-server --test /path/to/redis.conf` --- #### 二、安装编译报错 1. **依赖缺失错误** - 现象:`error...deps/hiredis/libhiredis.a: No such file or directory` - 解决方法: - 进入Redis源码目录的`deps`文件夹 - 手动编译依赖: ```bash cd deps make hiredis jemalloc linenoise lua ``` - 返回根目录重新编译Redis[^2] --- #### 三、应用整合报错 1. **Spring Boot版本冲突** - 现象:`java.lang.NoSuchMethodError: org.apache.commons.pool2.impl.GenericObjectPoolConfig.setMaxWait` - 解决方法: - **方案1**:降级Spring Boot版本至2.0.x(修改`pom.xml`)[^3] ```xml <version>2.0.2.RELEASE</version> ``` - **方案2**:升级Redis客户端依赖(推荐使用Lettuce替代Jedis) ```xml <dependency> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> <version>6.2.4.RELEASE</version> </dependency> ``` --- #### 四、容器环境报错 1. **Podman/SELinux权限问题** - 现象:`Permission denied`或`无法访问持久化文件` - 解决方法: - 临时关闭SELinux:`sudo setenforce 0` - 永久关闭SELinux:修改`/etc/selinux/config`为`SELINUX=disabled` - 添加容器安全策略:`podman run --security-opt label=disable ...`[^4] --- #### 五、连接测试方法 验证Redis服务是否正常: ```bash redis-cli -p 6379 ping # 返回PONG表示成功 ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值