kotalawala / laravel-stomp

Laravel Stomp 驱动程序

dev-master 2024-08-22 08:17 UTC

This package is auto-updated.

Last update: 2024-09-22 08:28:34 UTC


README

Laravel Stomp 驱动程序

此包允许在 Laravel 内部原生使用 Stomp 驱动程序进行队列操作。

安装

通过 composer 安装,并自动注册为 Laravel 服务提供者。

composer require asseco-voice/laravel-stomp

要将它连接到您的队列,您需要更改 .env 文件中的队列连接驱动程序

QUEUE_CONNECTION=stomp

连接变量

STOMP_PROTOCOL      protocol (defaults to TCP)
STOMP_HOST          broker host (defaults to 127.0.0.1)
STOMP_PORT          port where STOMP is exposed in your broker (defaults to 61613)
STOMP_USERNAME      broker username (defaults to admin)
STOMP_PASSWORD      broker password (defaults to admin)

您可以使用以下方式订阅队列以读取或写入

STOMP_READ_QUEUES=...
STOMP_WRITE_QUEUES=...

两者在订阅方面具有相同的命名约定

topic                        <-- will read from all queues on the topic
topic::queue1                <-- will read only from queue1 on the topic
topic::queue1;topic::queue2  <-- will read from queue1 and queue2 on the topic

使用客户端确认选项(环境变量)进行订阅

STOMP_CONSUMER_WIN_SIZE=819200      // number of bytes that Broker will send to client before it expects ACK
STOMP_CONSUMER_ACK_MODE=auto		// mode: client (ACK needs to be sent) | auto (no ACK, and window-size has to be -1 in that case)

Laravel 工作进程选项

STOMP_CONSUMER_ALL_QUEUES = default;	// which queue name(s) represent that all queues from Config should be read
STOMP_READ_MESSAGE_DB_LOG = false		// write POP-ed events in DB table `stomp_event_logs`

您可以在 配置文件 中查看所有其他可用的 .env 变量、它们的默认值和使用说明。

如果使用 horizon 作为工作进程,库将与 Laravel Horizon 并行工作,并将自动解决基本配置

'environments' => [
    'production' => [
        'supervisor-1' => [
            'connection' => 'stomp',
            'queue' => [env('STOMP_READ_QUEUES', 'default')],
            ...
        ],
    ],

    'local' => [
        'supervisor-1' => [
            'connection' => 'stomp',
            'queue' => [env('STOMP_READ_QUEUES', 'default')],
            ...
        ],
    ],
],

如果您需要自定义配置,请发布 Horizon 配置(请参阅 Horizon 文档)并适应您的需求。

非 Laravel 事件

也可以处理外部事件。默认情况下,如果事件不是标准 Laravel 事件,它将被重新抛出为 stomp.* 事件,其中包含它接收到的有效负载。

如果您收到的帧属于 topic::test_queue 队列,系统将抛出 stomp.topic.test_queue 事件,否则如果出于某种原因无法解析队列名称,它将调度一个 stomp.event 事件。

您可以通过在 EventServiceProvider::boot() 中包含以下内容来监听它

Event::listen('stomp.*', function ($event, $payload) {
    ...
});

失败的任务

为了简洁和简洁,StompJob 类被定义为利用 Laravel 的尝试和回退属性(官方文档)。

失败后,作业将重试 5 次然后写入 failed_jobs 表。

每次后续尝试将在 attempt^2 秒内进行,这意味着如果它是第三次尝试,它将在前一次作业失败后 9 秒后重试。

注意,默认情况下,**作业属性优先于 CLI 命令**,因此在此默认设置下,--tries--backoff 标志将被覆盖。

您可以使用以下 env 变量来关闭此行为

  • STOMP_AUTO_TRIES - 默认为 true。将其设置为 false 以恢复 Laravel 默认的 0 重试。
  • STOMP_AUTO_BACKOFF - 默认为 true。将其设置为 false 以恢复 Laravel 默认的 0s 回退。
  • STOMP_BACKOFF_MULTIPLIER - 默认为 2。如果 STOMP_AUTO_BACKOFF 已关闭,则不起作用。增加它以使两次失败作业之间的间隔更大。

作业将被重新入队到它来自的队列。

报头

由于 Laravel 不将事件报头保存到 failed_jobs 表中,此包通过将报头作为有效负载的一部分存储在 _headers 键中来绕过此限制。这一切都是在幕后自动完成的,因此不需要与它交互。

如果您想向事件添加额外的报头,可以通过实现 HasHeaders 接口来实现。

原始数据

如果您的服务需要与外部非Laravel服务通信,可以通过实现HasRawData接口来绕过Laravel事件包装。这将使您能够向代理提供自定义的有效负载。

日志

默认情况下,日志是关闭的。您可以通过设置环境键STOMP_LOGS=true来包含它们。

如果您想更改默认的日志管理器,可以在asseco-stomp配置文件中完成。新的日志管理器必须扩展Illuminate\Log\LogManager

用法

现在您可以使用库,就像使用原生的Laravel队列一样。有关用法,您可以查看官方Laravel队列文档