rabbitmq消息队列php实际应用

本文介绍了一种基于RabbitMQ的消息队列实现方案,并提供了使用PHP进行开发的具体实例。文章详细展示了如何通过AMQPLib库来创建连接、声明交换机及队列、发送消息等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

rabbitmq 消息队列 php应用

前段时间公司需要用到消息队列,就凑时间研究了下rabbitmq,由于本人ubuntu环境,windows应用不确定哈。

代码贴出来了,根据自身项目编写,并不适合所有项目,需要调整自行改动。


1.MQ则是遵循了AMQP协议的具体实现和产品,所以需要AMQPLib的支持

composer require php-amqplib/php-amqplib

2.参考

rabbitmq官方示例


3.base示例


<?php

/**
 * @datetime 2016-11-28  13:30:19
 * @encoding UTF-8 
 * @filename RabbitmqService.php 
 * @description
 */

namespace projects\Service;

use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;


class RabbitmqService
{

    /**
     * 配置
     * @var array
     */
    public $config;


    /**
     * @var string
     */
    public $channel;

    /**
     * initialized
     * @var string
     */
    public $connect;

    /**
     * @var string
     */
    public $queue_name = '';

    /**
     * @var string
     */
    public $exchange_name = '';

    /**
     * @var string
     */
    public $severity;


    public function __construct(array $config) 
    {
        $this->config = $config;
    }

    /**
     * @param string $channel_id
     */
    public function open($channel_id = null) 
    {
        $this->connect = new AMQPStreamConnection($this->config['host'], $this->config['port'], $this->config['user'], $this->config['pwd'], $this->config['vhost']);
        $this->channel = $this->connect->channel($channel_id);
    }

    /**
     * @param string $queue
     * @param bool $passive
     * @param bool $durable
     * @param bool $exclusive
     * @param bool $auto_delete
     * @param bool $nowait
     * @param null $arguments
     * @param null $ticket
     * @return mixed|null
     */
    public function queueDeclare(
        $queue = '',
        $passive = false,
        $durable = false,
        $exclusive = false,
        $auto_delete = false,
        $nowait = false,
        $arguments = null,
        $ticket = null
    ) 
    {
        $this->queue_name = $queue;
        $this->channel->queue_declare($this->queue_name, $passive, $durable, $exclusive, $auto_delete, $nowait, $arguments, $ticket);

    }

    /**
     * @param string $queue_name
     * @param string $exchange_name
     * @param string $severity
     */
    public function queueBind($queue_name = '', $exchange_name = '', $severity = '') 
    {
        if($queue_name == '') $queue_name = $this->queue_name;
        if($exchange_name == '') $exchange_name = $this->exchange_name;
        $this->severity = $severity;
        $this->channel->queue_bind($queue_name, $exchange_name, $this->severity);
    }

    /**
     * @param $path
     * @param string $data
     * @param string $type
     * @return int
     */
    public function putContents($path, $data = '', $type = '') 
    {
        return file_put_contents($path, $data, $type);
    }

    /**
     * @param array $data
     * @param $name
     * @param $exchange_name
     * @param string $type
     * @param bool $queue_durable
     * @param bool $message_durable
     */
    public function send(array $data, $name, $exchange_name, $type = 'fanout', $queue_durable = true, $message_durable = true) 
    {
        $this->open();
        $this->exchangeDeclare($exchange_name, $type, $queue_durable);
        $this->queueDeclare($name, false, $queue_durable, false, false);
        if(isset($data['time'])) {
            $this->basicPublish($data, $message_durable, $name);
        }
        $this->close();
    }

    /**
     * @param $exchange
     * @param $type
     * @param bool $durable
     * @param bool $passive
     * @param bool $auto_delete
     * @param bool $internal
     * @param bool $nowait
     * @param null $arguments
     * @param null $ticket
     */
    public function exchangeDeclare(
        $exchange,
        $type,
        $durable = false,
        $passive = false,
        $auto_delete = true,
        $internal = false,
        $nowait = false,
        $arguments = null,
        $ticket = null
    ) 
    {
        $this->exchange_name = $exchange;
        $this->channel->exchange_declare($exchange, $type, $passive, $durable, $auto_delete, $internal, $nowait, $arguments, $ticket);
    }

    /**
     * A Message for use with the Channnel.basic_* methods.
     * @param $msg
     * @param $message_durable
     * @return AMQPMessage
     */
    public function message($msg, $message_durable) 
    {
        if($message_durable === true) {
            return new AMQPMessage($msg, ['delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT]);
        }
        return new AMQPMessage($msg);
    }

    /**
     * close connect
     */
    public function close() 
    {
        $this->channel->close();
        $this->connect->close();
    }

    /**
     * Publishes a message
     * @param array $msg
     * @param bool $message_durable
     * @param string $routing_key
     * @param string $exchange
     * @param bool $mandatory
     * @param bool $immediate
     * @param null $ticket
     */
    public function basicPublish(array $msg,
        $message_durable = true,
        $routing_key = '',
        $exchange = '',
        $mandatory = false,
        $immediate = false,
        $ticket = null
    ) 
    {
        if(empty($msg['queue'])) {
            $msg['queue'] = $this->queue_name;
        }
        if(is_array($msg)) {
            $msg = serialize($msg);
        }
        if(!is_object($msg)) {
            $msg = $this->message($msg, $message_durable);
        }
        $this->channel->basic_publish($msg, $exchange, $routing_key, $mandatory, $immediate, $ticket);
    }

    /**
     *
     * Wait for some expected AMQP methods and dispatch to them.
     * Unexpected methods are queued up for later calls to this PHP
     * method.
     */
    function wait()
    {
        while(count($this->channel->callbacks)) {
            $this->channel->wait();
        }
    }

    /**
     * start a queue consumer
     * @param string $consumer_tag
     */
    public function basicConsume($consumer_tag) 
    {
        $this->channel->basic_consume($this->queue_name, $consumer_tag);
    }

    /**
     * acknowledge one or more messages
     * @param string $delivery_tag
     */
    public function basicAck($delivery_tag) 
    {
        $this->channel->basic_ack($delivery_tag);
    }

    /**
     * @param string $queue
     * @param null $callback
     * @param string $consumer_tag
     * @param bool $no_local
     * @param bool $no_ack
     * @param bool $exclusive
     * @param bool $nowait
     * @param null $ticket
     * @param array $arguments
     */
    function receive(
        $queue = '',
        $callback = null,
        $consumer_tag = '',
        $no_local = false,
        $no_ack = false,
        $exclusive = false,
        $nowait = false,
        $ticket = null,
        $arguments = array()
    ) 
    {
        $this->channel->basic_consume($queue, $consumer_tag, $no_local, $no_ack, $exclusive, $nowait, $callback, $ticket, $arguments);

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值