RabbitMQ自带了显示能够接受的最大连接数,有2种比较直观的方式:
1. rabbitmqctl命令。

1
2
3
4
5
6
7
8
9
10
11
12
<span style="color: rgb(102, 102, 102);">n$ </span>rabbitmqctl status
Status of node <span style="color: rgb(255, 0, 0);">'rabbit@10-101-17-13'</span> ...
<span style="color: rgb(122, 8, 116); "><strong>[</strong></span><span style="color: rgb(122, 8, 116); "><strong>{</strong></span>pid,23658<span style="color: rgb(122, 8, 116); "><strong>}</strong></span>,
......
<span style="color: rgb(122, 8, 116); "><strong>{</strong></span>file_descriptors,
<span style="color: rgb(122, 8, 116); "><strong>[</strong></span><span style="color: rgb(122, 8, 116); "><strong>{</strong></span>total_limit,924<span style="color: rgb(122, 8, 116); "><strong>}</strong></span>,
<span style="color: rgb(122, 8, 116); "><strong>{</strong></span>total_used,10<span style="color: rgb(122, 8, 116); "><strong>}</strong></span>,
<span style="color: rgb(122, 8, 116); "><strong>{</strong></span>sockets_limit,829<span style="color: rgb(122, 8, 116); "><strong>}</strong></span>,
<span style="color: rgb(122, 8, 116); "><strong>{</strong></span>sockets_used,10<span style="color: rgb(122, 8, 116); "><strong>}</strong></span><span style="color: rgb(122, 8, 116); "><strong>]</strong></span><span style="color: rgb(122, 8, 116); "><strong>}</strong></span>,
......
<span style="color: rgb(122, 8, 116); "><strong>]</strong></span>
...done.

2. rabbitmq_management WebUI插件。

本文关注当RabbitMQ可用连接数耗尽时客户端的影响以及如何增加最大连接数默认值。

RabbitMQ的socket连接数(socket descriptors)是文件描述符(file descriptors,fd)的一个子集。也就是说,RabbitMQ能同时打开的最大连接数和最大文件句柄数(文件系统,管道)都是受限于操作系统关于文件描述符数量的设置,两者是此消彼长的关系。初始时,可用socket描述符与可用fd数量的比率大概在0.8-0.9左右,这个值并不固定,当socket描述符有剩余时,RabbitMQ会使用尽量多的文件描述符用于磁盘文件读写。随着服务器建立越来越多的socket连接,文件句柄开始回收,数量减少。总之,RabbitMQ会优先将文件描述符用于建立socket连接,宁可牺牲频繁打开/关闭文件带来的磁盘操作性能损耗,这种取舍很容易理解,作为网络服务器,当然优先保障网络吞吐率了。因此,对于高并发连接数的多队列读写时,队列性能会稍微差那么一点,比如用RabbitMQ做RPC。

当服务器建立的socket连接已经达到限制(sockets_limit)时,服务器不再接受新连接。这里要区分清楚,RabbitMQ不再接收的是AMQP连接,而不是传输层的TCP连接,通过简单抓包分析即可清楚流程:

