php極速開發源碼包之super.rabbitmq

前言

super.rabbitmq是php-super的子功能包,實現了php操作rabbitmq的源碼封裝,閱讀本文之前,如需了解php-super請先閱讀 php極速開發源碼包super,本文是super.rabbitmq 的功能介紹。

文章簡介

本文提供php操作rabbitmq的代碼包供開發者開發使用

適用對象

  • php開發者

文章價值

  • 開箱即用,節省開發時間
  • 提供測試代碼供學習使用

目錄結構

  • RabbitMq/Service:核心代碼封裝
  • RabbitMq/Client:調用Service的代碼封裝,開發者可以調用Client里的代碼,也可以直接調用Service里的代碼

代碼(圖片)

源碼以圖片方式展示,獲取源碼請聯系站長
查看源碼
獲取源碼

安裝方式

  • composer安裝(推薦)
  • 直接復制源碼到項目并調用

調用方式

那么,如何在業務中快速使用源碼進行開發呢?
查看測試代碼

  • 創建消息隊列
<?php
// 示例代碼:創建息隊列/初始化消息隊列

namespace tests\Service\Mq\RabbitMq;

use Seed\Super\Service\Mq\RabbitMq\Service\InitService;
use Seed\Super\Service\Mq\RabbitMq\Service\MqInstanceService;

require_once __DIR__ . '/../../../../vendor/autoload.php';

// 獲取連接
$config = [];
$config['mq_host'] = '127.0.0.1';
# 默認端口:15672 為網頁管理 5672 為 AMQP端口
$config['mq_port'] = 5672;
$config['mq_user'] = 'guest';
$config['mq_password'] = 'guest';
$config['mq_virtual_host'] = 'test-host';
$mqInstanceService = new MqInstanceService($config);
$connection = $mqInstanceService->handle();

// 初始化路由、隊列
$config = [];
$config['connection'] = $connection;
$config['exchange'] = 'test-exchange';
$config['queue'] = 'test-queue';
$config['route_key'] = 'test-route_key';
$initService = new InitService($config);
$initService->handle();
  • 發布Mq消息
<?php
// 示例代碼:發布消息
namespace tests\Service\Mq\RabbitMq;

use Seed\Super\Service\Mq\RabbitMq\Service\MqInstanceService;
use Seed\Super\Service\Mq\RabbitMq\Service\PublisherService;

require_once __DIR__ . '/../../../../vendor/autoload.php';

// 獲取連接
$config = [];
$config['mq_host'] = '127.0.0.1';
# 默認端口:15672 為網頁管理 5672 為 AMQP端口
$config['mq_port'] = 5672;
$config['mq_user'] = 'guest';
$config['mq_password'] = 'guest';
$config['mq_virtual_host'] = 'test-host';
$mqInstanceService = new MqInstanceService($config);
$connection = $mqInstanceService->handle();

$config = [];
$config['connection'] =$connection;
$config['message'] = 'this is a test mq message';
$config['route_key'] = 'test-route_key';
$config['exchange'] = 'test-exchange';
$publisherService = new PublisherService($config);
$publisherService->handle();
  • 消費mq消息
<?php
// 示例代碼:消費消息隊列
require_once __DIR__ . '/../../../../vendor/autoload.php';

use Seed\Super\Service\Mq\RabbitMq\Service\ConsumerTraitService;
use Seed\Super\Service\Mq\RabbitMq\Service\MqInstanceService;

// 消費消息隊列
class Consumer_Test{

    use ConsumerTraitService;

    // 消費
    public function consume(){
        // 獲取連接
        $config = [];
        $config['mq_host'] = '127.0.0.1';
        # 默認端口:15672 為網頁管理 5672 為 AMQP端口
        $config['mq_port'] = 5672;
        $config['mq_user'] = 'guest';
        $config['mq_password'] = 'guest';
        $config['mq_virtual_host'] = 'test-host';
        //消費
        $mqInstanceService = new MqInstanceService($config);
        $connection = $mqInstanceService->handle();
        $config['connection'] = $connection;
        $config['consumer_tag'] = 'consumer_tag_1';
        $config['queue'] = 'test-queue';
        return  $this->consumeHandle($config);
    }

    // 獲取到消息數據并處理
    public function consumeMessageCallback($message)
    {
        print_r("receive message:".$message->body);
        $this->consumeAck(\Seed\Super\Service\Mq\RabbitMq\Constant::CONSUMER_ACK,$message);
    }

    // 報錯處理
    public function errorCatchCallBack($e)
    {
        print_r("error:".$e->getMessage());
    }
}

(new Consumer_Test())->consume();

相關文章

原文鏈接

原文來自《稻田代碼》php極速開發源碼包之super.rabbitmq

源碼獲取

聯系博主獲取源碼>>

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容