定制 goodway/laravel-nats 二次开发

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

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

goodway/laravel-nats

Composer 安装命令:

composer require goodway/laravel-nats

包简介

Nats jetstream queue driver with client for Laravel

README 文档

README

With multiple flexible client and queue configurations, with events and customizable handlers

PHP Version Requirement Latest Version License Total Downloads

Scheme

Feel free to contribute or give any feedback.

Prerequisites

Laravel Version

This package can be used in Laravel 8 or higher. The minimum PHP version required is 8.1

Config file

This package publishes a config/nats.php file. If you already have a file by that name, you must rename or remove it, as it will conflict with this package. You could optionally merge your own values with those required by this package, as long as the keys that this package expects are present. See the source file for more details.

Nats Client

As a Nats client we use an external basis-company/nats.php package - the most popular, well-written and functional Nats client for PHP. Greatest thanks to Dmitry Krokhin (nekufa)!

Installation

The recommended way to install the library is through Composer:

$ composer require goodway/laravel-nats

You should publish the config/nats.php config file with:

$ php artisan vendor:publish --provider="Goodway\LaravelNats\LaravelNatsProvider"

Configuration

Client connection

The client connection configuration is specified in the config/nats.php file. Multiple configuration supported

    'client' => [
        'configurations' => [
            'default' => [
                'host' => env('NATS_HOST', 'localhost'),
                'port' => intval(env('NATS_PORT', 4222)),
                'user' => env('NATS_USER'),
                'password' => env('NATS_PASSWORD'),
                'token' => env('NATS_TOKEN'), // Sets an authorization token for a connection
                'nkey' => env('NATS_NKEY'), // new, highly secure public-key signature system based on Ed25519
                'jwt' => env('NATS_JWT'), // Token for JWT Authentication
                'reconnect' => env('NATS_RECONNECT', true),
                'connection_timeout' => floatval(env('NATS_CONNECTION_TIMEOUT', 1)), // Number of seconds the client will wait for a connection to be established
                'verbose_mode' => env('NATS_VERBOSE_MODE', false), // Turns on +OK protocol acknowledgements
                'inbox_prefix' => env('NATS_INBOX_PREFIX', '_INBOX'), // Sets default prefix for automatically created inboxes
                'ping_interval' => intval(env('NATS_PING_INTERVAL', 2)), // Number of seconds between client-sent pings
                'ssl_key' => env('NATS_SSL_KEY_FILE'),
                'ssl_cert' => env('NATS_SSL_CERT_FILE'),
                'ssl_ca' => env('NATS_SSL_CA_FILE'),
            ],
            ...
        ]
    ]

Queue connection

Describe the queue connection configuration in your 'config/queue.php' file. It supports multiple client configurations.

Example:

    'nats' => [
        'driver' => 'nats',
        'consumer_client' => 'default', // client configuration name from nats.php config file to listen queue
        'publisher_client' => 'default', // client configuration name from nats.php config file to publish queue
        'jetstream' => env('NATS_JETSTREAM', 'jetstream'),
        'jetstream_retention_policy' => env('NATS_JETSTREAM_RETENTION_POLICY', 'workqueue'),
        'consumer' => env('NATS_CONSUMER_GROUP', 'consumer'),
        'consumer_iterations' => intval(env('NATS_CONSUMER_ITERATIONS', 3)),
        'queue_consumer_create' => (bool)env('NATS_QUEUE_CONSUMER_CREATE', false),
        'queue_consumer_prefix' => env('NATS_QUEUE_CONSUMER_PREFIX', 'con'),
        'queue_separated_clients' => env('NATS_QUEUE_SEPARATE_IDENTICAL_CLIENTS', true), // separate Nats clients with the identical configuration
        //            'queue_handler' => 'someClass',
        'fire_events' => (bool)env('NATS_QUEUE_MESSAGE_EVENTS', true),
        'default_batch_size' => intval(env('NATS_DEFAULT_BATCH_SIZE', 10)),
        'verbose_mode' => (bool)env('NATS_QUEUE_VERBOSE_MODE', true),
        'check_jetstream_publish' => (bool)env('NATS_QUEUE_CHECK_JETSTREAM_PUBLISH', false)
    ],

