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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

RabbitMQ:The channelMax limit is reached. Try later.

發布時間:2024/3/12 编程问答 53 豆豆
生活随笔 收集整理的這篇文章主要介紹了 RabbitMQ:The channelMax limit is reached. Try later. 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

RabbitMQ:The channelMax limit is reached. Try later.

? 這個問題是我當初寫項目時遇到的,因為用RabbitMQ做削峰處理,高并發情況下,channel數到達了限制,所以不能繼續創建,相信大家也遇到過。

? 正常來說,這個錯誤還是比較少見的,只不過項目需要保證消息的可靠性,所以采取了發送確認和消費手動確認機制,導致并發性能下降,從而出現這個問題。、

? 這里先上結論,方便著急的小伙伴們改bug。

? 結論:RabbitMQ java客戶端在創建連接時,會向服務端發送一個請求,這個請求會獲取到服務端的channelMax值,java客戶端會自己進行一個處理,兩者都不為0時,會選擇一個小的值,如果你沒有在rabbitmq.conf文件中修改channel_Max的值,那么java客戶端會采用默認的2047或更小,這就會導致你明明在客戶端連接上配置了channelMax(比如你配置了4095),但依舊會報錯,而且web管理頁面最大值依舊是2047

第一次修改配置不生效

出現這種情況經常伴隨著消息丟失,而且消息丟失情況非常嚴重,達到了百分之二十的丟失率,這個丟失率也會因為并發量、每次消費數量等等配置的不同而變化。

由于項目是基于SpringBoot2.2的,yml暫時無法配置RequestChannelMax的值,這里只能采用直接通過set的方式放入值。

