zoukankan      html  css  js  c++  java
  • MQ:RabbitMQ学习(三) --- 使用官网提供的php composer包连接

    RabbitMQ学习(三) --- 使用官网提供的php composer包连接

    官网链接:

    https://www.rabbitmq.com/tutorials/tutorial-one-php.html

    https://xiaoxiami.gitbook.io/rabbitmq_into_chinese_php/ying-yong-jiao-cheng/php-ban/1-hello_world

    对应的代码:

    https://github.com/rabbitmq/rabbitmq-tutorials/blob/master/php/receive.php

    一、安装composer拓展

    新建composer.json文件,添加代码

    {
      "require": {
        "php-amqplib/php-amqplib": "2.6.1"
      }
    }

    安装拓展:

    composer install

    二、使用代码

     消费者代码receive.php

    <?php
    require_once __DIR__ . '/vendor/autoload.php';
    use PhpAmqpLibConnectionAMQPStreamConnection;
    
    $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
    $channel = $connection->channel();
    
    $channel->queue_declare('hello', false, false, false, false);
    
    echo " [*] Waiting for messages. To exit press CTRL+C
    ";
    
    $callback = function ($msg) {
        echo ' [x] Received ', $msg->body, "
    ";
    };
    
    $channel->basic_consume('hello', '', false, true, false, false, $callback);
    
    while(count($channel->callbacks)) {
        $channel->wait();
    }
    
    $channel->close();
    $connection->close();

    生产者代码send.php

    <?php
    require_once __DIR__ . '/vendor/autoload.php';
    use PhpAmqpLibConnectionAMQPStreamConnection;
    use PhpAmqpLibMessageAMQPMessage;
    
    $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
    $channel = $connection->channel();
    
    $channel->queue_declare('hello', false, false, false, false);
    
    $msg = new AMQPMessage('Hello World!');
    $channel->basic_publish($msg, '', 'hello');
    
    echo " [x] Sent 'Hello World!'
    ";
    
    $channel->close();
    $connection->close();

    三、使用

    先启动消费者 receive.php 接收消息

    再启动 生产者 send.php 发送消息

    本博客地址: wukong1688

    本文原文地址:https://www.cnblogs.com/wukong1688/p/13406908.html

    转载请著名出处!谢谢~~

  • 相关阅读:
    javascript运动系列第二篇——变速运动
    深入学习jQuery动画控制
    深入学习jQuery动画队列
    深入学习jQuery自定义动画
    深入学习jQuery的三种常见动画效果
    深入学习jQuery鼠标事件
    深入学习jQuery事件对象
    深入学习jQuery事件绑定
    只想显示日期不想显示时间
    The conversion of a varchar data type to a datetime data type resulted in an out-of-range value
  • 原文地址:https://www.cnblogs.com/wukong1688/p/13406908.html
Copyright © 2011-2022 走看看