1
2
3
4
5
6
7
8
$ <span style="color: rgb(194, 12, 185); "><strong>sudo</strong></span> tcpdump host 10.101.17.13 and port 5672
17:24:12.214186 IP 10.101.17.166.56925 <span style="font-weight: bold;">></span> 10.101.17.13.amqp: Flags <span style="color: rgb(122, 8, 116); "><strong>[</strong></span>S<span style="color: rgb(122, 8, 116); "><strong>]</strong></span>, <span style="color: rgb(194, 12, 185); "><strong>seq</strong></span> 3319779561, win 65535, options <span style="color: rgb(122, 8, 116); "><strong>[</strong></span>mss 1368,<span style="color: rgb(194, 12, 185); "><strong>nop</strong></span>,wscale 5,<span style="color: rgb(194, 12, 185); "><strong>nop</strong></span>,<span style="color: rgb(194, 12, 185); "><strong>nop</strong></span>,TS val 1006381554 ecr 0,sackOK,eol<span style="color: rgb(122, 8, 116); "><strong>]</strong></span>, length 0
17:24:12.214231 IP 10.101.17.13.amqp <span style="font-weight: bold;">></span> 10.101.17.166.56925: Flags <span style="color: rgb(122, 8, 116); "><strong>[</strong></span>S.<span style="color: rgb(122, 8, 116); "><strong>]</strong></span>, <span style="color: rgb(194, 12, 185); "><strong>seq</strong></span> 1636058035, ack 3319779562, win 14480, options <span style="color: rgb(122, 8, 116); "><strong>[</strong></span>mss 1460,sackOK,TS val 24529834 ecr 1006381554,<span style="color: rgb(194, 12, 185); "><strong>nop</strong></span>,wscale 5<span style="color: rgb(122, 8, 116); "><strong>]</strong></span>, length 0
17:24:12.218795 IP 10.101.17.166.56925 <span style="font-weight: bold;">></span> 10.101.17.13.amqp: Flags <span style="color: rgb(122, 8, 116); "><strong>[</strong></span>.<span style="color: rgb(122, 8, 116); "><strong>]</strong></span>, ack 1, win 4110, options <span style="color: rgb(122, 8, 116); "><strong>[</strong></span><span style="color: rgb(194, 12, 185); "><strong>nop</strong></span>,<span style="color: rgb(194, 12, 185); "><strong>nop</strong></span>,TS val 1006381560 ecr 24529834<span style="color: rgb(122, 8, 116); "><strong>]</strong></span>, length 0
17:24:12.243184 IP 10.101.17.166.56925 <span style="font-weight: bold;">></span> 10.101.17.13.amqp: Flags <span style="color: rgb(122, 8, 116); "><strong>[</strong></span>P.<span style="color: rgb(122, 8, 116); "><strong>]</strong></span>, <span style="color: rgb(194, 12, 185); "><strong>seq</strong></span> 1:9, ack 1, win 4110, options <span style="color: rgb(122, 8, 116); "><strong>[</strong></span><span style="color: rgb(194, 12, 185); "><strong>nop</strong></span>,<span style="color: rgb(194, 12, 185); "><strong>nop</strong></span>,TS val 1006381583 ecr 24529834<span style="color: rgb(122, 8, 116); "><strong>]</strong></span>, length 8
17:24:12.243201 IP 10.101.17.13.amqp <span style="font-weight: bold;">></span> 10.101.17.166.56925: Flags <span style="color: rgb(122, 8, 116); "><strong>[</strong></span>.<span style="color: rgb(122, 8, 116); "><strong>]</strong></span>, ack 9, win 453, options <span style="color: rgb(122, 8, 116); "><strong>[</strong></span><span style="color: rgb(194, 12, 185); "><strong>nop</strong></span>,<span style="color: rgb(194, 12, 185); "><strong>nop</strong></span>,TS val 24529841 ecr 1006381583<span style="color: rgb(122, 8, 116); "><strong>]</strong></span>, length 0
17:24:22.247907 IP 10.101.17.166.56925 <span style="font-weight: bold;">></span> 10.101.17.13.amqp: Flags <span style="color: rgb(122, 8, 116); "><strong>[</strong></span>F.<span style="color: rgb(122, 8, 116); "><strong>]</strong></span>, <span style="color: rgb(194, 12, 185); "><strong>seq</strong></span> 9, ack 1, win 4110, options <span style="color: rgb(122, 8, 116); "><strong>[</strong></span><span style="color: rgb(194, 12, 185); "><strong>nop</strong></span>,<span style="color: rgb(194, 12, 185); "><strong>nop</strong></span>,TS val 1006391550 ecr 24529841<span style="color: rgb(122, 8, 116); "><strong>]</strong></span>, length 0
17:24:22.284914 IP 10.101.17.13.amqp <span style="font-weight: bold;">></span> 10.101.17.166.56925: Flags <span style="color: rgb(122, 8, 116); "><strong>[</strong></span>.<span style="color: rgb(122, 8, 116); "><strong>]</strong></span>, ack 10, win 453, options <span style="color: rgb(122, 8, 116); "><strong>[</strong></span><span style="color: rgb(194, 12, 185); "><strong>nop</strong></span>,<span style="color: rgb(194, 12, 185); "><strong>nop</strong></span>,TS val 24532352 ecr 1006391550<span style="color: rgb(122, 8, 116); "><strong>]</strong></span>, length 0

line 2-4是TCP握手包,成功建立TCP连接。line 5开始客户端向服务器端发送AMQP协议头字符串“AMQP0091”,共8个字节,开始AMQP握手。line 6是服务器回给客户端的ack包,但未发送AMQP connection.start方法,导致客户端一直等到超时(line 7-8),发送FIN包关闭TCP连接。至此,AMQP连接建立失败。

