php极速开发源码包之super.rabbitmq

2022/6/27 4:20:11

本文主要是介绍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的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程