uncleqiu/hyperf-rocketmq-sdk

阿里云RocketMQ PHP框架Hyperf的SDK

v1.0.0 2023-11-18 06:19 UTC

This package is auto-updated.

Last update: 2024-09-18 08:11:29 UTC


README

英文 | 中文

简介

基于阿里巴巴云的aliyunmq/mq-http-sdk,支持Hyperf框架的RocketMQ SDK。

安装

使用composer将hyperf-rocketmq-sdk安装到您的项目中

composer require uncleqiu/hyperf-rocketmq-sdk

使用composer.json

{
    "require": {
      "uncleqiu/hyperf-rocketmq-sdk": "dev-master"
    }
}

并运行

composer install

配置

php bin/hyperf.php vendor:publish uncleqiu/hyperf-rocketmq-sdk

示例

发送消息

<?php
    $messageData = [
        'name' => 'uncleqiu',
    ];
    (new \Uncleqiu\RocketMQ\Client())->push('topic_key1', $messageData);

消费消息

您可以使用以下命令生成自定义命令行

php bin/hyperf.php gen:command TestConsumeCommand

然后编写您的消费逻辑代码

<?php

declare(strict_types=1);

namespace App\Command;

use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Psr\Container\ContainerInterface;
use Uncleqiu\RocketMQ\Client;

#[Command]
class TestConsumeCommand extends HyperfCommand
{
    public function __construct(protected ContainerInterface $container)
    {
        parent::__construct('consume:topic_one');
    }

    public function configure()
    {
        parent::configure();
        $this->setDescription('Consume the rocketmq data');
    }

    public function handle()
    {
        $this->line('Begin Consume....', 'info');
        (new Client())->consume('topic_key1', $this);
    }

    // rocketmq data consume processing logic
    public function handlerMessage($message)
    {
        $mqData = json_decode($message->getMessageBody(), true);
        // write your consume logic......
        var_dump($mqData);
    }
}

最后,运行您的自定义命令行

php bin/hyperf.php consume:topic_one