phpgears/cqrs-async 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

phpgears/cqrs-async

Composer 安装命令:

composer require phpgears/cqrs-async

包简介

Async CQRS

README 文档

README

PHP version Latest Version License

Build Status Style Check Code Quality Code Coverage

Total Downloads Monthly Downloads

Async CQRS

Async decorator for CQRS command bus

Installation

Composer

composer require phpgears/cqrs-async

Usage

Require composer autoload file

require './vendor/autoload.php';

Asynchronous Commands Bus

Command bus decorator to handle commands asynchronously

Enqueue

use Gears\CQRS\Async\AsyncCommandBus;
use Gears\CQRS\Async\Serializer\JsonCommandSerializer;
use Gears\CQRS\Async\Discriminator\ParameterCommandDiscriminator;

/* @var \Gears\CQRS\CommandBus $commandBus */

/* @var Gears\CQRS\Async\CommandQueue $commandQueue */
$commandQueue = new CustomCommandQueue(new JsonCommandSerializer());

$asyncCommandBus new AsyncCommandBus(
    $commandBus,
    $commandQueue,
    new ParameterCommandDiscriminator('async')
);

$asyncCommand = new CustomCommand(['async' => true]);

$asyncCommandBus->handle($asyncCommand);

Dequeue

This part is highly dependent on your message queue, though command serializers can be used to deserialize queue message

This is just an example of the process

use Gears\CQRS\Async\ReceivedCommand;
use Gears\CQRS\Async\Serializer\JsonCommandSerializer;

/* @var \Gears\CQRS\Async\AsyncCommandBus $asyncCommandBus */
/* @var your_message_queue_manager $queue */

$serializer = new JsonCommandSerializer();

while (true) {
  $message = $queue->getMessage();

  if ($message !== null) {
    $command = new ReceivedCommand($serializer->fromSerialized($message));

    $asyncCommandBus->handle($command);
  }
}

Deserialized commands should be wrapped in Gears\CQRS\Async\ReceivedCommand in order to avoid infinite loops should you decide to handle the commands to an async command bus. If you decide to use a non-async bus on the dequeue side you don't need to do this

Discriminator

Discriminates whether a command should or should not be enqueued based on arbitrary conditions

Three discriminators are provided in this package

  • Gears\CQRS\Async\Discriminator\ArrayCommandDiscriminator selects commands if they are present in the array provided
  • Gears\CQRS\Async\Discriminator\ClassCommandDiscriminator selects commands by their class or interface
  • Gears\CQRS\Async\Discriminator\ParameterCommandDiscriminator selects commands by the presence of a command payload parameter (optionally by its value as well)

Command queue

This is the one responsible for actual async handling, which would normally be sending the serialized command to a message queue system such as RabbitMQ

No implementation is provided but an abstract base class so you can extend from it

use Gears\CQRS\Async\AbstractCommandQueue;

class CustomCommandQueue extends AbstractCommandQueue
{
  public function send(Command $command): void
  {
    // Do the actual enqueue of $this->getSerializedCommand($command);
  }
}

You can use cqrs-async-queue-interop that uses queue-interop for enqueuing messages

Serializer

Abstract command queue uses serializers to do command serialization so it can be sent to the message queue as a string message

Gears\CQRS\Async\Serializer\JsonCommandSerializer is directly provided as a general serializer allowing maximum compatibility in case of commands being handled by other systems

You can create your own serializer if the one provided does not fit your needs, for example by using JMS serializer, by implementing Gears\CQRS\Async\Serializer\CommandSerializer interface

Distributed systems

On distributed systems, such as micro-service systems, commands can be dequeued on a completely different part of the system, this part should of course know about commands and their contents but could eventually not have access to the command class itself

For example in the context of Domain Events on DDD a bounded context could handle command delivered by another completely different bounded context and of course won't be able to deserialize the original command as it is located on another domain

This can be solved in one of two ways, transform messages coming out from the message queue before handing them to the command serializer, or better by creating a custom Gears\CQRS\Async\Serializer\CommandSerializer encapsulating this transformation

Transformation can be as simple as changing command class to be reconstituted

Contributing

Found a bug or have a feature request? Please open a new issue. Have a look at existing issues before.

See file CONTRIBUTING.md

License

See file LICENSE included with the source code for a copy of the license terms.

phpgears/cqrs-async 适用场景与选型建议

phpgears/cqrs-async 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 128 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 09 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「command」 「query」 「async」 「asynchronous」 「immutable」 「cqrs」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 phpgears/cqrs-async 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 phpgears/cqrs-async 我们能提供哪些服务?
定制开发 / 二次开发

基于 phpgears/cqrs-async 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 128
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 9
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-09-29