alesima/laravel-azure-service-bus
Composer 安装命令:
composer require alesima/laravel-azure-service-bus
包简介
A Laravel Queue driver for Azure Service Bus
关键字:
README 文档
README
Integrate Azure Service Bus as a queue driver and Pub/Sub module in Laravel, now with support for multiple topics.
This package provides a custom queue driver for Laravel that integrates with Azure Service Bus and adds support for Topics and Subscriptions (Pub/Sub), enabling both queue-based and publish/subscribe messaging models with the ability to manage multiple topics dynamically.
Features 🎯
- Azure Service Bus Integration: Seamlessly integrate Azure's messaging capabilities into your Laravel application.
- Support for Laravel 5.x - 8.x: Compatible with older Laravel versions and PHP 7.2 and above.
- Queue Operations: Push, pop, and manage jobs in Azure Service Bus queues with ease.
- Pub/Sub Module: Publish messages to multiple topics and subscribe to them using Azure Service Bus topics and subscriptions.
- Job Scheduling: Supports delayed jobs using
later()with multiple formats (e.g.,DateTime,DateInterval,int). - Built with Laravel's Queuing System: Follows the same conventions, making it easy to work with.
Installation ⚙️
1. Install the package via Composer:
composer require alesima/laravel-azure-service-bus
2. Publish the configuration:
After installing, publish the configuration file to adjust your Azure Service Bus settings.
php artisan vendor:publish --provider="LaravelAzureServiceBus\Providers\ServiceProvider" --tag=config
3. Configure .env file:
In your .env file, set the Azure Service Bus connection details:
SERVICE_BUS_NAMESPACE=https://<your-namespace> SERVICE_BUS_SHARED_ACCESS_KEY_NAME=<your-key-name> SERVICE_BUS_SHARED_ACCESS_KEY=<your-key>
4. Define Azure Service Bus Configuration
Update your config/queue.php file to include the following configuration for the Azure Service Bus driver:
'connections' => [ // Other connections... 'azureservicebus' => [ 'driver' => 'azureservicebus', 'endpoint' => sprintf('https://%s.servicebus.windows.net/', env('SERVICE_BUS_NAMESPACE')), 'shared_access_key_name' => env('SERVICE_BUS_SHARED_ACCESS_KEY_NAME'), 'shared_access_key' => env('SERVICE_BUS_SHARED_ACCESS_KEY'), 'queue' => 'default', 'UseTopic' => false, ], ],
5. Register the service provider and the queue manager (optional):
If you are using Lumen, you need to register the service provider in bootstrap/app.php:
$app->register(Alesima\LaravelAzureServiceBus\Providers\ServiceProvider::class);
Also you might to register the queue manager in config/app.php:
$app->bind(Illuminate\Queue\QueueManager::class, function ($app) { return $app['queue']; });
Local Development & Testing with Docker 🐳
For local development and testing, you can use the official PHP 7.4 Docker image to ensure compatibility.
Run the following command to install dependencies using Docker:
docker run --rm \
-v $(pwd):/app \
-w /app \
php:7.4-cli \
bash -c "apt-get update && apt-get install -y zip unzip git && curl -sS https://getcomposer.org/installer | php && php composer.phar install"
How to test:
docker run --rm \
-v $(pwd):/app \
-w /app \
php:7.4-cli \
bash -c "apt-get update && apt-get install -y zip unzip git && curl -sS https://getcomposer.org/installer | php && php composer.phar install && vendor/bin/phpunit --no-coverage"
Usage 🛠️
Queue Operations
Push Jobs onto the Queue ⬆️
You can push jobs to Azure Service Bus using the standard Laravel syntax:
use App\Jobs\MyJob; dispatch(new MyJob($data)); // Push to the default queue
Use later() for Delayed Jobs ⏳
You can schedule jobs to be pushed after a delay using various formats:
dispatch((new MyJob($data))->delay(60)); // Delay by 60 seconds $interval = new \DateInterval('PT10M'); // 10 minutes dispatch((new MyJob($data))->delay($interval)); $releaseTime = new \DateTime('+1 hour'); dispatch((new MyJob($data))->delay($releaseTime));
Handle Jobs 🚀
When a job is received from the queue, it will be processed as a standard Laravel job:
public function handle() { // Your job logic here }
Pub/Sub Module
The Pub/Sub module enables publishing messages to Azure Service Bus topics and receiving them from subscriptions. This now supports managing multiple topics dynamically.
Publish Messages to a Topic 📢
You can publish a message to a specific topic:
use LaravelAzureServiceBus\Services\AzurePubSubService; $pubSub = app(AzurePubSubService::class); // Publish to a specific topic $pubSub->publishMessage('topic1', [ 'event' => 'user.created', 'data' => ['user_id' => 123], ]);
Subscribe to a Specific Topic 🔔
To retrieve messages from a subscription under a specific topic:
use LaravelAzureServiceBus\Services\AzurePubSubService; $pubSub = app(AzurePubSubService::class); // Subscribe to messages from 'topic1' $messages = $pubSub->retrieveMessages('topic1', 'subscription1'); foreach ($messages as $message) { echo $message; // Process the message }
Retrieve Messages from Multiple Topics 🔄
To work with multiple topics dynamically:
use LaravelAzureServiceBus\Services\AzurePubSubService; $pubSub = app(AzurePubSubService::class); // Retrieve messages from multiple topics $topics = ['topic1', 'topic2', 'topic3']; foreach ($topics as $topic) { $messages = $pubSub->retrieveMessages($topic, 'subscription1'); foreach ($messages as $message) { echo "From {$topic}: " . $message; } }
Compatibility 🧩
This package is compatible with:
- Laravel: 5.x, 6.x, 7.x, and 8.x.
- PHP: 7.2 to 7.4.
Testing ⚡️
You can run tests using PHPUnit:
vendor/bin/phpunit
License 📜
This package is licensed under the MIT License. See LICENSE for more information.
Contributing 🤝
We welcome contributions to make this package even better!
- Fork the repository.
- Create a new branch.
- Make your changes and commit them.
- Open a pull request.
Credits 🏆
- Azure SDK for PHP: Provides the integration with Azure Service Bus.
- Laravel: The PHP framework that powers this package.
Attribution
Inspired by https://github.com/goavega-software/laravel-azure-servicebus-topic & https://github.com/pawprintdigital/laravel-queue-raw-sqs
Contact 📬
For any questions, feel free to reach out to us via GitHub Issues or email us at alex@codingwithalex.com.
alesima/laravel-azure-service-bus 适用场景与选型建议
alesima/laravel-azure-service-bus 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 654 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 12 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「queue」 「azure」 「laravel」 「servicebus」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 alesima/laravel-azure-service-bus 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 alesima/laravel-azure-service-bus 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 alesima/laravel-azure-service-bus 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Small library to access Microsoft Windows Azure Blob Storage with a Service or a StreamWrapper.
PHP AMQP Binding Library
A Laravel package to monitor queue jobs.
Adds Silverstripe Flysystem support for using the Azure Blob adapter
Laravel 5 Queue Driver for Microsoft Azure Storage Queue
Laravel Azure storage service provider
统计信息
- 总下载量: 654
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-12-03