linxi/pulsar-client 问题修复 & 功能扩展

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

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

linxi/pulsar-client

Composer 安装命令:

composer require linxi/pulsar-client

包简介

README 文档

README

Contents

About

This is a Apache Pulsar client library implemented in php Reference PulsarApi.proto And support Swoole coroutine

Features

  • Support URL (pulsar://pulsar+ssl://http://https://)
  • Multi topic consumers
  • TLS connection
  • Automatic reconnection (Only Consumer)
  • Message batching
  • Message Properties
  • Authentication with jwt, basic

Requirements

  • PHP >=7.0 (Supported PHP8)
  • Swoole Extension(If you want to use in swoole)
    • Use in the swoole only requires that the SWOOLE_HOOK_SOCKETS、SWOOLE_HOOK_STREAM_FUNCTION or SWOOLE_HOOK_ALL

Installation

composer require linxi/pulsar-client

registering

registering service providers and facades in the config/app.php

 'providers' => [
        PulsarProducerProvider::class,
        PulsarConsumerProvider::class,],
 'aliases' => [
        'PulsarProducer' => PulsarProducer::class,
        'PulsarConsumer' => PulsarConsumer::class,
]

Publish Vendor Files

This command will generate a pulsar.php configuration file in the config directory. This file contains the configuration settings for the Pulsar client.

php artisan vendor:publish --provider="Linxi\PulsarClient\PulsarProducerProvider"

config/pulsar.php

the default option is default

<?php

return [

//config your pulsar connections 
    'connections' => [
        'default' => [
            'url' => env('PULSAR_SERVICE_URL_DEFAULT', 'pulsar://localhost:6650'),
            'token' => env('PULSAR_SERVICE_TOKEN_DEFAULT', 'pulsar://localhost:6650'),
            'timeout' => env('PULSAR_SERVICE_TIMEOUT_DEFAULT', 3),
        ],
    ],

//config your pulsar topics 
    'topics' => [
        'default' => [
            'name' => env('PULSAR_TOPIC_DEFAULT', 'persistent://public/test/demo_1216'),
            'subscriber' => env('PULSAR_SUBSCRIBER_DEFAULT', 'default_consumer'),
            //subscription_type Exclusive:0 Shared:1 Failover:2 Key_Shared:3
            'subscription_type' => env('PULSAR_SUBSCRIPTION_TYPE_DEFAULT', 1),
            //earliest:1  latest:0
            'initial_position' => env('PULSAR_SUBSCRIPTION_INITIAL_POSITION_DEFAULT', 1),
            'queue_size' => env('PULSAR_RECEIVE_QUEUE_SIZE', env('QUEUE_SIZE', 100)),
            //redeliver after a few seconds
            'nack_redelivery_delay' => env('PULSAR_SET_NACK_REDELIVERY_DELAY', 5),
            'dead_letter_policy' => [
                'max_redeliver_count' => env('PULSAR_MAX_REDELIVER_COUNT', 3),
                'dead_letter_topic' => env('PULSAR_DEAD_LETTER_TOPIC', 'persistent://public/test/test_dead')
            ],
        ],

    ],

//config your pulsar connetion and topic 

    'topic_servers' => [
        'default' => [
            'connection' => 'default',
            'topic' => 'default'
        ],
    ],
    
    //this is for consumer command 
    'tasks' => [
        //task_name
        'pulsar_test_task' => [
            'topic_server' => 'default',
            'task_process_class' => '\App\Pulsar\PulsarTest::class',
        ]
    ]

];

PulsarTask

the demo of pulsar task is App\PulsarTask\PulsarTaskDemo

Register the Command Class

use Linxi\PulsarClient\PulsarTask\PulsarConsumerTask;
class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        PulsarConsumerTask::class,
    ];
}

Producer

<?php
use Linxi\PulsarClient\Facades\PulsarProducerFacade;
require_once __DIR__ . '/vendor/autoload.php';

//            $msgId = PulsarProducer::setTopicServer("default")->send("this message is " . time());
            $msgId = PulsarProducerFacade::send("this message is " . time());

Consumer

<?php
use Linxi\PulsarClient\Facades\PulsarConsumerFacade;
require_once __DIR__ . '/vendor/autoload.php';
        while (true) {
            $message = PulsarConsumerFacade::setTopicServer('default')->receive();
            echo sprintf('Got message 【%s】messageID[%s] topic[%s] nowTime[%s] publishTime[%s] redeliveryCount[%d]',
                    $message->getPayload(),
                    $message->getMessageId(),
                    $message->getTopic(),
                    date('Y-m-d H:i:s'),
                    date('Y-m-d H:i:s', $message->getPublishTime() / 1000),
                    $message->getRedeliveryCount()
                ) . "\n";
            PulsarConsumerFacade::ack($message);
        }

Configure the Consumer Script

config tasks in config/pulsar.php

    'tasks' => [
        //task_name
        'pulsar_task_demo' => [
            'topic_server' => 'default',
            'task_process_class' => App\PulsarTask\PulsarTaskDemo::class,
        ]
    ]

Launch the Task

php artisan  pulsar:consumer pulsar_task_demo

Options

  • Producer
    • setTopicServer()
    • send()
    • sendAsync()
  • Consumer
    • receive()
    • batchReceive()
    • ack()
    • nack()
    • setTopicServer()

MessageNotFound ErrCode (v1.2.1)

  • MessageNotFound::Ignore
  • MessageNotFound::CommandParseFail

License

MIT LICENSE

linxi/pulsar-client 适用场景与选型建议

linxi/pulsar-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 12 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 linxi/pulsar-client 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2024-12-16