Springboot集成Redis常见的报错和解决方案

这篇具有很好参考价值的文章主要介绍了Springboot集成Redis常见的报错和解决方案。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1. io.lettuce.core.protocol.CommandHandler : null Unexpected exception during request: java.io.IOException: 远程主机强迫关闭了一个现有的连接。

报错信息

during request: java.io.IOException: 远程主机强迫关闭了一个现有的连接。
项目时基于若依框架开发,在生产环境报错:

io.lettuce.core.protocol.CommandHandler : null Unexpected exception
during request: java.io.IOException: 远程主机强迫关闭了一个现有的连接。

详细日志信息如下:

INFO 3352 --- [ioEventLoop-4-1] io.lettuce.core.protocol.CommandHandler  : null Unexpected exception during request: java.io.IOException: 远程主机强迫关闭了一个现有的连接。

java.io.IOException: 远程主机强迫关闭了一个现有的连接。
	at sun.nio.ch.SocketDispatcher.read0(Native Method) ~[na:1.8.0_221]
	at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:43) ~[na:1.8.0_221]
	at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223) ~[na:1.8.0_221]
	at sun.nio.ch.IOUtil.read(IOUtil.java:192) ~[na:1.8.0_221]
	at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:380) ~[na:1.8.0_221]
	at io.netty.buffer.PooledByteBuf.setBytes(PooledByteBuf.java:253) ~[netty-buffer-4.1.69.Final.jar!/:4.1.69.Final]
	at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1132) ~[netty-buffer-4.1.69.Final.jar!/:4.1.69.Final]
	at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:350) ~[netty-transport-4.1.69.Final.jar!/:4.1.69.Final]
	at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:151) ~[netty-transport-4.1.69.Final.jar!/:4.1.69.Final]
	at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:719) [netty-transport-4.1.69.Final.jar!/:4.1.69.Final]
	at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655) [netty-transport-4.1.69.Final.jar!/:4.1.69.Final]
	at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581) [netty-transport-4.1.69.Final.jar!/:4.1.69.Final]
	at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493) [netty-transport-4.1.69.Final.jar!/:4.1.69.Final]
	at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:986) [netty-common-4.1.69.Final.jar!/:4.1.69.Final]
	at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) [netty-common-4.1.69.Final.jar!/:4.1.69.Final]
	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) [netty-common-4.1.69.Final.jar!/:4.1.69.Final]
	at java.lang.Thread.run(Thread.java:748) [na:1.8.0_221]
	2023-06-14 00:52:16.956  INFO 3352 --- [ionShutdownHook] org.quartz.core.QuartzScheduler          : Scheduler TaskScheduler_$_iZj575z0jja92rZ1686469913890 paused.
	2023-06-14 00:52:16.985  INFO 3352 --- [xecutorLoop-1-8] i.l.core.protocol.ConnectionWatchdog     : Reconnecting, last destination was /172.17.151.173:6379

原因分析

日志中容易看出报错信息与redis有关,并且springboot 使用 lettuce做redis 的连接池,lettuce 调用netty与redis服务器通讯

在gitee上找到了若依框架的相同问题,并且官方对于此问题给出了修改方案
Springboot集成Redis常见的报错和解决方案

解决方案

  1. 修改配置文件redis.windows.conf的timeout参数
    Springboot集成Redis常见的报错和解决方案
Close the connection after a client is idle for N seconds (0 to disable)
在客户端空闲N秒后关闭连接(0表示禁用)

Springboot集成Redis常见的报错和解决方案
2. 检查spring项目中redis的配置

redis:
   # 地址
   host: 192.168.0.1
   # 端口,默认为6379
   port: 6379
   # 数据库索引
   database: 10
   # 密码
   password: 147258
   # 连接超时时间 60s
   timeout: 60s
   lettuce:
     pool:
       # 连接池中的最小空闲连接 
       min-idle: 10
       # 连接池中的最大空闲连接 
       max-idle: 30
       # 连接池的最大数据库连接数
       max-active: 8
       # #连接池最大阻塞等待时间(使用负值表示没有限制)
       max-wait: -1ms

3.redis.windows.conf中设置tcp-keepalive时间为60s

# TCP keepalive.
#
# If non-zero, use SO_KEEPALIVE to send TCP ACKs to clients in absence
# of communication. This is useful for two reasons:
#
# 1) Detect dead peers.
# 2) Take the connection alive from the point of view of network
#    equipment in the middle.
#
# On Linux, the specified value (in seconds) is the period used to send ACKs.
# Note that to close the connection the double of the time is needed.
# On other kernels the period depends on the kernel configuration.
#
# A reasonable value for this option is 60 seconds.
tcp-keepalive 60

配置完成后重启redis服务