从客户端(Java SDK)来看上述这个过程,客户端通过ConnectionFactory实例的newConnection()方法创建一条AMQP连接。在网络层,它首先通过java.net.Socket与服务器建立一条TCP连接,发送协议协商字符串“AMQP0091”,然后启动MainLoop线程,通过封装的Frame实例来循环读取帧(readFrame()),注意readFrame()方法可能会有一个SocketTimeoutException的超时异常,这个超时时间是由socket实例setSoTimeout方法写入,默认是10s,由AMQConnection.HANDSHAKE_TIMEOUT常量指定。当超时发生在AMQP连接握手阶段时,就抛出一个SocketTimeoutException异常,发生在其他阶段(除心跳超时)时,什么都不做继续下一个循环:

1
2
3
4
  1. Caused by<span style="color:#339933">:</span> java.<span style="color:#006633">net</span>.<span style="color:#006633">SocketTimeoutException</span><span style="color:#339933">:</span> Timeout during <span style="color:#003399">Connection</span> negotiation
  2. at com.<span style="color:#006633">rabbitmq</span>.<span style="color:#006633">client</span>.<span style="color:#006633">impl</span>.<span style="color:#006633">AMQConnection</span>.<span style="color:#006633">handleSocketTimeout</span><span style="color:#009900">(</span>AMQConnection...
  3. <span style="color:#006633">at</span> com.<span style="color:#006633">rabbitmq</span>.<span style="color:#006633">client</span>.<span style="color:#006633">impl</span>.<span style="color:#006633">AMQConnection</span>.<span style="color:#006633">access</span>$500<span style="color:#009900">(</span>AMQConnection.<span style="color:#006633">java</span><span style="color:#339933">:</span><span style="color:#cc66cc">59</span><span style="color:#009900">)</span>
  4. at com.<span style="color:#006633">rabbitmq</span>.<span style="color:#006633">client</span>.<span style="color:#006633">impl</span>.<span style="color:#006633">AMQConnection</span>$MainLoop.<span style="color:#006633">run</span><span style="color:#009900">(</span>AMQConnection.<span style="color:#006633">java</span><span style="color:#339933">:</span><span style="color:#cc66cc">541</span><span style="color:#009900">)</span>

这里的socket读取超时很容易跟连接超时搞混。连接超时由ConnectionFactory实例的setConnectionTimeout()方法指定,对应着网络层Socket实例connect()方法中的timeout参数,指的是完成TCP三次握手的超时时间;而读取超时是从socket中读取字节流的等待时间,前文已经说过,由Socket实例的setSoTimeout()指定。这在各种网络库中应该很常见,比如HttpClient。

私以为这种情况下更好的设计是应该由RabbitMQ主动断开与客户端的TCP连接,减少客户端等待时间。

最后一个问题,如何增加RabbitMQ的能够同时打开的连接数。通过前文可知,最大并发连接数由此进程可打开的最大文件描述符数量(乘以一个比例系数)决定,因此只要增加单个进程可打开的文件描述符数量即可。有几个常规方法,按作用范围可以归纳为以下几类:

1. 进程级别。在启动脚本rabbitmq-server中加入ulimit -n 10240命令(假设将最大文件描述符数量设置为10240,下同),相当于在shell中执行,由此shell进程fork出来的进程都能继承这个配置。

2. 用户级别。修改/etc/security/limits.conf文件,添加以下配置,重新登录生效:

1
2
user    soft    nofile    10240
user hard nofile 10240

3. 系统级别。

1
<span style="color: rgb(102, 102, 102);"># </span><span style="color: rgb(122, 8, 116); "><strong>echo</strong></span> 10240 <span style="font-weight: bold;">></span> <span style="font-weight: bold;">/</span>proc<span style="font-weight: bold;">/</span>sys<span style="font-weight: bold;">/</span>fs<span style="font-weight: bold;">/</span>file-max

上述设置只是针对proc文件系统,相当于修改了操作系统的运行时参数,重启后失效。要想永久生效,需要修改/etc/sysctl.conf文件,加入配置项fs.file-max=10240。

一个进程能打开的最大文件描述符数量受限于上述三个级别配置中的最小值。理论上,系统级别的配置数值必须要大于用户级别,用户级别的要大于进程级别的,只有这样配置才是安全的,否则进程容易因为打开文件数量问题受到来自操作系统的种种限制。操作系统为什么要限制可打开的文件描述符数量?为了系统安全。因为文件描述符本质上是一种内存中的数据结构,如果不加以限制,很容易被进程无意或恶意耗尽内存,比如fork bomb

rabbitmq最大连接数(Socket Descriptors)的更多相关文章

  1. 初识RabbitMQ

    1.安装 rabbitmq官网:http://www.rabbitmq.com/ 下载地址:https://packagecloud.io/rabbitmq 下载rabbitmq-server 安装脚 ...

  2. RabbitMQ安装、集群搭建、概念解析

    RabbitMQ安装.集群搭建.概念解析 基本概念 为什么会产生MQ 1.解耦:采用异步方式实现业务需求达到解耦的目的. 2.缓冲流量,削峰填谷: 问:为什么会有流量冲击? 答:采用"直接调 ...

  3. 消息中间件——RabbitMQ(四)命令行与管控台的基本操作!

    前言 在前面的文章中我们介绍过RabbitMQ的搭建:RabbitMQ的安装过以及各大主流消息中间件的对比:,本章就主要来介绍下我们之前安装的管控台是如何使用以及如何通过命令行进行操作. 1. 命令行 ...

  4. RabbitMQ的管控台的使用说明

    1.RabbitMQ的管控台确实是一个好东西,但是如果是新手,比如刚接触RabbitMQ的时候,看到RabbitMQ的管控台也是一脸懵逼的说,慢慢接触多了,才了解一些使用. 1).RabbitMQ的管 ...

  5. RabbitMQ 入门 (Go) - 2. 发布和接收消息

    本文我将使用 Go 语言在 RabbitMQ 上发布和接收消息. Go 的标准库本身并没有 RabbitMQ 的原生绑定,但是有一个第三方库确能够支持 RabbitMQ,它的源码在 https://g ...

  6. Socket 多任务(多进程/线程、I/O多路复用、事件驱动开发框架)

    0. 概述 1. 循环版实现多连接 2. threading.Thread 多线程 3. SockerServer 实现多任务 3.1 ForkingMixIn - 多进程(限 linux) 3.2 ...

  7. Python Socket 编程——聊天室示例程序

    上一篇 我们学习了简单的 Python TCP Socket 编程,通过分别写服务端和客户端的代码了解基本的 Python Socket 编程模型.本文再通过一个例子来加强一下对 Socket 编程的 ...

  8. AMQP与RabbitMQ简介

    MQ(Message Queue,消息队列)是一种应用系统之间的通信方法.是通过读写出入队列的消息来通信(RPC则是通过直接调用彼此来通信的). 1.AMQP协议 在了解RabbitMQ之前,首先要了 ...

  9. winsock教程- windows下的socket编程(c语言实现)

    winsock教程- windows下的socket编程(c语言实现) 使用winsock进行socket 编程     这是一个学习windows下socket编程(c语言)的快速指南.这是因为一下 ...

