前导  

  上次Redis MQ分布式改造完成之后, 编排的容器稳定运行了一个多月,昨天突然收到ETL端同事通知,没有采集到解析日志了。

赶紧进服务器看了一下,用于数据接收的receiver容器挂掉了, 尝试docker container start [containerid],  几分钟后该容器再次崩溃。

Redis连接超限

docker log [containerid]  查看容器日志; 重点:CSRedis.RedisException: ERR max number of clients reached

日志上显示连接Redis服务器的客户端数量超限,头脑快速思考,目前编排的某容器使用CSRedisCore 对于16个Redis DB实例化了16个客户端,但Redis服务器也不至于这么不经折腾吧。

赶紧进redis.io官网搜集相关资料。

After the client is initialized, Redis checks if we are already at the limit of the number of clients that it is possible to handle simultaneously (this is configured using the maxclients configuration directive, see the next section of this document for further information).

In case it can't accept the current client because the maximum number of clients was already accepted, Redis tries to send an error to the client in order to make it aware of this condition, and closes the connection immediately. The error message will be able to reach the client even if the connection is closed immediately by Redis because the new socket output buffer is usually big enough to contain the error, so the kernel will handle the transmission of the error.

大致意思是:Redis服务器maxclients配置了客户端连接数, 如果当前连接的客户端超限,Redis会回发一个错误消息给客户端,并迅速关闭客户端连接。

立刻登录Redis服务器查看默认配置,确认当前Redis服务器默认配置是10000。

After the client is initialized, Redis checks if we are already at the limit of the number of clients that it is possible to handle simultaneously (this is configured using the maxclients configuration directive, see the next section of this document for further information).

In case it can't accept the current client because the maximum number of clients was already accepted, Redis tries to send an error to the client in order to make it aware of this condition, and closes the connection immediately. The error message will be able to reach the client even if the connection is closed immediately by Redis because the new socket output buffer is usually big enough to contain the error, so the kernel will handle the transmission of the error.

 左图表明:通过Redis-Cli 登录进服务器立即就被踢下线。

基本可认定redis客户端使用方式有问题。

CSRedisCore使用方式

 继续查看相关资料,可在redis服务器上利用redis-cli命令:info clients、client list仔细分析客户端连接。

info clients 命令显示现场确实有10000的连接数;

client list命令显示连接如下

官方对client list命令输出字段的解释:

  • addr: The client address, that is, the client IP and the remote port number it used to connect with the Redis server.

  • fd: The client socket file descriptor number.

  • name: The client name as set by CLIENT SETNAME.

  • age: The number of seconds the connection existed for.

  • idle: The number of seconds the connection is idle.

  • flags: The kind of client (N means normal client, check the full list of flags).

  • omem: The amount of memory used by the client for the output buffer.

  • cmd: The last executed command.

根据以上解释,表明 Redis服务器收到很多ip=172.16.1.3(故障容器在网桥内的Ip 地址)的客户端连接,这些连接最后发出的是ping命令(这是一个测试命令)

故障容器使用的Redis客户端是CSRedisCore,该客户端只是单纯将 Msg 写入Redis list 数据结构,CSRedisCore上相关github issue给了我一些启发。

发现自己将CSRedisClient实例化代码写在 .netcore api Controller构造函数,这样每次请求构造Controller时都 实例化一次Redis客户端,最终Redis客户端不超限才怪。

依赖注入三种模式: 单例(系统内单一实例,一次性注入);瞬态(每次请求产生实例并注入);自定义范围。

有关dotnet apicontroller 以瞬态模式 注入,请查阅链接。

 赶紧将CSRedisCore实例化代码移到 startup.cs 并注册为单例。

大胆求证

info clients命令显示稳定在53个Redis连接。

client list命令显示:172.16.1.3(故障容器)建立了50个客户端连接,编排的另一个容器webapp建立了2个连接,redis-cli命令登录到服务器建立了1个连接。

那么问题来了,修改之后,receiver容器为什么还稳定建立了50个redis连接?

进一步与CSRedisCore原作者沟通,确定CSRedisCore有预热机制,默认在连接池中预热了50个连接。

bingo,故障和困惑全部排查清楚。

总结

经此一役,在使用CSRedisCore客户端时, 要深入理解

① Stackexchange.Redis 使用的多路复用连接机制(使用时很容易想到注册到单例),CSRedisCore开源库采用连接池机制,在高并发场景下强烈建议注册为单例, 否则在生产使用中可能会误用在瞬态请求中实例化,导致redis客户端几天之后被占满。

② CSRedisCore会默认建立连接池,预热50个连接, 开发者心里要有数。

额外的方法论: 尽量不要从某度找答案,要学会问问题,并尝试从官方、stackoverflow 、github社区寻求解答,你挖过的坑也许别人早就挖过并踏平过。

