-
RabbitMQ 실패 재시도하기 (NACK 처리 php code sample)개발/etc 2019. 10. 2. 16:00
1. Queue 생성
queue-delivery
- 프로퍼티
- x-dead-letter-exchange : test.exchanger
- x-dead-letter-routing-key : delivery.failed
- UI
queue-delivery.retriable
1. 프로퍼티
- x-dead-letter-exchange : test.exchanger
- x-dead-letter-routing-key : delivery
- x-message-ttl: 3000
2. UI
2. exchanger 생성
3. excahnger bind queue
4. CODE
<?php namespace Test\Consume; use PhpAmqpLib\Message\AMQPMessage; class DeliveryConsume extends RabbitMQ { public function execute() : void { $callback = function (AMQPMessage $msg) { $nativeData = $msg->get('application_headers')->getNativeData(); if (isset($nativeData['x-death']) && $nativeData['x-death'][0]['count'] > 3) { echo 'ACK 처리하지만 수동으로 해당 액션처리해줘야함.'; $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']); } if ($this->tenPercentChanceOfTrue()) { echo 'ACK'.PHP_EOL; $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']); exit(); } echo 'NACK'.PHP_EOL; $msg->delivery_info['channel']->basic_nack($msg->delivery_info['delivery_tag']); }; try { $this->connect(); $this->channel->basic_qos(0, 1, false); $this->channel->basic_consume('queue-delivery', '', false, false, false, false, $callback); while (count($this->channel->callbacks)) { $this->channel->wait(); } } catch (\Exception $e) { echo 'error'; } } private function tenPercentChanceOfTrue() { return rand(1, 10) === 1; } }
참고문서 : https://mk.kakaocdn.net/dn/if-kakao/conf2019/%EB%B0%9C%ED%91%9C%EC%9E%90%EB%A3%8C_2019/T02_S02.pdf
'개발 > etc' 카테고리의 다른 글
Docker-for-desktop 대체하기 (Vagrant 를 활용한 docker) (0) 2022.02.10 RabbitMQ 지연큐 만들기 (0) 2020.06.19 github 블로그를 jekyll 를 통해 관리해보자 (0) 2018.11.09 MongoDB 를 docker로 세팅해보자 (0) 2018.11.09 tcpdump 와 wireshark 를 이용해 디버깅 하기 (0) 2018.11.09 - 프로퍼티