最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP實(shí)現(xiàn)異步延遲消息隊(duì)列的方法詳解

 更新時(shí)間:2022年05月25日 09:11:47   作者:PHP開源社區(qū)  
這篇文章主要為大家詳細(xì)介紹了如何利用PHP+Laravel+RabbitMQ來實(shí)現(xiàn)異步延遲消息隊(duì)列,文中的實(shí)現(xiàn)過程講解詳細(xì),快跟隨小編一起學(xué)習(xí)一下吧

一、前言

需求:電商秒殺場(chǎng)景中,如果用戶下單10分鐘未支付,需要進(jìn)行庫(kù)存歸還

本篇是用PHP+Laravel+RabbitMQ來實(shí)現(xiàn)異步延遲消息隊(duì)列

二、場(chǎng)景

在電商項(xiàng)目中,當(dāng)我們下單之后,一般需要 20 分鐘之內(nèi)或者 30 分鐘之內(nèi)付款,否則訂單就會(huì)進(jìn)入異常處理邏輯中,被取消,那么進(jìn)入到異常處理邏輯中,就可以當(dāng)成是一個(gè)延遲隊(duì)列

公司的會(huì)議預(yù)定系統(tǒng),在會(huì)議預(yù)定成功后,會(huì)在會(huì)議開始前半小時(shí)通知所有預(yù)定該會(huì)議的用戶

安全工單超過 24 小時(shí)未處理,則自動(dòng)拉企業(yè)微信群提醒相關(guān)責(zé)任人

用戶下單外賣以后,距離超時(shí)時(shí)間還有 10 分鐘時(shí)提醒外賣小哥即將超時(shí)

很多場(chǎng)景下我們都需要延遲隊(duì)列。

本文以 RabbitMQ 為例來和大家聊一聊延遲隊(duì)列的玩法。

使用 RabbitMQ 的 rabbitmq_delayed_message_exchange 插件來實(shí)現(xiàn)定時(shí)任務(wù),這種方案較簡(jiǎn)單。

三、安裝RabbitMQ延遲隊(duì)列插件

官網(wǎng)插件下載地址

我這里直接下載了最新版本,你們根據(jù)自己的rabbitmq版本號(hào)進(jìn)行下載

把下載好的文件移動(dòng)到rabbitmq的插件plugins下,以我自己的Mac為例子,放到了如下路徑

然后執(zhí)行安裝插件指令,如下

rabbitmq-plugins?enable?rabbitmq_delayed_message_exchange

最后重啟rabbitmq服務(wù),并刷新查看exchanges交換機(jī)有沒有該插件

如上圖則延遲消息隊(duì)列插件安裝完成

四、在Laravel框架中進(jìn)行使用

新建rabbitmq服務(wù)類,包含延遲消息隊(duì)列生產(chǎn)消息,和消費(fèi)消息,如下

代碼如下:

<?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;

    //  過期時(shí)間
    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();
    }
}

比如新建QueueController控制器,進(jìn)行測(cè)試生產(chǎn)消息放到延遲消息隊(duì)列中

代碼如下:

<?php

namespace App\Http\Controllers\Api\v1;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Service\RabbitmqServer;
use App\Jobs\Queue;
use Illuminate\Http\Request;

class QueueController extends Controller
{
    //
    public function index(Request $request)
{
        //比如說現(xiàn)在是下訂單操作
        //需求:如果用戶10分鐘之內(nèi)不支付訂單就要取消訂單,并且?guī)齑鏆w還
        $msg = $request->post();
        $Rabbit = new RabbitmqServer("x-delayed-message");
        //第一個(gè)參數(shù)發(fā)送的消息,第二個(gè)參數(shù)延遲多少秒
        $Rabbit->sendDelay(json_encode($msg),5);
    }
}

至此通過接口調(diào)試工具進(jìn)行模擬生產(chǎn)消息即可

消息生產(chǎn)完畢要進(jìn)行消費(fèi),這里使用的是Laravel的任務(wù)調(diào)度,代碼如下

<?php

namespace App\Console\Commands;

use App\Http\Controllers\Service\RabbitmqServer;
use Illuminate\Console\Command;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

class RabbitmqConsumerCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'rabbitmq_consumer';//給消費(fèi)者起個(gè)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 RabbitmqServer("x-delayed-message");
        $Rabbit->consumDelay();
    }
}

五、執(zhí)行生產(chǎn)消息和消費(fèi)消息

用postman模擬生產(chǎn)消息,效果如下:

然后消費(fèi)消息,用一下命令,如果延遲5秒執(zhí)行消費(fèi)則成功

至此,就完成了rabbitmq異步延遲消息隊(duì)列

到此這篇關(guān)于PHP實(shí)現(xiàn)異步延遲消息隊(duì)列的方法詳解的文章就介紹到這了,更多相關(guān)PHP異步延遲消息隊(duì)列內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

博兴县| 盐山县| 永平县| 萍乡市| 兴国县| 宁南县| 巫山县| 花莲县| 长武县| 秭归县| 延庆县| 潢川县| 女性| 长阳| 三穗县| 宜丰县| 遂平县| 千阳县| 云林县| 青冈县| 东莞市| 沙湾县| 安图县| 安化县| 肥东县| 黄山市| 兴义市| 吉首市| 桂平市| 道真| 双牌县| 南乐县| 洪泽县| 察隅县| 镇坪县| 西吉县| 大石桥市| 宝丰县| 定州市| 南皮县| 洪泽县|