php-amqplib使用实例

本文详细介绍了如何使用PHP-AMQPLib库与RabbitMQ进行集成,包括安装配置过程、代码实例展示以及如何通过shell脚本监控消费者。通过具体示例,读者可以了解到如何在本地环境中搭建并使用RabbitMQ消息队列。

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

相关链接

composer安装地址:https://packagist.org/packages/php-amqplib/php-amqplib

搭建和配置rabbitmq:https://blog.csdn.net/why444216978/article/details/84570801

安装PHP-AMPQ:https://blog.csdn.net/why444216978/article/details/84571127

PHP-AMPQ扩展使用,方便理解rabbitmq和三种交换机:https://blog.csdn.net/why444216978/article/details/102534283

通过shell脚本监控消费者举例:https://blog.csdn.net/why444216978/article/details/84987048

也可参考使用supervisord:https://blog.csdn.net/why444216978/article/details/102153705

代码实例

Rabbitmq.php

<?php
require_once './vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

class Rabbitmq
{
    protected $channel;
    public function __construct($name = 'default')
    {
        //$this->_logSrv = &load_class('Log', 'core');
        //$this->_conf   = C('rabbitmq');
        $this->_name   = $name;
    }

    public function connect()
    {
        $connection    = new AMQPStreamConnection('localhost', 5672, 'why', 'why', 'why', false, 'AMQPLAIN', null, 'en_US', 3.0, 3.0, null, false, 0);
        $this->channel = $connection->channel();
        return true;
    }

    /**
     * @param $messageBody  消息内容
     * @param $exchange     交换机名
     * @param $routeKey     路由键
     * @param array $head
     * @return bool
     */
    public function publish($messageBody, $exchange, $routeKey, $head = [])
    {
        if (!$this->connect()) {
            return false;
        }
        $head          = array_merge(array('content_type' => 'text/plain', 'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT), $head);
        $message       = new AMQPMessage($messageBody, $head);
        $res = $this->channel->basic_publish($message, $exchange, $routeKey);
        return $res;
    }

    /**
     * @param $queue 队列名
     * @param $consumerTag 路由键(这里自动遵循交换机类型匹配规则,默认已经创建好)
     * @param $call_back 回调处理方法
     * @return bool
     */
    public function consumer($queue, $consumerTag, $call_back)
    {
        if (!isset($call_back) || empty($call_back)) {
            return false;
        }

        if (!$this->connect()) {
            return false;
        }

        $this->channel->basic_qos(null, 10, null);

        $this->channel->basic_consume($queue, $consumerTag, false, false, false, false, $call_back);
        while (count($this->channel->callbacks)) {
            $this->channel->wait();
        }
    }
    static public function consumer_ack($message)
    {
        echo "\n--------\n";
        echo $message->body;
        echo "\n--------\n";

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

        // Send a message with the string "quit" to cancel the consumer.
        if ($message->body === 'quit') {
            $message->delivery_info['channel']->basic_cancel($message->delivery_info['consumer_tag']);
        }
    }
}

producer.php

<?php
require_once './Rabbitmq.php';

$rabbitmq = new Rabbitmq();
$res = $rabbitmq->connect();

if($res){
    echo 'connect success!' . "\n";
    $msg = ['key' => 'name', 'value' => 'weihaoyu'];
    $msg = json_encode($msg);
    $rabbitmq->publish($msg, 'why_exchange', 'why_route');
    echo 'send success:' . $msg . "\n";
}else{
    echo 'connect error!' . "\n";
}

consumer.php

<?php
require_once './Rabbitmq.php';

$callBack = function ($message){
    //print_r($message);
    $body = $message->body;
    if(!empty($body)){
        echo "\n" . 'consume success:' . $body . "\n";
        $message->delivery_info['channel']->basic_ack($message->delivery_info['delivery_tag']);
    }
};

$rabbitmq = new Rabbitmq();
$res = $rabbitmq->connect();

$rabbitmq->consumer('why_queue', 'why_route', $callBack);

看结果

生产:

whydeMacBook-Pro:php why$ php producer.php 
connect success!
send success:{"key":"name","value":"weihaoyu"}

消费:

consume success:{"key":"name","value":"weihaoyu"}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AirGo.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值