• rabbitmq生产与消费测试


    消息生成者:

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    /* require_once FCPATH . '/vendor/autoload.php';
    use PhpAmqpLibConnectionAMQPStreamConnection;
    use PhpAmqpLibMessageAMQPMessage; */
    /* set_include_path(APPPATH . '/third_party/PhpAmqpLib/Connection');
    require_once APPPATH . 'third_party/PhpAmqpLib/Connection/AMQPStreamConnection.php';
    require_once APPPATH . 'third_party/PhpAmqpLib/Connection/Message/AMQPMessage.php'; */
    
    require_once FCPATH . '/vendor/autoload.php';
    use PhpAmqpLibConnectionAMQPStreamConnection;
    use PhpAmqpLibMessageAMQPMessage;
    
    $exchangeName = 'kd_sms_send_ex'; //交换机名
    $queueName = 'kd_sms_send_q'; //队列名称
    $routingKey = 'sms_send'; //路由关键字(也可以省略)
    
    //建立生产者与mq之间的连接
    $connection = new AMQPStreamConnection('101.200.62.202', 5672, 'msgsend', 'test_123','msgs');
    $channel = $connection->channel();//在已连接基础上建立生产者与mq之间的通道
    
    $channel->exchange_declare($exchangeName, 'direct', false, true, false); //声明初始化交换机
    $channel->queue_declare($queueName, false, true, false, false);//声明初始化一条队列
    $channel->queue_bind($queueName, $exchangeName, $routingKey); //将队列与某个交换机进行绑定,并使用路由关键字
    
    $msgBody = json_encode(["name" => "iGoo", "age" => 22]);
    $msg = new AMQPMessage($msgBody, ['content_type' => 'text/plain', 'delivery_mode' => 2]); //生成消息
    $channel->basic_publish($msg, $exchangeName, $routingKey);//推送消息到某个交换机
    
    echo " [x] Sent 'Hello World!'
    ";
    
    $channel->close();
    $connection->close();
    

      

    消息消费者:

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    
    require_once FCPATH . '/vendor/autoload.php';
    use PhpAmqpLibConnectionAMQPStreamConnection;
    use PhpAmqpLibMessageAMQPMessage; 
    
    $exchangeName = 'kd_sms_send_ex'; //交换机名
    $queueName = 'kd_sms_send_q'; //队列名称
    $routingKey = 'sms_send'; //路由关键字(也可以省略)
    
    $connection = new AMQPStreamConnection('101.200.62.202', 5672, 'msgsend', 'test_123','msgs');
    $channel = $connection->channel();
    
    $channel->exchange_declare($exchangeName, 'direct', false, true, false); //声明初始化交换机
    $channel->queue_declare($queueName, false, true, false, false);
    $channel->queue_bind($queueName, $exchangeName, $routingKey); //将队列与某个交换机进行绑定,并使用路由关键字
    
    echo " [*] Waiting for messages. To exit press CTRL+C
    ";
    
    $callback = function ($msg) {
        echo ' [x] Received ', $msg->body, "
    ";
    };
    
    $channel->basic_consume($queueName, '', false, true, false, false, $callback);
    
    while ($channel->is_consuming()) {//这个是阻塞模式,有消息就执行回调
        $channel->wait();
    }
    
    /* $channel->close();
    $connection->close(); */
    

      

  • 相关阅读:
    edge.js架起node.js和.net互操作桥梁
    Swift学习:闭包(Closures)
    swift UIAlertController教程
    linux配置IP的方法
    centos 6.5安装vncserver 并开启远程桌面
    CSS中各种居中方法
    jquery中的index方法和eq方法
    line-height的用法(一)
    第八章—BOM(一)
    第四章—变量,作用域和内存问题(一)
  • 原文地址:https://www.cnblogs.com/andydao/p/14783660.html
Copyright © 2020-2023  润新知