rabbitmq死信队列(延迟队列)demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | <?php require_once './vendor/autoload.php' ; use PhpAmqpLib\Connection\AMQPStreamConnection; use PhpAmqpLib\Message\AMQPMessage; use PhpAmqpLib\Wire\AMQPTable; use PhpAmqpLib\Exchange\AMQPExchangeType; $connection = new AMQPStreamConnection( '127.0.0.1' , 5672, 'mq' , 'mq123' , '/' ); $channel = $connection ->channel(); $channel ->exchange_declare( 'exchange.dlx' , AMQPExchangeType::DIRECT, false, true); $channel ->exchange_declare( 'exchange.normal' , AMQPExchangeType::FANOUT, false, true); $args = new AMQPTable(); // 消息过期方式:设置 queue.normal 队列中的消息10s之后过期 $args ->set( 'x-message-ttl' , 10000); $args ->set( 'x-dead-letter-exchange' , 'exchange.dlx' ); $args ->set( 'x-dead-letter-routing-key' , 'routingkey' ); $channel ->queue_declare( 'queue.normal' , false, true, false, false, false, $args ); $channel ->queue_declare( 'queue.dlx' , false, true, false, false); $channel ->queue_bind( 'queue.normal' , 'exchange.normal' ); $channel ->queue_bind( 'queue.dlx' , 'exchange.dlx' , 'routingkey' ); $message = new AMQPMessage( '死信队列消息' ); $channel ->basic_publish( $message , 'exchange.normal' , 'rk' ); $channel ->close(); $connection ->close(); ———————————————— 原文链接:<a href= "https://blog.csdn.net/qq_42724459/article/details/109571547" target= "_blank" rel= "noopener nofollow" >https://blog.csdn.net/qq_42724459/article/details/109571547<br>生产</a>者与消费者代码如下: /** * 入消息队列 * * @param $queue string 队列名 * @param $data mixed 数据 */ public function pushMessageQueue( $data = null) { $host = config( "queue.connections.rabbitmq.host" ); $port = config( "queue.connections.rabbitmq.port" ); $login = config( "queue.connections.rabbitmq.login" ); $password = config( "queue.connections.rabbitmq.password" ); $vhost = config( "queue.connections.rabbitmq.vhost" ); $exchange = $this ->config[ "dds-zl-mq" ][ "exchange_name" ]; $queueName = $this ->config[ "dds-zl-mq" ][ "queue_name" ]; $routingKey = $this ->config[ "dds-zl-mq" ][ "router_key_name" ]; //路由关键字(也可以省略) //建立生产者与mq之间的连接 $connection = new AMQPStreamConnection( $host , $port , $login , $password , $vhost ); $channel = $connection ->channel(); //在已连接基础上建立生产者与mq之间的通道 $channel ->exchange_declare( $exchange , 'direct' , false, true, false); //声明初始化交换机 $channel ->queue_declare( $queueName , false, true, false, false); //声明初始化一条队列 $channel ->queue_bind( $queueName , $exchange , $routingKey ); //将队列与某个交换机进行绑定,并使用路由关键字 $msgBody = serialize( $data ); //json_encode($data); $msg = new AMQPMessage( $msgBody , [ 'content_type' => 'text/plain' , 'delivery_mode' => 2]); //生成消息 $channel ->basic_publish( $msg , $exchange , $routingKey ); //推送消息到某个交换机 echo " [x] Sent 'Hello World!'\n" ; $channel ->close(); $connection ->close(); } public function receive(){ $host = config( "queue.connections.rabbitmq.host" ); $port = config( "queue.connections.rabbitmq.port" ); $login = config( "queue.connections.rabbitmq.login" ); $password = config( "queue.connections.rabbitmq.password" ); $vhost = config( "queue.connections.rabbitmq.vhost" ); $exchange = $this ->config[ "dds-zl-mq" ][ "exchange_name" ]; $queueName = $this ->config[ "dds-zl-mq" ][ "queue_name" ]; $routingKey = $this ->config[ "dds-zl-mq" ][ "router_key_name" ]; //路由关键字(也可以省略) $connection = new AMQPStreamConnection( $host , $port , $login , $password , $vhost ); $channel = $connection ->channel(); $channel ->exchange_declare( $exchange , 'direct' , false, true, false); //声明初始化交换机 $channel ->queue_declare( $queueName , false, true, false, false); $channel ->queue_bind( $queueName , $exchange , $routingKey ); //将队列与某个交换机进行绑定,并使用路由关键字 echo " [*] Waiting for messages. To exit press CTRL+C\n" ; $callback = function ( $msg ) { echo ' [x] Received ' , $msg ->body, "\n" ; }; $channel ->basic_consume( $queueName , '' , false, true, false, false, $callback ); while ( $channel ->is_consuming()) { //这个是阻塞模式,有消息就执行回调 $channel ->wait(); } } |
posted on 2021-05-25 08:57 andydaopeng 阅读(94) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)