PHP 操作RabbitMQ

RabbitMq-PHP

{ "require": { "php-amqplib/php-amqplib": "2.5.*" }}

composer update

操作实例

Singletion.php 单例创建

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/26
 * Time: 11:53
 */

namespace App\lib;


trait Singleton
{
    private static $instance;

    public static function getInstance(...$args) {
        if(is_null(self::$instance)) {
            self::$instance = new static(...$args);
        }
        return self::$instance;
    }
}

基础类

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/26
 * Time: 11:38
 */

namespace App\lib;

use Illuminate\Support\Facades\Log;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Exception\AMQPTimeoutException;
use PhpAmqpLib\Message\AMQPMessage;

class Amqp
{
    private $connection;

    private $channel;

    const EXCHANGE_NAME = 'test_exchange';

    const ROUTE_KEY = 'test_route';

    const QUEUE_NAME = 'test_queue';


    use Singleton;

    public function __construct() {
        $this->connection = new AMQPStreamConnection('127.0.0.1', 5672, 'guest', 'guest', '/');
        $this->channel = $this->connection->channel();
    }


    /**
     * 创建 exchange
     */
    public function createChange() {

        $type = "direct";
        $passive = false;
        $durable = true;
        $auto_delete = true;
        $this->channel->exchange_declare(self::EXCHANGE_NAME, $type, $passive, $durable, $auto_delete);
    }

    /**
     * 创建queue
     */
    public function createQueue() {
        $this->channel->queue_declare(self::QUEUE_NAME, false, true, false, false);
    }

    /**
     * 绑定 change 和 queue
     */
    public function bindExchangeAndQueue() {
        $this->channel->queue_bind(self::QUEUE_NAME, self::EXCHANGE_NAME);
    }


    /**
     * 发布到exchange 消息
     */
    public function publish() {
        $messageBody = [
            'example' => date('Y-m-d H:i:sS')
        ];
        $message = new AMQPMessage($messageBody);
        $this->channel->basic_publish($message, self::EXCHANGE_NAME);
    }



    public function consumer() {
        $this->channel->basic_consume(self::QUEUE_NAME, "", false, false, false, false,function($msg){


            $message = json_decode($msg->body, true);

            file_put_contents("./mq.log", $message,FILE_APPEND);

            $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
        });

    }

    /**
     * 监听
     * @throws \ErrorException
     */
    public function listenerOfShell() {
        try {
            while (count($this->channel->callbacks)) {

                $this->channel->wait();
                dd($this->channel->callbacks);
            }
        }
        catch(AMQPTimeoutException $e){
            Log::error($e->getMessage());
        }
    }


    /**
     * 测试执行
     */
    public function exec() {
        $this->createChange();
        $this->createQueue();
        $this->bindExchangeAndQueue();
        $this->publish();
        $this->consumer();
    }


    /**
     * 销毁链接
     */
    public function __destruct()
    {
        Log::info('destroy connection of rabbitMqp');

        $this->channel->close();
        $this->connection->close();
    }


}

posted @ 2019-11-26 15:26  zoubiao  阅读(174)  评论(0)    收藏  举报