前言
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();
相干文章
- docker装置RabbitMQ
原文链接
原文来自《稻田代码》
php极速开发源码包之super.rabbitmq
源码获取
分割博主获取源码>>