您现在的位置是:首页 > PHP框架交流PHP框架交流

yii2 使用RabbitMQ mikemadisonweb/yii2-rabbitmq

上善若水2021-11-28 22:12:02【PHP框架交流】 3074人已围观

简介yii2 使用RabbitMQ,入门搭建RabbitMQ并使用一个简单的demo,前提 准备好RabbitMQ 一个yii2项目mq默认登录地址http://127.0.0.1:15672/默认账号密

yii2 使用RabbitMQ,入门搭建RabbitMQ并使用一个简单的demo,
前提 准备好RabbitMQ 一个yii2项目
mq默认登录地址http://127.0.0.1:15672/
默认账号密码 guest guest

1.安装扩展.

composer require mikemadisonweb/yii2-rabbitmq

2.配置RabbitMQ连接

打开config/console.php components中追加连接信息.

'components' => [
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'log' => [
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db' => $db,
        'rabbitmq' => [
            'class' => \mikemadisonweb\rabbitmq\Configuration::class,
            'connections' => [
                [
                    // You can pass these parameters as a single `url` option: https://www.rabbitmq.com/uri-spec.html
                    'host' => '127.0.0.1',
                    'port' => '5672',
                    'user' => 'guest',
                    'password' => 'guest',
                    'vhost' => '/',
                ]
                // When multiple connections is used you need to specify a `name` option for each one and define them in producer and consumer configuration blocks
            ],
        ],
    ],

注意端口号为5672 非web端登录的端口15672

3.编写一个生产者推送消息到队列中

class DemoProducerController extends Controller
{
    const DEMO_PRODUCER = 'DEMO_PRODUCER';
    const DEMO_ROUTING_KEY = 'DEMO_ROUTING_KEY';
    const DEMO_ROUTING_KEY2 = 'DEMO_ROUTING_KEY2';
    const DEMO_EXCHANGE = 'DEMO_EXCHANGE';

    /**
     * php yii demo-producer/index
     */
    public function actionIndex()
    {
        /** @var \mikemadisonweb\rabbitmq\Configuration $rabbitmq */
        $rabbitmq = \Yii::$app->rabbitmq;
        /** @var \mikemadisonweb\rabbitmq\components\Producer $producer */
        $producer = $rabbitmq->getProducer(self::DEMO_PRODUCER);
        $msg = json_encode(['DEMO_ROUTING_KEY2插入随机数' . rand(1, 1000)]);
        for($i=0;$i<100;$i++)
        {
            sleep(1);
            $producer->publish($msg, self::DEMO_EXCHANGE, self::DEMO_ROUTING_KEY2);

        }
    }
}

4.配置一个交换机 配置一个队列 并将队列和交换机绑定

'producers' => [
                [
                    'name' => 'DEMO_PRODUCER',
                ],
            ],
            'exchanges' => [
                [
                    'name' => 'DEMO_EXCHANGE',
                    'type' => 'direct'
                ],
            ],
            'queues' => [
                [
                    'name' => 'DEMO_QUEUES',
                ],
            ],
            'bindings' => [
                [
                    'queue' => 'DEMO_QUEUES',
                    'exchange' => 'DEMO_EXCHANGE',
                    'routing_keys' => ['DEMO_ROUTING_KEY','DEMO_ROUTING_KEY2'],
                ],
            ],

5.生产者发送推送消息

输入命令php yii demo-producer/index 向交换机推送数据.

可以看到已经小消息进来并且进入就绪状态 等待消费.

6.编写一个消费者

<?php

namespace app\commands\rabbitmq\consumer;

use mikemadisonweb\rabbitmq\components\ConsumerInterface;
use PhpAmqpLib\Message\AMQPMessage;

class DemoConsumer implements ConsumerInterface
{
    /**
     * @param AMQPMessage $msg
     * @return int|mixed
     */
    public function execute(AMQPMessage $msg)
    {
        var_dump($msg->getBody());
        return ConsumerInterface::MSG_ACK;
    }
}

6.绑定消费者回调消费

 'bindings' => [
                [
                    'queue' => 'DEMO_QUEUES',
                    'exchange' => 'DEMO_EXCHANGE',
                    'routing_keys' => ['DEMO_ROUTING_KEY','DEMO_ROUTING_KEY2'],
                ],
            ],
            'consumers' => [
                [
                    'name' => 'DEMO_CONSUMER',
                    // Every consumer should define one or more callbacks for corresponding queues
                    'callbacks' => [
                        // queue name => callback class name
                        'DEMO_QUEUES' => app\commands\rabbitmq\consumer\DemoConsumer::class,
                    ],
                ],
            ],

7执行消费命令

php yii rabbitmq/consume DEMO_CONSUMER

成功消费,至此 RabbitMQ 简单使用完成.

Tags: RabbitMQ

很赞哦! (1)

相关文章

文章评论

站点信息

  • 建站时间:2019-10-24
  • 网站程序:Thinkphp6 Layui
  • 文章统计247篇文章
  • 标签管理标签云
  • 统计数据cnzz统计
  • 微信公众号:扫描二维码,关注我们