Fields description:

  • driver - [string] queue driver name. Use 'nats'
  • consumer_client - [string] client configuration name used to listen queue
  • publisher_client - [string] client configuration name used to publish messages
  • jetstream - [string] nats jetstream name
  • jetstream_retention_policy - [string] jetstream retention policy. Used on queue listening to generate the correct consumer name
  • consumer - [string] consumer group. Used for the final name of the consumer
  • consumer_iterations - [int, optional, 2] how many times message request should be sent
  • consumer_delay - [float, optional, 1] how long to wait (in sec.) before sending the next request if an empty response was received
  • queue_consumer_create - [bool] if true, Queue will try to automatically create a new consumer if one is not found. This functionality only works if the current connection client has the necessary permissions to create a consumer
  • queue_consumer_prefix - [string] consumer prefix. Used for the final name of the consumer
  • queue_separated_clients - [bool] see description below
  • fire_events - [bool] if true then events will be fired during the publishing and handling received messages processes
  • default_batch_size - [int, optional, 10] batch size. How many messages would be requested from nats stream
  • queue_handler - [string, optional] classname of your custom queue handler. If not defined, then the standard handler will be used
  • verbose_mode - [bool, optional, false] var_dump some additional info. For example on publishing
  • check_jetstream_publish [bool, optional, false] - additionally check for the existence of a jetstream with each PUB. When enabled, it can significantly slow down performance because in this case an additional request-response to the Jetstream API is required

You can specify one connection for publisher and another one for consumer, or use one connection for both roles.

You can also use one or separate connections if the publisher client configuration name matches the consumer client configuration. Use this feature through the "queue_separated_clients" bool attribute.

Publishing to queue

The easiest way to send a message to queue is to call a dispatch() method on a simple class that extends NatsMessageJob. The class must contain a body() method that will return the contents of the message. Return type for body() is string.

Example:

use Goodway\LaravelNats\NatsMessageJob;

class TestNatsJob extends NatsMessageJob
{
    public function body(): string
    {
        return json_encode([
            'msd_id' => $this->msgId,
            'push_data' => ['some_field' => md5($this->msgId)]
        ]);
    }
}

You can also use a dynamic body by passing content through the constructor.

NatsMessageJob class uses Dispatchable and Queueable concerns and implements classic ShouldQueue interface. Examples:

dispatch((new TestNatsJob())
    ->onConnection('nats')
    ->onQueue('jetstream_subject');
TestNatsJob::dispatch()
    ->onConnection('nats')
    ->onQueue('jetstream_subject');

You can also specify the subject and jetstream for message using the $subject and $jetstream variables

class
    protected string $subject = 'mySubject';

or

dispatch((new TestNatsJob())
    ->setSubject('mySubject'))
    ->onConnection('nats')->onQueue('queue-name');

DispatchNats

This package provides an additional dispatch mechanism and a corresponding helper with the dispatchNats() function. It supports additional functions designed to make it easier to understand and interact with publishing.

With dispatchNats You can set a specific jetstream or subject when dispatching a job. And also specify whether to call events.

Examples:

With helper function

dispatchNats((new TestNatsJob())
    ->onConnection('nats')
    ->onJetstream('jetstream_name')
    ->onSubject('jetstream_subject')
    ->withEvents();

With class static function

TestNatsJob::dispatchNats()
    ->onConnection('nats')
    ->onJetstream('jetstream_name')
    ->onSubject('jetstream_subject')
    ->withoutEvents();

Headers

You can set a message headers using the headers() method of your class. Example:

class TestNatsJob extends NatsMessageJob
{
    public function headers(): array
    {
        return [
            'header1_key' => 'header1_value',
            'header2_key' => 'header2_value',
        ];
    }
    ...
}

Listening from queue/jetstream subject

You can connect and listen to messages from the queue using standard queue:work mechanism. Example:

php artisan queue:work nats --queue=queue-name

queue:work command options (Recommended)

This package also extends the queue:work command and provides additional options that allow you to specify a jetstream and consumer you want to connect to, as well as to set the batch size for reading messages.

php artisan queue:work nats --jetstream=jetstream --consumer=durable_consumer --batch=50 --queue=any-name

--consumer option ignores the --queue option. In this case, the --queue option will not be used in the connection logic, it is only needed for visual recognition of the queue for the developer.

Message object structure

{
    "body": [string] any string: plain text, json-string or serialized data of your message
    "headers": [array] headers array
    "subject": [string] subject value
    "timestamp": [int] timestamp in ms
}

Events

These events are fired during the publishing and listening processes.

NatsQueueMessageSent

This event is fired after a message is sent to the queue

class NatsQueueMessageSent
    ...
    public function __construct(
        public readonly string $jetstream,
        public readonly string $subject,
        public readonly NatsMessage $message
    ) {}

NatsQueueMessageReceived

This event is fired when a message is received from the queue

class NatsQueueMessageReceived
    ...
    public function __construct(
        public readonly string $jetstream,
        public readonly string $subject,
        public readonly NatsMessage $message
    ) {}

More documentation will be added soon...

...

goodway/laravel-nats 适用场景与选型建议

goodway/laravel-nats 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.82k 次下载、GitHub Stars 达 34, 最近一次更新时间为 2024 年 03 月 31 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 goodway/laravel-nats 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 5.82k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 34
  • 点击次数: 13
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 34
  • Watchers: 2
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-03-31