刚刚学习Spring Boot相关知识,比较迷茫,在集成Redis数据库的时候 就出现了一些问题,遇到的第一个问题就是无法连接到Redis,以下为错误log:
org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to localhost:6379
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getNativeConnection(LettuceConnectionFactory.java:1111) ~[spring-data-redis-2.1.8.RELEASE.jar:2.1.8.RELEASE]
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getConnection(LettuceConnectionFactory.java:1084) ~[spring-data-redis-2.1.8.RELEASE.jar:2.1.8.RELEASE]
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.getSharedConnection(LettuceConnectionFactory.java:866) ~[spring-data-redis-2.1.8.RELEASE.jar:2.1.8.RELEASE]
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.getConnection(LettuceConnectionFactory.java:341) ~[spring-data-redis-2.1.8.RELEASE.jar:2.1.8.RELEASE]
at org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:132) ~[spring-data-redis-2.1.8.RELEASE.jar:2.1.8.RELEASE]
at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:95) ~[spring-data-redis-2.1.8.RELEASE.jar:2.1.8.RELEASE]
at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:82) ~[spring-data-redis-2.1.8.RELEASE.jar:2.1.8.RELEASE]
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:211) ~[spring-data-redis-2.1.8.RELEASE.jar:2.1.8.RELEASE]
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:184) ~[spring-data-redis-2.1.8.RELEASE.jar:2.1.8.RELEASE]
at org.springframework.data.redis.core.RedisTemplate.delete(RedisTemplate.java:709) ~[spring-data-redis-2.1.8.RELEASE.jar:2.1.8.RELEASE]
at com.example.demo.listener.UserTestListener.contextInitialized(UserTestListener.java:30) ~[classes/:na]
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4680) [tomcat-embed-core-9.0.19.jar:9.0.19]
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5150) [tomcat-embed-core-9.0.19.jar:9.0.19]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) [tomcat-embed-core-9.0.19.jar:9.0.19]
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1377) [tomcat-embed-core-9.0.19.jar:9.0.19]
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1367) [tomcat-embed-core-9.0.19.jar:9.0.19]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_201]
at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75) [tomcat-embed-core-9.0.19.jar:9.0.19]
at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134) [na:1.8.0_201]
......(很长很长)
解决方法:
如果有同样遇到该问题的同学,首先别怀疑自己的代码问题,而是看看有没有启动Redis服务器(redis-server.exe):
如果已经排除了该问题的存在,在考虑别的原因:
1.application.yml的redis配置中的spring.redis.timeout中连接超时时间(毫秒)中时间设置不能为0
2.找到redis的配置文件 redis.conf :vim redis.conf 修改 protected-mode yes 改为:protected-mode no