关于rabbitmq:php极速开发源码包之superrabbitmq

31次阅读

共计 2953 个字符,预计需要花费 8 分钟才能阅读完成。

前言

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

源码获取

分割博主获取源码 >>

正文完
 0