2. io.netty.util.internal.OutOfDirectMemoryError: failed to allocate 16777216 byte(s) of direct memory (used: 1073741824, max: 1073741824)

报错信息

io.netty.util.internal.OutOfDirectMemoryError: failed to allocate 16777216 byte(s) of direct memory (used: 1073741824, max: 1073741824)
    at io.netty.util.internal.PlatformDependent.incrementMemoryCounter(PlatformDependent.java:802) ~[netty-common-4.1.72.Final.jar:4.1.72.Final]
    at io.netty.util.internal.PlatformDependent.allocateDirectNoCleaner(PlatformDependent.java:731) ~[netty-common-4.1.72.Final.jar:4.1.72.Final]
    at io.netty.buffer.PoolArena$DirectArena.allocateDirect(PoolArena.java:648) ~[netty-buffer-4.1.72.Final.jar:4.1.72.Final]
    at io.netty.buffer.PoolArena$DirectArena.newChunk(PoolArena.java:623) ~[netty-buffer-4.1.72.Final.jar:4.1.72.Final]
    at io.netty.buffer.PoolArena.allocateNormal(PoolArena.java:202) ~[netty-buffer-4.1.72.Final.jar:4.1.72.Final]
    at io.netty.buffer.PoolArena.tcacheAllocateSmall(PoolArena.java:172) ~[netty-buffer-4.1.72.Final.jar:4.1.72.Final]
    at io.netty.buffer.PoolArena.allocate(PoolArena.java:134) ~[netty-buffer-4.1.72.Final.jar:4.1.72.Final]
    at io.netty.buffer.PoolArena.allocate(PoolArena.java:126) ~[netty-buffer-4.1.72.Final.jar:4.1.72.Final]
    at io.netty.buffer.PooledByteBufAllocator.newDirectBuffer(PooledByteBufAllocator.java:395) ~[netty-buffer-4.1.72.Final.jar:4.1.72.Final]
    at io.netty.buffer.AbstractByteBufAllocator.directBuffer(AbstractByteBufAllocator.java:188) ~[netty-buffer-4.1.72.Final.jar:4.1.72.Final]
    at io.netty.buffer.AbstractByteBufAllocator.directBuffer(AbstractByteBufAllocator.java:179) ~[netty-buffer-4.1.72.Final.jar:4.1.72.Final]
    at io.netty.buffer.AbstractByteBufAllocator.buffer(AbstractByteBufAllocator.java:116) ~[netty-buffer-4.1.72.Final.jar:4.1.72.Final]
    ...

原因分析

当前引入的依赖pom.xml

<!-- redis 缓存操作 -->
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-data-redis</artifactId>
 </dependency>

Springboot2.x以后弃用jedis改为使用lettuce作为操作redis数据库的客户端,而Lettuce使用的 netty 框架,引用的netty包netty-common-4.1.49.Final.jarPlatformDependent.java类 中的incrementMemoryCounter方法。该方法判断堆外内存是否大于当前服务可使用的内存,是则抛出throw new OutOfDirectMemoryError。因此该问题的发生是Spring对Lettuce的支持不够好,还存在问题。文章来源地址https://www.toymoban.com/news/detail-482343.html

 private static void incrementMemoryCounter(int capacity) {
        if (DIRECT_MEMORY_COUNTER != null) {
            long newUsedMemory = DIRECT_MEMORY_COUNTER.addAndGet(capacity);
            //校验堆外内存是否大于当前服务可使用的内存,如果大于则抛出 OutOfDirectMemoryError(堆外内存溢出)
            if (newUsedMemory > DIRECT_MEMORY_LIMIT) {
                DIRECT_MEMORY_COUNTER.addAndGet(-capacity);
                throw new OutOfDirectMemoryError("failed to allocate " + capacity
                        + " byte(s) of direct memory (used: " + (newUsedMemory - capacity)
                        + ", max: " + DIRECT_MEMORY_LIMIT + ')');
            }
        }
    }

解决方案

  1. 调大堆外内存 -Dio.netty.maxDirectMemory,但是治标不治本
  2. 放弃Lettuce客户端,改回Jedis客户客户端
<!--引入redis,排除lettuce的引用-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <exclusions> 
        <exclusion>
            <groupId>io.lettuce</groupId>
            <artifactId>lettuce-core</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<!-- 引入Jedis客戶端-->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
</dependency>

