super.rabbitmq是php-super
的子功能包,实现了php操作rabbitmq的源码封装,阅读本文之前,如需了解php-super
请先阅读 php极速开发源码包super,本文是super.rabbitmq 的功能介绍。
本文提供php操作rabbitmq的代码包供开发者开发使用
RabbitMq/Service
:核心代码封装RabbitMq/Client
:调用Service的代码封装,开发者可以调用Client里的代码,也可以直接调用Service里的代码源码以图片方式展示,获取源码请联系站长
查看源码
获取源码
那么,如何在业务中快速使用源码进行开发呢?
,
查看测试代码
<?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();
<?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();
<?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();