superbalist/php-pubsub-google-cloud

此包已被弃用且不再维护。未建议替代包。

php-pubsub 包的 Google Cloud 适配器

5.4.0-beta 2020-10-27 13:41 UTC

README

php-pubsub 包的 Google Cloud 适配器。

Author Build Status Software License Packagist Version Total Downloads

安装

composer require superbalist/php-pubsub-google-cloud

使用方法

putenv('GOOGLE_APPLICATION_CREDENTIALS=' . __DIR__ . '/../your-gcloud-key.json');

$client = new \Google\Cloud\PubSub\PubSubClient([
    'projectId' => 'your-project-id-here',
]);

$adapter = new \Superbalist\PubSub\GoogleCloud\GoogleCloudPubSubAdapter($client);


// disable auto topic & subscription creation
$adapter->setAutoCreateTopics(false); // this is true by default
$adapter->setAutoCreateSubscriptions(false); // this is true by default

// set a unique client identifier for the subscriber
$adapter->setClientIdentifier('search_service');

// consume messages
// note: this is a blocking call
$adapter->subscribe('my_channel', function ($message) {
    var_dump($message);
});

// publish messages
$adapter->publish('my_channel', 'HELLO WORLD');
$adapter->publish('my_channel', json_encode(['hello' => 'world']));
$adapter->publish('my_channel', 1);
$adapter->publish('my_channel', false);

gRPC 支持

Google Cloud PHP v0.12.0 添加了对 gRPC 协议通信的支持。

gRPC 非常适合高性能、低延迟的应用程序,在性能和延迟是关键的情况下强烈推荐。

如果所有依赖都安装了,库将自动选择 gRPC 而不是 REST。

pecl install grpc

composer require google/gax
composer require google/proto-client

后台批量消息支持

Google Cloud v0.33.0 添加了对排队消息和后台发布的支持。这在本包的版本 5+ 中可用,需要至少 google/cloud ^0.33.0 版本。

您可以通过在构造 GoogleCloudPubSubAdapter 时将 $backgroundBatching 设置为 true 或在现有适配器上调用 setBackgroundBatching(true) 来启用后台批量消息。

如果 semaphorepcntl PHP 扩展已启用,并且 IS_BATCH_DAEMON_RUNNING 环境变量设置为 true,则库将排队消息以便由 批量守护进程 发布。批量守护进程需要作为长期后台进程手动运行。

对于所有其他情况,消息将在内存中排队,并在脚本终止之前使用供应商注册的关闭处理程序发布。

请注意

此功能由 google/cloud 标记为实验性功能,可能在发布之前以向后不兼容的方式进行更改。

示例

库包含了适配器的 示例 和运行示例脚本的 Dockerfile

运行 make up

您将开始在 /opt/php-pubsub 目录中的 bash 提示符。

如果您需要另一个 shell 来向阻塞消费者发布消息,可以运行 docker-compose run php-pubsub-google-cloud /bin/bash

要运行示例

$ php examples/GoogleCloudConsumerExample.php
$ php examples/GoogleCloudPublishExample.php (in a separate shell)