随机推荐

  1. C++命名空间、标准库(std,全局命名空间)

    背景 别人遇到的问题: C++ 全局变量不明确与 using namespace std 冲突 我遇到的问题与他相似,函数调用冲突 using namespace std; class compare ...

  2. HNOI 2010 物品调度 并查集 置换

    题意: 题意有点细,暂不概括.请仔细审题. 分析: 我们先要把c生成出来. 记得颜神讲这道题,首先表明,这道题有两个问题需要处理. 第一个是要先定位,第二个是要求最小移动步数. 定位时对于每一个物品i ...

  3. 17Spring前置通知

    1).加入jar包:下载地址 spring-beans-4.1.6.RELEASE.jar commons-logging-1.1.3.jar spring-context-4.1.6.RELEASE ...

  4. DFS求连通块(漫水填充法)

    G - DFS(floodfill),推荐 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I6 ...

  5. 笔记——collections模块

    collections模块 collections模块在内置数据类型(dict.list.set.tuple)的基础上,还提供了几个额外的数据类型:ChainMap.Counter.deque.def ...

  6. api安全认证

    三.auth自定义授权 客户端代码: import requests import hashlib import time current_time = time.time() #自意义的字符串app ...

  7. Django开发:(3.1)ORM:单表操作

    MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人员的工作量,不需 ...

  8. node.js 写流 createWriteStream----由浅入深

    createWriteStream 写流 基于fs模块 let fs=require("fs"); createWriteStream创建一个写流 我们先创建一个2.txt要写入的 ...

  9. Git Cheat Sheet 中文版

    Git Cheat Sheet 中文版 索引 配置 配置文件 创建 本地修改 搜索 提交历史 分支与标签 更新与发布 合并与重置 撤销 Git Flow 配置 列出当前配置: $ git config ...

  10. Nginx 的 server_names_hash_bucket_size 问题

    在 Nginx 0.6.35 的版本中,配置多个 server 虚拟主机,必须要在配置文档中 http { 里头加上 server_names_hash_bucket_size 64; 这么一句 ht ...