日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

rabbitmq最大连接数(Socket Descriptors)

發(fā)布時間:2025/4/9 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 rabbitmq最大连接数(Socket Descriptors) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

RabbitMQ自帶了顯示能夠接受的最大連接數(shù),有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可用連接數(shù)耗盡時客戶端的影響以及如何增加最大連接數(shù)默認值。

RabbitMQ的socket連接數(shù)(socket descriptors)是文件描述符(file descriptors,fd)的一個子集。也就是說,RabbitMQ能同時打開的最大連接數(shù)和最大文件句柄數(shù)(文件系統(tǒng),管道)都是受限于操作系統(tǒng)關于文件描述符數(shù)量的設置,兩者是此消彼長的關系。初始時,可用socket描述符與可用fd數(shù)量的比率大概在0.8-0.9左右,這個值并不固定,當socket描述符有剩余時,RabbitMQ會使用盡量多的文件描述符用于磁盤文件讀寫。隨著服務器建立越來越多的socket連接,文件句柄開始回收,數(shù)量減少。總之,RabbitMQ會優(yōu)先將文件描述符用于建立socket連接,寧可犧牲頻繁打開/關閉文件帶來的磁盤操作性能損耗,這種取舍很容易理解,作為網絡服務器,當然優(yōu)先保障網絡吞吐率了。因此,對于高并發(fā)連接數(shù)的多隊列讀寫時,隊列性能會稍微差那么一點,比如用RabbitMQ做RPC。

當服務器建立的socket連接已經達到限制(sockets_limit)時,服務器不再接受新連接。這里要區(qū)分清楚,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開始客戶端向服務器端發(fā)送AMQP協(xié)議頭字符串“AMQP0091”,共8個字節(jié),開始AMQP握手。line 6是服務器回給客戶端的ack包,但未發(fā)送AMQP connection.start方法,導致客戶端一直等到超時(line 7-8),發(fā)送FIN包關閉TCP連接。至此,AMQP連接建立失敗。

從客戶端(Java SDK)來看上述這個過程,客戶端通過ConnectionFactory實例的newConnection()方法創(chuàng)建一條AMQP連接。在網絡層,它首先通過java.net.Socket與服務器建立一條TCP連接,發(fā)送協(xié)議協(xié)商字符串“AMQP0091”,然后啟動MainLoop線程,通過封裝的Frame實例來循環(huán)讀取幀(readFrame()),注意readFrame()方法可能會有一個SocketTimeoutException的超時異常,這個超時時間是由socket實例setSoTimeout方法寫入,默認是10s,由AMQConnection.HANDSHAKE_TIMEOUT常量指定。當超時發(fā)生在AMQP連接握手階段時,就拋出一個SocketTimeoutException異常,發(fā)生在其他階段(除心跳超時)時,什么都不做繼續(xù)下一個循環(huán):

1 2 3 4 [java]?view plaincopy
  • 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??
  • ?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...??
  • ?<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>??
  • ?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參數(shù),指的是完成TCP三次握手的超時時間;而讀取超時是從socket中讀取字節(jié)流的等待時間,前文已經說過,由Socket實例的setSoTimeout()指定。這在各種網絡庫中應該很常見,比如HttpClient。

    私以為這種情況下更好的設計是應該由RabbitMQ主動斷開與客戶端的TCP連接,減少客戶端等待時間。

    最后一個問題,如何增加RabbitMQ的能夠同時打開的連接數(shù)。通過前文可知,最大并發(fā)連接數(shù)由此進程可打開的最大文件描述符數(shù)量(乘以一個比例系數(shù))決定,因此只要增加單個進程可打開的文件描述符數(shù)量即可。有幾個常規(guī)方法,按作用范圍可以歸納為以下幾類:

    1. 進程級別。在啟動腳本rabbitmq-server中加入ulimit -n 10240命令(假設將最大文件描述符數(shù)量設置為10240,下同),相當于在shell中執(zhí)行,由此shell進程fork出來的進程都能繼承這個配置。

    2. 用戶級別。修改/etc/security/limits.conf文件,添加以下配置,重新登錄生效:

    1 2 user soft nofile 10240 user hard nofile 10240

    3. 系統(tǒng)級別。

    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文件系統(tǒng),相當于修改了操作系統(tǒng)的運行時參數(shù),重啟后失效。要想永久生效,需要修改/etc/sysctl.conf文件,加入配置項fs.file-max=10240。

    一個進程能打開的最大文件描述符數(shù)量受限于上述三個級別配置中的最小值。理論上,系統(tǒng)級別的配置數(shù)值必須要大于用戶級別,用戶級別的要大于進程級別的,只有這樣配置才是安全的,否則進程容易因為打開文件數(shù)量問題受到來自操作系統(tǒng)的種種限制。操作系統(tǒng)為什么要限制可打開的文件描述符數(shù)量?為了系統(tǒng)安全。因為文件描述符本質上是一種內存中的數(shù)據結構,如果不加以限制,很容易被進程無意或惡意耗盡內存,比如fork bomb。

    轉載于:https://www.cnblogs.com/405845829qq/p/6490343.html

    總結

    以上是生活随笔為你收集整理的rabbitmq最大连接数(Socket Descriptors)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。