前言
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();
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