RabbitMQ

Stella981
• 阅读 528

rabbitmq-web-stomp 优化过程

基础环境

  • OS Centos 7 x86_64
  • Erlang 19.0.3
  • Rabbitmq 3.6.5

症状

  1. OA系统显示有2200个人在线,但是Rabbitmq的连接数一直小于1300

  2. IE8下会频繁出现连接错误的异常,重启rabbitmq后能够正常运行30分钟左右,问题重复出现

参考代码:

   var stompClient=null;
   function stompConnect(){
           var ws=new SockJS('http://localhost:15674/stomp');
           stompClient=Stomp.over(ws);
           stompClient.connect('guest','guest',on_connected,on_error,'test');
           
   }
   var on_connected=function(result){
           if(stompClient!=null){
               stompClient.subscribe(pattern,function(body){
                   //todo 
               })
           }
   }
   var on_error=function(x){
           setTimeout(stompConnect,10000);
   }
   stompConnect();

当用户少的时候可能正常运行到on_connected,随着用户的增加部分用户会丢失与rabbitmq的连接,频繁的执行on_error方法

优化过程

  • 查看系统的并发数量

    root@alice:~ # netstat -an | awk '/^tcp/ { ++S[$NF]} END { for (a in S) print a, S[a]}' LISTEN 19 ESTABLISHED 1674 FIN_WAIT1 1 FIN_WAIT2 537 TIME_WAIT 5295

并发数比较符合预期,OA系统的在线用户数在2200左右

  • 查看15674端口打开的文件数数量

与15674端口建立连接的数量,OA系统的用户是通过web_stomp与rabbitmq进行交互

root@alice:~# lsof -i:5674|grep 'TCP'|wc -l   
1034 

经过多次执行 lsof -i:5674|wc -l 发现这个数字永远小于等于1034OA系统2200在线用户的规模应该有可能会超过这个数值才对(猜测)

  • 查看rabbitmq的错误日志

    root@alice:~# tail /data/rabbitmq_server-3.6.5/var/log/rabbitmq/rabbit@alice.log -n 1000|grep 'ERROR' -B 2 -A 2 Error in process <0.12547.685> on node 'rabbit@alice' with exit value: {[{reason,{badmatch,{error,timeout}}},{mfa,{sockjs_cowboy_handler,handle,2}},{stacktrace,[{sockjs_http,body,1,[{file,"src/sockjs_http.erl"},{line,36}]},{sockjs_action,xhr_send,4,[{file,"src/sockjs_action.erl"},{line,144}]},{sockjs_handler...

官方文档描述了有关的web stomp和Ranch的配置,怀疑是不是因为Ranch的参数配置导致15674端口使用最大只能有1034个连接

RabbitMQ

Ranch配置有关的配置项似乎没有与连接数有关的配置。

Ranch 1.3 User Guide关于concurrent connections描述了最大连接数的限制

The max_connections transport option allows you to limit the number of concurrent connections.
 It defaults to 1024. Its purpose is to prevent your system from being overloaded and 
 ensuring all the connections are handled optimally.
Customizing the maximum number of concurrent connections

{ok, _} = ranch:start_listener(tcp_echo, 100,
        ranch_tcp, [{port, 5555}, {max_connections, 100}],
        echo_protocol, []
).

You can disable this limit by setting its value to the atom infinity.
Disabling the limit for the number of connections

{ok, _} = ranch:start_listener(tcp_echo, 100,
        ranch_tcp, [{port, 5555}, {max_connections, infinity}],
        echo_protocol, []
).

The maximum number of connections is a soft limit. In practice, 
it can reach max_connections + the number of acceptors.

When the maximum number of connections is reached, Ranch will stop accepting connections.
 This will not result in further connections being rejected, as the kernel option allows queueing incoming connections. 
 The size of this queue is determined by the backlog option and defaults to 1024. Ranch does not know about the number of 
 connections that are in the backlog.

