gdg-tangier/cloud-pubsub

Google Cloud pub-sub for laravel

This package is not auto-updated.

Last update: 2024-09-18 21:02:59 UTC


README

为什么?

使用事件驱动的微服务架构(Pub/Sub)构建可扩展的 Laravel 应用程序,此工具为您的 Laravel 应用程序添加了相互通信的能力,使用 Google Cloud Pub/Sub

定义您的架构。

首先,您需要在 Google Cloud Platform 中创建订阅和主题,或者您可以使用 cli。

安装。

  • composer require gdg-tangier/cloud-pubsub

配置。

  • config/queue.php

您可以在 config/queue.php 中定义多个订阅者(队列连接)配置,应用程序可以订阅多个订阅。

示例。

'pubsub' => [
      'driver' => 'pubsub',
      'queue'  => env('SUBSCRIPTION'),
      'credentials' => [
          'keyFilePath' => storage_path(env('PUBSUB_CLIENT_KEY')), // credentials file path '.json'
          'projectId'   => env('GCP_PROJECT_ID'),
      ],
  ],
  • config/pubsub.php

在这里,您可以定义您的 subscriptions 作业、事件和主题映射。

示例。

<?php

return [
    /*
     * GCP Credentials.
     */
    'credentials' => [
        'keyFilePath' => storage_path(env('PUBSUB_CLIENT_KEY', 'client')),
        'projectId'   => env('GCP_PROJECT_ID'),
    ],

    /*
     * Here where you map events name with Google Pub/Sub topics.
     *
     * Means, map each event name to specific Google Pub/Sub topic.
     */
    'events'      => [
        'event_name' => '__YOUR_TOPIC_NAME__',
    ],

    /*
     * Here where you can tie the subscription classes (jobs) to topics.
     *
     * Means, map each subscription job to a specific Google pubsub topic.
     * The subscription job is responsible for handling the incoming data
     * from a Google Pub/Sub topic.
     */
    'subscriptions'        => [
        \App\PubSub\DummyJob::class => '__YOUR_TOPIC_NAME__',
    ],
];

创建订阅类。

  • php artisan pubsub:make-subscriber <Name>

订阅类将被创建在 app/Subscribers

示例。

<?php

namespace App\Subscribers;

use GDGTangier\PubSub\Subscriber\SubscriberJob;
use GDGTangier\PubSub\Subscriber\Traits\JobHandler;

class UserUpdated
{
    use JobHandler;

    /**
     * @var mixed
     */
    public $payload;

    /**
     * @var \GDGTangier\PubSub\Subscriber\SubscriberJob
     */
    public $job;

    /**
     * UserUpdated constructor.
     *
     * @param \GDGTangier\PubSub\Subscriber\SubscriberJob $job
     * @param $payload
     */
    public function __construct(SubscriberJob $job, $payload)
    {
        $this->job = $job;
        $this->payload = $payload;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        // 
    }
}

将数据发布到云端。

  • 使用外观。
use GDGTangier\PubSub\Publisher\Facades\PublisherFacade;

PublisherFacade::publish('MyData', 'event_name');
  • 使用服务容器。
$publisher = app('gcloud.publisher.connection');

$publisher->publish('MyData', 'event_name');
  • 使用 artisan 命令。

php artisan pubsub:publish <message> <event>

订阅工作器。

  • php artisan pubsub:subscribe <connection>

或者,您也可以运行 php artisan queue:work <connection>

注意:为了使 queue:subscribe 进程永久在后台运行,您应该使用进程监视器(如 supervisor),以确保队列工作器不会停止运行。

使用 GCP Pub/Sub 模拟器。

您需要安装 GCP 命令行工具设置主题/订阅

要使用模拟器

  1. 转到 AppServiceProvider@register 并添加 PubSub::useEmulatorCredentials()

  2. 导出 pubsub 模拟器主机 export PUBSUB_EMULATOR_HOST=localhost:8085

  3. 运行模拟器,php artisan pubsub:emulator

测试。

您需要安装 GCP 命令行工具

  1. 运行 pubsub 模拟器./emulator.sh
  2. 导出 pubsub 模拟器主机 export PUBSUB_EMULATOR_HOST=localhost:8085
  3. 运行 phpunit