到了这里,关于Springboot集成Redis常见的报错和解决方案的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【YOLOv5】一些网上找不到答案的报错解决方案

    目录 AssertionError: Label class 4 exceeds nc=4 in /xxxxxx解决方法 原因 解决方法:(以我的情况为例) RuntimeError: result type Float can‘t be cast to the desired output type long int 原因 解决方法 ImportError: libgthread-2.0.so.0: cannot open shared object file: tensorboard :No dashboards are active for the current data set. 问题

    2024年02月12日
    浏览(26)
  • ChatGPT常见的报错解决方法(全网最全解决方法)

    因为最近在使用ChatGPT的过程中,时常会出现一些错误提示,为了方便自己快速解决问题,所以也搜集了一些其他博主的解决方法,以下是整理的内容。 目录 1、拒绝访问 2、Access denied错误 3、We have detected suspicious 错误 4、Too many requests in 1 hour. Try again later. 5、Not available 6、G

    2024年02月07日
    浏览(32)
  • 深度学习和日常代码中遇到的报错汇总及解决方案,持续更新中。。。。

    本文是深度学习和日常代码中遇到的报错汇总,因时间比较久,暂时都没有图片,只有文字描述。解决方案也大多参考网上的解决方案,有些有用,有些没有效果,本文章中的问题,也仅是本人遇到的问题,使用列举的方案已经解决。 处理:调用的方法是一个类,需要先进行

    2023年04月22日
    浏览(36)
  • 【YOLOv5】一些不常见的报错及解决【持续更新】

    目录 AssertionError: Label class 4 exceeds nc=4 in /xxxxxx解决方法 原因 解决方法:(以我的情况为例) RuntimeError: result type Float can‘t be cast to the desired output type long int 原因 解决方法 ImportError: libgthread-2.0.so.0: cannot open shared object file: tensorboard :No dashboards are active for the current data set. 问题

    2024年02月12日
    浏览(24)
  • 使用Python+selenium实例化Microsoft Edge或Chrome浏览器对象和常见的报错

    实例化谷歌浏览器对象: 实例化Microsoft Edge对象: 1.浏览器窗口闪退: 用import time    time.sleep(5),让浏览器多待上一会就好了 2.报错\\\"DeprecationWarning: executable_path has been deprecated\\\":  此错误不耽误程序运行,如果程序因为此错误不能运行,修改代码为: 3.报错\\\"TypeError: \\\'module\\\'

    2024年02月12日
    浏览(32)
  • 微信小程序常见的报错问题:TypeError: Cannot read property ‘forceUpdate‘ of undefined

    微信小程序遇到 Cannot read property \\\'forceUpdate\\\' of undefined是很常见的问题 这是由于 没有为项目配置 AppID。所以解决我们只需要为其配置AppID即可 登录微信开发者文档,在指南的下面选择申请账号菜单 开始 | 微信开放文档    (1)如果使用的是微信开发者工具软件 在该软件的右

    2024年02月12日
    浏览(43)
  • 记录:ET6 框架,由于 vs2019 不支持 .NET 6,在[生成解决方案]“Client-Server.sln“解决方案时会发生的报错

    ET 自己的论坛 ET社区 中的帖子内容,百度好像不能直接找到结果。记录一下,便于搜索。 ET6 依赖.NET 6 ,IDE应当选择:VS 2022 或 Rider2021.3.x(对应支持.NET 6 的版本,可以从 Rider官方文章得到相关信息) VS 2019、Rider2021.1.x 均不支持.NET 6,因此都无法正常使用。 VS 2019 只会在编译

    2024年02月12日
    浏览(37)
  • PHP-FPM与Nginx通信报 502 Bad Gateway或504 Gateway Timeout终极解决方案(适用于PHP执行耗时任务情况下的报错)

    适用于常规请求都没问题,但是执行某些php脚本需要超过一分钟的情况下的502/504,并不是任何请求都502/504的情况(这说明php-fpm或者nginx配置错误)。 执行脚本时间太长,期间php没有返回任何的数据。 php-fpm超时,nginx没超时 。nginx认为php-fpm罢工了,然后抛出了异常。 执行脚

    2024年02月08日
    浏览(43)
  • python初学必读常见报错和警告及其解决方案(篇一)

    目录 一、AssertionError 二、AttributeError 三、DeprecationWarning 四、FileExistsError 五、FileNotFoundError 一、AssertionError AssertionError是程序里写的assert抛出的错误 范例: 输入输出: 解决方案: 1.删除assert语句: 2.更改语法 二、AttributeError AttributeError是当程序调用了类或库中没用的变量或方

    2024年02月09日
    浏览(27)
  • Redis高可用解决方案之Redis集群,和Spring Cloud集成实战

    专栏集锦,大佬们可以收藏以备不时之需 Spring Cloud实战专栏:https://blog.csdn.net/superdangbo/category_9270827.html Python 实战专栏:https://blog.csdn.net/superdangbo/category_9271194.html Logback 详解专栏:https://blog.csdn.net/superdangbo/category_9271502.html tensorflow专栏:https://blog.csdn.net/superdangbo/category_869

    2024年02月05日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包