文中描述了max_connections是一个软的限制,实际限制是max_connections+acceptors的数量,acceptors默认是10所以通过

lsof -i:15674|grep TCP|wc -l 

获取的值是1034

1034=1024(max_connections)+10(acceptors)

至此,可以通过设定max_connections来增加rabbitmq_web_stomp的并发限制

[
    {rabbit,[
        {hipe_compile,true},
        {dis_free_limit,"5GB"}
        ]
    },
    {rabbitmq_web_stomp,[
        {tcp_config,[
            {backlog,3000},
            {max_connections,5000}

        ]}
    ]
    }
].

将最大连接数增加到5000,比较遗憾的是Ranch配置选项中没有明确指出max_connectioins的配置,导致走了不少弯路

优化结果

修改配置重启RabbitMQ服务后,不断监控日志的情况及15674端口的连接数量,比较正常

lsof -i:15674|grep TCP|wc -l 
1683


root@alice:~# tail /data/rabbitmq_server-3.6.5/var/log/rabbitmq/rabbit@alice.log -n 1000|grep 'ERROR' -B 2 -A 2

无错误信息

总结

  • 新系统上线对负载预估不足
  • 对RabbitMQ熟悉程度有待加强
点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
待兔 待兔
2星期前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Souleigh ✨ Souleigh ✨
3年前
前端性能优化 - 雅虎军规
无论是在工作中,还是在面试中,web前端性能的优化都是很重要的,那么我们进行优化需要从哪些方面入手呢?可以遵循雅虎的前端优化35条军规,这样对于优化有一个比较清晰的方向.35条军规1.尽量减少HTTP请求个数——须权衡2.使用CDN(内容分发网络)3.为文件头指定Expires或CacheControl,使内容具有缓存性。4.避免空的
Stella981 Stella981
2年前
Spring Cloud 系列之 Spring Cloud Stream
SpringCloudStream是消息中间件组件,它集成了kafka和rabbitmq。本篇文章以RabbitMQ为消息中间件系统为基础,介绍SpringCloudStream的使用。如果你没有用过消息中间件,可以到RabbitMQ的官网看一下,或者参考这个http://rabbitmq.mrping.com/。理
Stella981 Stella981
2年前
Android So动态加载 优雅实现与原理分析
背景:漫品Android客户端集成适配转换功能(基于目标识别(So库35M)和人脸识别库(5M)),导致apk体积50M左右,为优化客户端体验,决定实现So文件动态加载.!(https://oscimg.oschina.net/oscnet/00d1ff90e4b34869664fef59e3ec3fdd20b.png)点击上方“蓝字”关注我
Easter79 Easter79
2年前
Twitter的分布式自增ID算法snowflake (Java版)
概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移
Wesley13 Wesley13
2年前
35岁,真的是程序员的一道坎吗?
“程序员35岁是道坎”,“程序员35岁被裁”……这些话咱们可能都听腻了,但每当触及还是会感到丝丝焦虑,毕竟每个人都会到35岁。而国内互联网环境确实对35岁以上的程序员不太友好:薪资要得高,却不如年轻人加班猛;虽说经验丰富,但大部分公司并不需要太资深的程序员。但35岁危机并不是不可避免的,比如你可以不断精进技术,将来做技术管理或者
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Stella981 Stella981
2年前
Rabbitmq学习之路1
最近项目中开始使用rabbitmq,以前只听过但是没使用过,所以有必要先系统的了解一下rabbitmq的使用方法。找管理员要了三台机器做学习之用。试验环境10.20.112.26 ubuntu12.04 ubuntuTest0110.20.112.27 ubuntu12.04ubuntuTest0210.20.112.28 
Wesley13 Wesley13
2年前
oracle小数点前零丢失的问题
1.问题起源 oracle数据库字段值为小于1的小数时,使用char类型处理,会丢失小数点前面的0 例如0.35就变成了.35 2.解决办法:(1)用to\_char函数格式化数字显示 select    to\_char(0.338,'fm9999999990.00')fromdual; 结果:0.34