企业级PHP异步RabbitMQ协程版客户端 2.0 正式发布

概述

workerman/rabbitmq 是一个异步RabbitMQ客户端,使用AMQP协议。

RabbitMQ是一个基于AMQP(高级消息队列协议)实现的开源消息组件,它主要用于在分布式系统中存储和转发消息。RabbitMQ由高性能、高可用以及高扩展性出名的Erlang语言写成,具有高度的可靠性和可扩展性。它支持多种消息协议,包括AMQP、STOMP、MQTT等,并广泛应用于消息队列、消息中间件等领域。

RabbitMQ允许应用程序通过消息传递进行通信,这使得不同的应用程序可以在不同的语言和操作系统之间进行通信。

RabbitMQ的消息工作机制涉及消息从发送端到接收端的流转过程。在这个过程中,消息首先被发送到交换机(Exchange),然后交换机根据路由规则将消息路由到一个或多个队列(Queue)中。消费者(Consumer)从队列中获取消息并进行处理。

生产者和消费者

安装

composer require workerman/rabbitmq

消费者

receive.php

<?php

declare(strict_types=1);

use Bunny\Channel;
use Bunny\Message;
use Workerman\Worker;
use Workerman\RabbitMQ\Client;

require_once __DIR__ . '/vendor/autoload.php';

$worker = new Worker();
$worker->eventLoop = \Workerman\Events\Revolt::class;

$worker->onWorkerStart = function() {
    // Create RabbitMQ Client
    $client = Client::factory([
        'host' => '127.0.0.1',
        'port' => 5672,
        'user' => 'guest',
        'password' => 'guest',
        'vhost' => '/',
        'heartbeat' => 60,
        'heartbeat_callback' => function () {
            echo " [-] coroutine-consumer-heartbeat\n";
        },
        'interval' => [100, 300]
    ])->connect();
    $channel = $client->channel();
    $channel->queueDeclare('hello-coroutine');

    // Consumer
    $channel->consume(function (Message $message, Channel $channel, \Bunny\AbstractClient $client) {
        echo " [>] Received ", $message->content, "\n";
    },
        'hello-coroutine',
        '',
        false,
        true
    );
    $client->run();
    echo ' [*] Waiting for messages. To exit press CTRL+C', "\n";

    // Producer
    \Workerman\Timer::add($interval = 5 , function () use ($channel) {
        $channel->publish($message = 'Hello World By Self Timer. ' . time(), [], '', 'hello-coroutine');
        echo " [<] Sent $message\n";
    });
    echo " [!] Producer timer created, interval: $interval s.\n";

};
Worker::runAll();

运行命令

php receive.php start

基于 Workerman 发布

send.php

<?php

declare(strict_types=1);

use Workerman\RabbitMQ\Client;
use Workerman\Worker;

require_once __DIR__ . '/vendor/autoload.php';

$worker = new Worker();
$worker->eventLoop = \Workerman\Events\Revolt::class;

$worker->onWorkerStart = function() {
    $client = Client::factory([
        'host' => 'host.docker.internal',
        'port' => 5672,
        'user' => 'guest',
        'password' => 'guest',
        'vhost' => '/',
        'heartbeat' => 60,
        'heartbeat_callback' => function () {
            echo "coroutine-producer-heartbeat\n";
        }
    ])->connect();
    $channel = $client->channel();
    $channel->queueDeclare('hello-coroutine');

    // 每5秒发一个消息
    \Workerman\Timer::add(5, function () use ($channel) {
        $channel->publish($message = 'Hello World By Workerman Env Producer. ' . time(), [], '', 'hello-coroutine');
        echo " [x] Sent '$message'\n";
    });
};
Worker::runAll();

运行命令

php send.php start

基于 PHP-FPM 发布

script.php

<?php

declare(strict_types=1);

use Workerman\RabbitMQ\Client;

require_once __DIR__ . '/vendor/autoload.php';

$client = Client::factory([
    'host' => 'host.docker.internal',
    'port' => 5672,
    'user' => 'guest',
    'password' => 'guest',
    'vhost' => '/',
    'heartbeat' => 60,
    'heartbeat_callback' => function () {
        echo "coroutine-producer-heartbeat\n";
    }
])->connect();
$channel = $client->channel();
$channel->queueDeclare('hello-coroutine');
$res = $channel->publish($message = 'Hello World By Normal Producer. ' . time(), [], '', 'hello-coroutine');

echo " [x] Sent '$message', success: $res\n";

运行命令

php script.php

异步消费者

receive.php

<?php

use Bunny\Channel;
use Bunny\Message;
use Workerman\Worker;
use Workerman\RabbitMQ\Client;

require __DIR__ . '/vendor/autoload.php';

$worker = new Worker();

$worker->onWorkerStart = function() {
    (new Client())->connect()->then(function (Client $client) {
        return $client->channel();
    })->then(function (Channel $channel) {
        return $channel->queueDeclare('hello', false, false, false, false)->then(function () use ($channel) {
            return $channel;
        });
    })->then(function (Channel $channel) {
        echo ' [*] Waiting for messages. To exit press CTRL+C', "\n";
        $channel->consume(
            function (Message $message, Channel $channel, Client $client) {
                echo " [x] Received ", $message->content, "\n";
            },
            'hello',
            '',
            false,
            true
        );
    });
};
Worker::runAll();

运行命令

php receive.php start

异步生产者

send.php

<?php
use Bunny\Channel;
use Bunny\Message;
use Workerman\Worker;
use Workerman\RabbitMQ\Client;

require __DIR__ . '/vendor/autoload.php';

$worker = new Worker();

$worker->onWorkerStart = function() {
    (new Client())->connect()->then(function (Client $client) {
        return $client->channel();
    })->then(function (Channel $channel) {
        return $channel->queueDeclare('hello', false, false, false, false)->then(function () use ($channel) {
            return $channel;
        });
    })->then(function (Channel $channel) {
        echo " [x] Sending 'Hello World!'\n";
        return $channel->publish('Hello World!', [], '', 'hello')->then(function () use ($channel) {
            return $channel;
        });
    })->then(function (Channel $channel) {
        echo " [x] Sent 'Hello World!'\n";
        $client = $channel->getClient();
        return $channel->close()->then(function () use ($client) {
            return $client;
        });
    })->then(function (Client $client) {
        $client->disconnect();
    });
};
Worker::runAll();

运行命令

php send.php start
相关推荐
网络安全-杰克1 分钟前
网络安全概论——防火墙原理与设计
网络·web安全·php
慢慢成长的码农15 分钟前
Kotlin 常量定义
开发语言·数据库·kotlin
Eiceblue34 分钟前
Python在Excel工作表中创建数据透视表
开发语言·python·visualstudio·excel
2401_858286111 小时前
124.【C语言】数据结构之快速排序的小区间优化和非递归的解决方法
c语言·开发语言·数据结构·算法·排序算法·
编程小筑1 小时前
TypeScript语言的网络编程
开发语言·后端·golang
老大白菜1 小时前
第6章:Go语言并发编程
开发语言·后端·golang
黑果果的思考1 小时前
C++例程:使用I/O模拟IIC接口(6)
开发语言·c++
西猫雷婶1 小时前
python学opencv|读取图像(三十)使用cv2.getAffineTransform()函数倾斜拉伸图像
开发语言·python·opencv
编程|诗人1 小时前
Erlang语言的学习路线
开发语言·后端·golang
小团团01 小时前
使用Python和Neo4j驱动程序来实现小规模数据的CSV导入
开发语言·python·neo4j