@Configuration @Slf4j public class RabbitMQConfig {@Beanpublic RabbitTemplate rabbitTemplate(final ConnectionFactory connectionFactory) {CachingConnectionFactory cachingConnectionFactory = (CachingConnectionFactory) connectionFactory;//這里我明明設置了4095,但是項目運行之后,壓測之后,還是會報異常,而且報異常的時候,RabbitMQ //web管理頁面上的channel數依舊是2047,不得已只能分析源碼了cachingConnectionFactory.getRabbitConnectionFactory().setRequestedChannelMax(4095);final RabbitTemplate rabbitTemplate = new RabbitTemplate(cachingConnectionFactory);rabbitTemplate.setMessageConverter(jackson2MessageConverter());rabbitTemplate.setMandatory(true);rabbitTemplate.setConfirmCallback((correlationData, b, s) -> {if(!b){log.error("confirmCallBack 發送失敗的數據:{}",correlationData);log.error("confirmCallBack 確認情況:{}",b);log.error("confirmCallBack 發送失敗的原因:{}",s);}});rabbitTemplate.setReturnCallback((message, i, s, s1, s2) -> {log.error("returnCallBack 消息:{}",message);log.error("returnCallBack 回應碼:{}",i);log.error("returnCallBack 回應信息:{}",s);log.error("returnCallBack 交換機:{}",s1);log.error("returnCallBack 路由鍵:{}",s2);});return rabbitTemplate;}@Beanpublic Jackson2JsonMessageConverter jackson2MessageConverter() {return new Jackson2JsonMessageConverter();} }

分析源碼

首先是模擬出報錯的場景,然后進入報異常的類。

發現是this.delegate.createChannel();方法返回的是一個空channel對象,進入這個方法看一下。

發現有一個ChannelManage對象,顧名思義,就是一個channel管理器,由它負責創建channel,那么看一下這個對象都有什么值呢?

public Channel createChannel() throws IOException {this.ensureIsOpen();ChannelManager cm = this._channelManager;if (cm == null) {return null;} else {Channel channel = cm.createChannel(this);this.metricsCollector.newChannel(channel);return channel;}}

只截取了部分代碼,首先可以看到有一個int類型的channelMax,這個值就是channel的最大值,還有一個構造器,很明顯,這個值是通過構造器傳進來的,通過容器初始化時打斷點進行跟蹤,發現此時的channelMax依舊是2047,這也進一步證明了,值的覆蓋或者處理發生在這個類調用之前。

public class ChannelManager {private static final Logger LOGGER = LoggerFactory.getLogger(ChannelManager.class);private final Object monitor;private final Map<Integer, ChannelN> _channelMap;private final IntAllocator channelNumberAllocator;private final ConsumerWorkService workService;private final Set<CountDownLatch> shutdownSet;private final int _channelMax;private ExecutorService shutdownExecutor;private final ThreadFactory threadFactory;private int channelShutdownTimeout;protected final MetricsCollector metricsCollector;public ChannelManager(ConsumerWorkService workService, int channelMax, ThreadFactory threadFactory, MetricsCollector metricsCollector) {this.monitor = new Object();this._channelMap = new HashMap();this.shutdownSet = new HashSet();this.channelShutdownTimeout = 63000;if (channelMax == 0) {channelMax = 65535;}this._channelMax = channelMax;this.channelNumberAllocator = new IntAllocator(1, channelMax);this.workService = workService;this.threadFactory = threadFactory;this.metricsCollector = metricsCollector;} }

進一步跟蹤之后,發現在AMQConnection類里的instantiateChannelManager()方法調用了構造器,繼續往上追蹤。

protected ChannelManager instantiateChannelManager(int channelMax, ThreadFactory threadFactory) {ChannelManager result = new ChannelManager(this._workService, channelMax, threadFactory, this.metricsCollector);this.configureChannelManager(result);return result;}

在AMQConnetion類的start()方法中最終發現了值改變的地方。

this.requestedChannelMax值是我在配置類中配置的4095

connTune.getChannelMax()是2047

也就是說,negotiateChannelMax()方法對這兩個值進行了處理,最終選擇了2047

int channelMax = this.negotiateChannelMax(this.requestedChannelMax, connTune.getChannelMax());this._channelManager = this.instantiateChannelManager(channelMax, this.threadFactory);

最終發現這么一段處理邏輯,如果兩個數字都不為0,那么就取最小的,反之取最大的,看到這里是明白做了什么處理,但是還是有一處不明白,2047的值究竟從何處來的?

protected int negotiateChannelMax(int requestedChannelMax, int serverMax) {return negotiatedMaxValue(requestedChannelMax, serverMax);}private static int negotiatedMaxValue(int clientValue, int serverValue) {return clientValue != 0 && serverValue != 0 ? Math.min(clientValue, serverValue) : Math.max(clientValue, serverValue);}

其實重點是connTune.getChannelMax()這個方法

int channelMax = this.negotiateChannelMax(this.requestedChannelMax, connTune.getChannelMax());

通過對connTune的追尋,發現了這段處理,debug也證明了確實在這里獲取的2047這個值,

其實不管從方法名rpc()還是變量名serverResponse來看,這個都是做了一個請求,那么向誰請求其實很顯而易見了,這里向RabbitMQ端做了一個請求,用來索取MQ端的channelMax、frameMax、heartBeat值等等

Tune connTune = null;try {......try {Method serverResponse = this._channel0.rpc((Method)method, this.handshakeTimeout / 2).getMethod();if (serverResponse instanceof Tune) {connTune = (Tune)serverResponse;}......

到現在其實就很明確了,我們只在客戶端修改邊界值配置是無效的,必須同步修改MQ服務端的配置,也就是rabbitmq.conf文件

## Set the max permissible number of channels per connection. ## 0 means "no limit". ##在配置文件中,輸入以下參數和自己想要設置的值即可,如果用不到2047,那就不用配置 # channel_max = 128

其實問題并不大,主要還是不了解MQ的一個客戶端連接過程,導致耗費了大量時間。

這里還是推薦大家,先用百度搜索,第一頁看不到正確解決方案,那就去StackOverflow網站,還不行的話,那就使用終極大法,要么官網逐行看文檔,要么走一波源碼,也是鍛煉自己解決問題的思路和能力。

總結

以上是生活随笔為你收集整理的RabbitMQ:The channelMax limit is reached. Try later.的全部內容,希望文章能夠幫你解決所遇到的問題。

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