原文链接:https://www.cnblogs.com/JulianHuang/p/11541658.html


.NET社区新闻,深度好文,欢迎访问公众号文章汇总 http://www.csharpkit.com

误用.Net Redis客户端工具CSRedisCore,自己挖坑自己填相关推荐

  1. [ 搭建Redis本地服务器实践系列三 ] :图解Redis客户端工具连接Redis服务器

    原文:[ 搭建Redis本地服务器实践系列三 ] :图解Redis客户端工具连接Redis服务器 上一章 [ 搭建Redis本地服务器实践系列二 ] :图解CentOS7配置Redis  介绍了Red ...

  2. redis 用中文做key_推荐一款Redis客户端工具

    日常开发过程中,项目常常都会使用Redis来做缓存或者Session服务器,为了更直观方便,开发者常常会使用一些可视化工具,如 Redis Desktop Manager.Redis Clent等,但 ...

  3. 推荐一款好用的redis客户端工具

    为什么80%的码农都做不了架构师?>>> 推荐一款好用的redis客户端工具 redis官方客户端(redis-cli)每次都要指定连接IP与端口,连接成功后还要执行auth命令进行 ...

  4. idea命令行运行多个客户端_推荐一款神仙颜值的 Redis 客户端工具,开源啦

    日常开发过程中,项目常常都会使用Redis来做缓存或者Session服务器,为了更直观方便,开发者常常会使用一些可视化工具,如 Redis Desktop Manager.Redis Clent等,但 ...

  5. redis 客户端_你在使用什么 Redis 客户端工具?

    今天发现一个不错的 Redis 客户端工具:AnotherRedisDesktopManager. 兼容 Windows Mac Linux,号称又快又稳定,加载大量 keys 时也不会崩溃. Git ...

  6. redis客户端mac_推荐一款神仙颜值的Redis客户端工具

    日常开发过程中,项目常常都会使用Redis来做缓存或者Session服务器,为了更直观方便,开发者常常会使用一些可视化工具,如 Redis Desktop Manager.Redis Clent等,但 ...

  7. Github标星 8K+,免费又好用的Redis客户端工具!

    最近在寻找一款免费又好用的Redis客户端工具,于是找到了AnotherRedisDesktopManager,界面漂亮而且支持Redis集群,推荐给大家! RedisDesktopManager 以 ...

  8. Github标星超级牛,免费又好用的Redis客户端工具!

    RedisDesktopManager 以前一直使用的是RedisDesktopManager这款Redis客户端工具,由于很久 这个工具大家相对不陌生了,也很多人都使用过了!RedisLettuce ...

  9. 找不到合适好用的redis客户端工具?试试官方的客户端工具RedisInsight

    这里是weihubeats,觉得文章不错可以关注公众号小奏技术,文章首发.拒绝营销号,拒绝标题党 背景 之前使用的redis客户端工具是AnotherRedisDesktopManager Anoth ...

最新文章

  1. Android源代码下载与编译 - 2019
  2. linux sshd服务是什么意思,Linux中sshd命令起什么作用呢?
  3. Python3使用md5
  4. CSS 实现加载动画之四-圆点旋转
  5. RabbitMQ消息队列简单异步邮件发送和订单异步处理实战【应用解耦】【异步削峰】
  6. 作为一名程序员,他们工作时的快乐来源于哪里?不仅仅是高工资!
  7. 米斯特白帽培训讲义(v2)信息收集
  8. 模态对话框与非模态对话框的区别
  9. Eclipse或者MyEclipse—在Eclipse或MyEclipse中的操作(2)
  10. python-封装继承多态
  11. 电子系统综合设计作业笔记
  12. CSS设置背景和渐变色
  13. 杭州是个技术乐观派的城市
  14. python模拟登录淘宝直通车_淘宝直通车推广计划
  15. 解决安装mysql“staring the server“问题
  16. 方舟破解版自建服务器,方舟生存进化自建服务器教程
  17. PDF文件如何快速转换成Word文件?两个方法教你搞定
  18. 数据挖掘的10个常见问题
  19. 从零开始编写minecraft光影包(8)中级水面绘制 水下阴影与焦散
  20. 基于卷积神经网络的猫狗识别系统的设计与实现

热门文章

  1. Mac OS使用技巧十七:丰富多彩的花哨输入法
  2. 什么是Microsoft Teams的App Studio
  3. windows10加载动画_如何关闭动画并使Windows 10看起来更快
  4. 用window.location.href实现页面跳转
  5. 《Apache Kafka实战》读书笔记-调优Kafka集群
  6. vue实现首屏加载等待动画 避免首次加载白屏尴尬
  7. 计算机与操作系统基础小结
  8. 存储世界瞬息万变 SSD掀行业浪潮
  9. Java基础- super 和 this 解析
  10. EFDC水模型 初学者入门 及软件下载学习指导