robertbaelde/persisting-message-bus
Composer 安装命令:
composer require robertbaelde/persisting-message-bus
包简介
README 文档
README
This package will provide a message bus that persists its messages. This can be used for cross context communication with public events. The message bus will be for a topic. A topic is a set of event types.This allows for the consuming context to only know about the topic and its event types, and not where they originate from.
Installation
composer require robertbaelde/persisting-message-bus
Usage
Configuring a topic
A topic is a set of message classes, mapped to a string name. Each topic must have a unique name.
<?php use Robertbaelde\PersistingMessageBus\BaseTopic; class TestTopic extends BaseTopic { public const SimpleDomainMessage = SimpleDomainMessage::class; public function getMessages(): array { return [ 'SimpleDomainMessage' => self::SimpleDomainMessage ]; } public function getName(): string { return 'TestTopic'; } }
Messages must implement the PublicMessage interface
use Robertbaelde\PersistingMessageBus\PublicMessage; class SimpleDomainMessage implements PublicMessage { }
Dispatching messages
In order to dispatch messages you'll need a message bus. This can be constructed using a topic and a message repository.
With this topic you are able to construct a MessageDispatcher, which can be used to dispatch messages. Message dispatchers can decorate messages using a MessageDecorator.
$message = new SimpleDomainMessage('bar'); $messageBus = new MessageBus( new TestTopic(), $this->messageRepository ); $messageDispatcher = new MessageDispatcher( $messageBus, new DefaultMessageDecorator(new SystemClock()), ); $messageDispatcher->dispatch($message);
Consuming messages
In order to consume messages you'll need a message bus and a repository that keeps track of your offset to the message stream. Eventsauce's message consumers can be used as consumers.
$messageConsumer = new MessageConsumer( messageBus: $this->messageBus, messageConsumerState: new InMemoryMessageConsumerState(), messageConsumer: $consumer ); $messageConsumer->handleNewMessages();
When using a message consumer you might want to run handleNewMessages in a loop. Make sure only one process is handling new messages at a time. Otherwise messages might be handled double.
Illuminate repositories
In order to persist messages and consumer state 2 repositories are provided.
Database scheme for messages:
CREATE TABLE IF NOT EXISTS `public_messages` ( `id` bigint unsigned NOT NULL AUTO_INCREMENT, `message_id` varchar (255) NOT NULL, `topic` varchar (255) NOT NULL, `message_type` varchar (255) NOT NULL, `payload` varchar (1200) NOT NULL, `headers` varchar (1200) NOT NULL, `published_at` timestamp NOT NULL, PRIMARY KEY (`id` ASC), KEY `topic` (`topic`, `id` ASC) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Database scheme for consumers:
CREATE TABLE IF NOT EXISTS `message_consumer_state` ( `consumer_name` varchar (255) NOT NULL, `cursor` varchar (1200) NOT NULL, `last_updated_at` timestamp NOT NULL, PRIMARY KEY (`consumer_name`), KEY `consumer_name` (`consumer_name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Roadmap
- Add a consumer that makes http requests for cross service sync
- Add system for Correlation & Causation id's?
- Message inbox pattern
- Message outbox pattern
License
The MIT License (MIT). Please see License File for more information.
robertbaelde/persisting-message-bus 适用场景与选型建议
robertbaelde/persisting-message-bus 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 21.15k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2022 年 09 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 robertbaelde/persisting-message-bus 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 robertbaelde/persisting-message-bus 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 21.15k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2022-09-12