asseco-voice/laravel-stomp

Laravel Stomp 驱动器

v4.0.5 2024-06-05 07:16 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 的尝试和回退属性,开箱即用(官方文档)。

失败后,任务将在写入 failed_jobs 表之前重试 5 次。

后续尝试将在 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队列文档