定制 rabbitevents/publisher 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

rabbitevents/publisher

Composer 安装命令:

composer require rabbitevents/publisher

包简介

The Publisher component of the RabbitEvents package.

README 文档

README

The RabbitEvents Publisher component provides an API for publishing events across the application structure. More information is available in the Nuwber's RabbitEvents documentation.

The RabbitEvents Publisher is the part that informs all other microservices that a payment has succeeded.

Table of Contents

  1. Installation via Composer
  2. Configuration
  3. Publishing
  4. Testing

Installation via Composer

RabbitEvents Publisher can be installed via the Composer package manager:

composer require rabbitevents/publisher

After installing Publisher, you can execute the rabbitevents:install Artisan command, which will install the RabbitEvents configuration file into your application:

php artisan rabbitevents:install

Configuration

Details about the configuration are described in the library documentation.

Publishing

Using an Event class

Here is an example event class:

<?php

use App\Payment;
use App\User;
use RabbitEvents\Publisher\ShouldPublish;
use RabbitEvents\Publisher\Support\Publishable;

class PaymentSucceededRabbitEvent implements ShouldPublish
{
    use Publishable;

    public function __construct(private User $user, private Payment $payment)
    {
    }

    public function publishEventKey(): string
    {
        return 'payment.succeeded';
    }

    public function toPublish(): mixed
    {
        return [
            'user' => $this->user->toArray(),
            'payment' => $this->payment->toArray(),
        ];
    }
}

The only requirement for event classes is to implement the \RabbitEvents\Publisher\ShouldPublish interface.

As an alternative, you could extend \RabbitEvents\Publisher\Support\AbstractPublishableEvent. This class was created to simplify the creation of event classes.

To publish this event, you just need to call the publish method of the event class and pass all the necessary data:

<?php

$payment = new Payment(...);

// ...

PaymentSucceededRabbitEvent::publish($request->user(), $payment);

The method publish is provided by the trait Publishable.

Using the publish function

Sometimes, it is easier to use the helper function publish with an event key and payload:

<?php

publish(
    'payment.succeeded',
    [
        'user' => $request->user()->toArray(),
        'payment' => $payment->toArray(),
    ]
);

Publish an Event object with the publish function

You can also use a combination of the two previous methods:

<?php

$event = new PaymentSucceededEvent($request->user(), $payment);

event($event)
publish($event);

Publishing Protobuf Messages

You can publish Google Protobuf messages. The system will automatically handle serialization and set the content_type to application/x-protobuf.

Note: You must install the rabbitevents/protobuf package to use this feature.

use App\Messages\AccountCreated; // Generated Protobuf class

$message = new AccountCreated(['id' => 123, 'name' => 'John']);

publish('account.created', $message);

The type header will be automatically set to App\Messages\AccountCreated, allowing the consumer to re-hydrate the object.

Testing

We always write tests. Tests in our applications contain many mocks and fakes to test how events are published.

There is the PublishableEventTesting trait that provides assertion methods in an Event class that you want to test.

Event.php

<?php

namespace App\BroadcastEvents;

use RabbitEvents\Publisher\ShouldPublish;
use RabbitEvents\Publisher\Support\Publishable;
use RabbitEvents\Publisher\Support\PublishableEventTesting;

class Event implements ShouldPublish
{
    use Publishable;
    use PublishableEventTesting;

    public function __construct(private array $payload) 
    {
    }

    public function publishEventKey(): string
    {
        return 'something.happened';
    }

    public function toPublish(): array
    {
        return $this->payload;
    }
}

Test.php

<?php

use \App\RabbitEvents\Event;
use \App\RabbitEvents\AnotherEvent;

Event::fake();

$payload = [
    'key1' => 'value1',
    'key2' => 'value2',
];

Event::publish($payload);

Event::assertPublished('something.happened', $payload);

AnotherEvent::assertNotPublished();

If the assertion does not pass, Mockery\Exception\InvalidCountException will be thrown.
Don't forget to call \Mockery::close() in tearDown or similar methods of your tests.

rabbitevents/publisher 适用场景与选型建议

rabbitevents/publisher 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.12k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 02 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 rabbitevents/publisher 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 11.12k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-02-18