首页 > 其他分享 >rabbitmq模式 publish subscribe

rabbitmq模式 publish subscribe

时间:2022-09-20 23:48:14浏览次数:65  
标签:false logs publish queue subscribe rabbitmq php data channel

rabbitmq模式 publish subscribe

emit_log.php

<?php

require_once __DIR__ . '/../../vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

$connection = new AMQPStreamConnection('localhost', 5672, 'admin', 'admin');
$channel = $connection->channel();

$channel->exchange_declare('logs', 'fanout', false, false, false);

$data = implode(' ', array_slice($argv, 1));
if (empty($data)) {
$data = "info: Hello World!";
}
$msg = new AMQPMessage($data);

$channel->basic_publish($msg, 'logs');

echo ' [x] Sent ', $data, "\n";

$channel->close();
$connection->close();

receive_logs.php

<?php

require_once __DIR__ . '/../../vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;

$connection = new AMQPStreamConnection('localhost', 5672, 'admin', 'admin');
$channel = $connection->channel();

$channel->exchange_declare('logs', 'fanout', false, false, false);

list($queue_name, ,) = $channel->queue_declare("", false, false, true, false);

$channel->queue_bind($queue_name, 'logs');

echo " [*] Waiting for logs. To exit press CTRL+C\n";

$callback = function ($msg) {
echo ' [x] ', $msg->body, "\n";
};

$channel->basic_consume($queue_name, '', false, true, false, false, $callback);

while ($channel->is_open()) {
$channel->wait();
}

$channel->close();
$connection->close();

运行调试

php emit_log.php
php receive_logs.php > logs_from_rabbit.log
php receive_logs.php

标签:false,logs,publish,queue,subscribe,rabbitmq,php,data,channel
From: https://www.cnblogs.com/hu308830232/p/16714085.html

相关文章

  • rabbitmq模式 RPC
    rabbitmq模式RPCrpc_server.php<?phprequire_once__DIR__.'/../../vendor/autoload.php';usePhpAmqpLib\Connection\AMQPStreamConnection;usePhpAmqpLib\Mes......
  • rabbitmq模式 topics
    rabbitmq模式topicsemit_log_topic.php<?phprequire_once__DIR__.'/../../vendor/autoload.php';usePhpAmqpLib\Connection\AMQPStreamConnection;usePhpAmqp......
  • RabbitMQ集群脑裂故障处理
    网络抖动或故障均可能会导致RabbitMQ集群发生脑裂故障,在不同节点的RabbitMQ管理界面上可以看出其他节点存在红色不可用,显示错误信息如下NetworkpartitiondetectedMnesi......
  • centos安装rabbitMQ
    rabbitmq下载地址https://github.com/rabbitmq/rabbitmq-server/releaseserlang下载地址https://github.com/rabbitmq/erlang-rpm/releases如:CentOS7可以选择 rabbitmq-s......
  • 连接RabbitMQ
    varfactory=newConnectionFactory();factory.HostName="localhost";factory.UserName="guest";factory.Password="guest"......
  • rabbitMQ中将ReadOnlyMemory转换为byte []
    rabbitmq-C#将ReadOnlyMemory转换为byte[]发布于2020-07-3111:16:05给定ReadOnlyMemory结构,我想将流转换为字符串我有以下代码:varbody=ea.Body;//ea.Bod......
  • RabbitMQ笔记
    1.RabbitMQ中间件1.1.什么是中间件什么是中间件我国企业从20世纪80年代开始就逐渐进行信息化建设,由于方法和体系的不成熟,以及企业业务的市场需求的不断变化,一个企业......
  • centos7安装rabbitmq-3.9.5单机部署
    资源下载地址:#erlang下载https://github.com/rabbitmq/erlang-rpm/releases/tag/v23.3.4.6#rabbitmq-server下载https://github.com/rabbitmq/rabbitmq-server/rele......
  • “Affinity Publisher”让排版更简单,轻松完成你的设计大作
    《AffinityPublisher》(以下简称《Publisher》)为桌面出版工作带来了一股简洁的新风。无论你要制作公司简报、社区垃圾分类宣传册,还是要把你自己创作的史诗巨著制成上千页的......
  • RabbitMQ安装教程(linux与Docker)
    一.架构的演变过程  二.正确看待异步架构1.优势(1).削峰降低了服务器的压力(2).响应快加快了服务器的响应速度,优化了用户体验(3).降耦合降低了服务器之间......