tdonselaar/googlecloud-pubsub-php-adapter
Composer 安装命令:
composer require tdonselaar/googlecloud-pubsub-php-adapter
包简介
PHP Adapter class for google-cloud-php-pubsub package.
关键字:
README 文档
README
A Php class adapter for the Goolge Cloud PubSub package.
Installation
composer require twipi-group/googlecloud-pubsub-php-adapter
Samples
Publish
use Google\Cloud\PubSub\PubSubClient; use TwipiGroup\GoogleCloudPubSubPhpAdapter\GcPubSub; $client = new PubSubClient([ 'projectId' => 'your-googlecloud-project-id', ]); $pubsub = new GcPubSub($client); $pubsub->publish('mychannel', 'test first'); $pubsub->publish('mychannel', json_encode(['test' => 'another'])); $pubsub->publish('mytopic', [ 'test' => [0, 1, 2] ]);
Consume
use Google\Cloud\PubSub\PubSubClient; use TwipiGroup\GoogleCloudPubSubPhpAdapter\GcPubSub; $client = new PubSubClient([ 'projectId' => 'your-googlecloud-project-id', ]); $pubsub = new GcPubSub($client); // case blocking call $pubsub->subscribe('mychannel', function ($message) { var_dump($message); }); // OR $pubsub->subscribe('mysubscriber', function ($message) { var_dump($message); }, 'mytopic'); // case non blocking call $messages = $pubsub->consume('mychannel'); // OR $messages = $pubsub->consume('mysubscriber', 'mytopic'); foreach ($messages as $message) { var_dump($message); }
Functions
Uses
function createTopic(string $topicName): Topic
This function creates a topic from $topicName and returns topic object.
function getTopic(string $topicName): Topic
This function returns an existing topic object from $topicName or create a new topic if setAutoCreateTopic is true. If setAutoCreateTopic is false, an exception will be thrown.
function deleteTopic(string $topicName): Topic
This function deletes an existing topic from $topicName and returns topic object.
function updateTopic($topicName, $properties): Topic
This function updates an existing topic from $topicName and returns topic object. Note that the topic's name and kms key name are immutable properties and may not be modified. https://googleapis.github.io/google-cloud-php/#/docs/google-cloud/v0.131.0/pubsub/topic
function getTopics(): array
This function returns an array of existing topics objects.
function deleteTopics(): array
This function deletes all existing topics and returns array of topics name deleted.
function createSubscription(string $subscriptionName, string $topicName = ''): Subscription
This function creates a topic from $subscriptionName and returns subscription object. If setAutoCreateTopicFromSubscription and setAutoCreateTopic are true and $topicName is empty, so a topic will be created from $subscriptionName.
function getSubscription(string $subscriptionName, string $topicName = ''): Subscription
This function returns an existing subscription object from $subscriptionName or create a new subscription if setAutoCreateSubscription is true. If setAutoCreateSubscription is false, an exception will be thrown. If setAutoCreateTopicFromSubscription and setAutoCreateTopic are true and $topicName is empty, so a topic will be created from $subscriptionName. If setAutoCreateTopicFromSubscription or setAutoCreateTopic is false and $topicName is empty, an exception will be thrown.
function deleteSubscription($subscriptionName): Subscription
This function deletes an existing subscription from $subscriptionName and returns subscription object.
function updateSubscription($subscriptionName, $properties): Subscription
This function updates an existing subscription from $subscriptionName and returns subscription object. Note that subscription name and topic are immutable properties and may not be modified. https://googleapis.github.io/google-cloud-php/#/docs/google-cloud/v0.131.0/pubsub/subscription
function getSubscriptions(): array
This function returns an array of existing subscriptions objects.
function consume(string $subscriptionName, string $topicName = ''): array
This function always return immediately after one pull. You just can change setMaxMessages for receiving x messages from this pull. If setAutoCreateTopic and setAutoCreateTopicFromSubscription are true, and if $topicName is empty, it will auto create topic from subscription name if topic not already exists. This function returns an array of messages.
function subscribe(string $subscriptionName, callable $handler, string $topicName = '', $infiniteLoop = true): void
This function is an infinite loop by default thanks to $infiniteLoop = true.
-
if $infiniteLoop is true :
- if setReturnImmediately is true and setDelay > 1 second, the loop will sleep every one second between each pull
- if setReturnImmediately is true and setDelay = 0 second, the loop will work everytime, this IS NOT RECOMMENDED because it adversely impacts the performance of the system.
- if setReturnImmediately is false, (RECOMMENDED) the system waits (~90 seconds) until at least one message is available, and setDelay has no effect.
-
if $infiniteLoop is false :
- setReturnImmediately and setDelay has no impact because just one loop will be executed with only one pull.
Settings
// set max messages number consuming by pull $pubsub->setMaxMessages(2); // 100 by default // set if return immediately after pull $pubsub->setReturnImmediately(true); // false by default // set delay in seconds to wait between each pull // note: if ReturnImmediately is false, this option has no effect. $pubsub->setDelay(2); // 10 by default // set a suffix for subscriptions $pubsub->setTopicSuffix('mysubsriber_'); // empty by default // set debug mode to display pull messages directly to output $pubsub->setDebug(true); // false by default // set if auto create topic when not existing $pubsub->setAutoCreateTopic(false); // true by default // set if auto create subscription when not existing $pubsub->setAutoCreateSubscription(false); // true by default // set if auto create topic from subscription name when consume messages with empty topic name $pubsub->setAutoCreateTopicFromSubscription(false); // true by default
Tests
vendor/bin/phpunit Tests
Contribution
You can contribute to this package by discovering bugs, opening issues or purpose new features.
Licence
This project is licensed under the terms of the MIT license. See License file for more information.
tdonselaar/googlecloud-pubsub-php-adapter 适用场景与选型建议
tdonselaar/googlecloud-pubsub-php-adapter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 01 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「adapter」 「pubsub」 「gcp」 「googlecloud」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tdonselaar/googlecloud-pubsub-php-adapter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tdonselaar/googlecloud-pubsub-php-adapter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tdonselaar/googlecloud-pubsub-php-adapter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Adapter designed to add Framework Agnosticism for Caching within PHP Packages.
Queue
flysystem cache Adapter
Virtual Filesystem Storage Adapter for Laravel
Библиотека для реализаций паттерна Pub/Sub
Библиотека для реализаций паттерна Pub/Sub
统计信息
- 总下载量: 8
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 26
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-01-01