Spring Boot 3.0中redis配置变更,导致Spring Boot 2.x项目升级后redis连接出错

Spring Boot 2.x的redis配置为spring.redis开头,比如spring.redis.host【1】。但是Spring Boot 3.0中,redis配置变更为以spring.data.redis开头,比如spring.data.redis.host【2】。

如果不更改相关配置,将导致Spring Boot 2.x项目升级到Spring Boot 3.0后,出现redis连接出错。比如”java.net.ConnectException: Connection refused“、或”redis.clients.jedis.exceptions.JedisConnectionException: Failed to connect to any host resolved for DNS name.“:

2022-12-31 13:10:51.681 [http-nio-17675-exec-5] ERROR o.a.c.c.C.[.[.[/toolset].[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [/toolset] threw exception [Request processing failed: org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis] with root cause
java.net.ConnectException: Connection refused
	at java.base/sun.nio.ch.Net.pollConnect(Native Method)
	at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672)
	at java.base/sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:946)
	at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:337)
	at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:334)
	at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:776)
	at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:724)
	at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:650)
	at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:562)
	at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997)
	at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.base/java.lang.Thread.run(Thread.java:857)

参考链接:

【1】https://docs.spring.io/spring-boot/docs/2.7.7/reference/html/application-properties.html#application-properties.data.spring.redis.host

【2】https://docs.spring.io/spring-boot/docs/3.0.0/reference/html/application-properties.html#application-properties.data.spring.data.redis.host

【3】https://juejin.cn/post/7173197506363260964

【4】https://juejin.cn/post/7181019431558971451

本页永久链接:https://www.orztip.com/?p=742&article_title=spring-boot-3-redis-config-change-to-spring-data-redis-prefix