Laravel框架使用RabbitMQ完成延时队列

RabbitMQ 安装部署链接

https://blog.csdn.net/Xian_Hu/article/details/125124644?spm=1001.2014.3001.5502icon-default.png?t=M4ADhttps://blog.csdn.net/Xian_Hu/article/details/125124644?spm=1001.2014.3001.5502

安装 RabbitMQ延时队列插件

官网下载地址

 

 

下载好的文件移动到 rabbitmq 的插件 plugins 下

docker   exec  -it  49a8887d7f67  /bin  /bash  进入容器代码

执行此命令安装即可

rabbitmq-plugins enable rabbitmq_delayed_message_exchange

 如果部署的 rabbitmq 为线上的话我们要将下载好的文件传输到线上并进行添加操作

 以宝塔为例子:

 

docker cp /RBMQ/rabbitmq_delayed_message_exchange-3.10.2.ez 49a2887d7f67:/opt/rabbitmq/plugins

文件添加后在执行一下安装代码即可

 查询延时队列插件是否安装完成

composer命令安装RabbitMQ

composer require php-amqplib/php-amqplib

书写核心代码操作

<?php

namespace App\Http\Controllers\Service;

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

class RabbitmqServer
{
    private $host = "127.0.0.1";
    private $port = 5672;
    private $user = "guest";
    private $password = "guest";

    private $msg;
    private $channel;
    private $connection;

    //  过期时间
    const TIMEOUT_5_S = 5;     // 5s
    const TIMEOUT_10_S = 10;    // 10s

    private $exchange_logs = "logs";
    private $exchange_direct = "direct";
    private $exchange_delayed = "delayed";

    private $queue_delayed = "delayedQueue";

    const EXCHANGETYPE_FANOUT = "fanout";
    const EXCHANGETYPE_DIRECT = "direct";
    const EXCHANGETYPE_DELAYED = "x-delayed-message";

    public function __construct($type = false)
    {
        $this->connection = new AMQPStreamConnection($this->host, $this->port, $this->user, $this->password);
        $this->channel = $this->connection->channel();
        // 声明Exchange
        $this->channel->exchange_declare($this->exchange_delayed, self::EXCHANGETYPE_DELAYED, false, true, false, false, false, new AMQPTable(["x-delayed-type" => self::EXCHANGETYPE_DIRECT]));
        $this->channel->queue_declare($this->queue_delayed, false, true, false, false);
        $this->channel->queue_bind($this->queue_delayed, $this->exchange_delayed, $this->queue_delayed);
    }

    /**
     * delay creat message
     */
    public function createMessageDelay($msg, $time)
    {
        $delayConfig = [
            'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT,
            'application_headers' => new AMQPTable(['x-delay' => $time * 1000])
        ];
        $msg = new AMQPMessage($msg, $delayConfig);
        return $msg;
    }

    /**
     * delay send message
     */
    public function sendDelay($msg, $time = self::TIMEOUT_10_S)
    {
        $msg = $this->createMessageDelay($msg, $time);;
        $this->channel->basic_publish($msg, $this->exchange_delayed, $this->queue_delayed);
        $this->channel->close();
        $this->connection->close();
    }

    /**
     * delay consum
     * 控制器调用后走这个方法  
     */
    public function consumDelay()
    {
        $callback = function ($msg) {
            //再次写需要进行的操作
            echo ' [x] ', $msg->body, "\n";
            $this->channel->basic_ack($msg->delivery_info['delivery_tag'], false);
        };
        $this->channel->basic_qos(null, 1, null);
        $this->channel->basic_consume($this->queue_delayed, '', false, false, false, false, $callback);
        echo ' [*] Waiting for logs. To exit press CTRL+C', "\n";
        while (count($this->channel->callbacks)) {
            $this->channel->wait();
        }

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

安装一个新的处理类

php artisan make:command RabbitMQConmmand
<?php

namespace App\Console\Commands;

use App\Http\Business\RabbitMQBusiness;
use Illuminate\Console\Command;

class RabbitMQConmmand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'rabbitmq_consumer';//给消费者起个command名称

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     * @return int
     */
    public function handle()
    {
        $Rabbit = new RabbitMQBusiness("x-delayed-message");
        $Rabbit->consumDelay();
    }
}

在控制器调用操作

//    消息队列延迟操作
    public function RbmqCache()
    {
//        获取订单所有值
        $orderId = request('order_id');

//        进行数据存储MQ
        $Rabbit = new RabbitMQBusiness("x-delayed-message");

//        传递数据,并设置时间为10秒执行
        $Rabbit->sendDelay($orderId,10);
    }

最后使用命令开始测试

php artisan rabbitmq_consumer

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值