承接 anik/laravel-amqp 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

anik/laravel-amqp

Composer 安装命令:

composer require anik/laravel-amqp

包简介

anik/amqp wrapper for Laravel-ish frameworks

README 文档

README

anik/laravel-amqp codecov PHP Version Require Latest Stable Version Total Downloads Jetbrains

anik/amqp wrapper for Laravel-ish frameworks.

Examples

Checkout the repository for example.

Documentation

Installation

To install the package, run

composer require anik/laravel-amqp

Laravel

The Anik\Laravel\Amqp\Providers\AmqpServiceProvider::class service provider should automatically get registered. If not, then you can manually add the service provider in your config/app.php providers array:

'providers' => [
    // ... 
    Anik\Laravel\Amqp\Providers\AmqpServiceProvider::class,
]
  • Publish configuration with php artisan vendor:publish --provider "Anik\Laravel\Amqp\Providers\AmqpServiceProvider" command.

Lumen

  • Register Anik\Laravel\Amqp\Providers\AmqpServiceProvider::class service provider in your bootstrap/app.php file.
$app->register(Anik\Laravel\Amqp\Providers\AmqpServiceProvider::class);
  • Copy configuration amqp.php in your config directory from vendor/anik/laravel-amqp/src/config/amqp.php.

  • Import your configuration using $app->configure('amqp'); in your bootstrap/app.php.

Laravel Zero

  • Register Anik\Laravel\Amqp\Providers\AmqpServiceProvider::class service provider in your config/app.php providers array:
'providers' => [
    /// ... 
    Anik\Laravel\Amqp\Providers\AmqpServiceProvider::class,
]
  • Copy configuration amqp.php in your config directory from vendor/anik/laravel-amqp/src/config/amqp.php.

Configuration

In your config/amqp.php, you can define multiple connections and use them from your code by pointing the connection name.

  • amqp.default denoting the default connection. Will be used if no connection is specified when producing or consuming messages.
  • amqp.connections.*.connection.class denoting the underlying Amqp connection to be used. By default, it uses lazy connection. You can change it to any implementation of PhpAmqpLib\Connection\AbstractConnection.
  • amqp.connections.*.connection.hosts can have multiple host configuration. Each host config must contain host , port, user, password keys. It can also contain vhost which is optional. Lazy connections cannot have more than one host configuration otherwise it'll throw error.
  • You can also pass optional array of parameters through amqp.connections.*.connection.options when creating an instance of amqp.connections.*.connection.class internally.
  • amqp.connections.*.message holds the default properties of a message when publishing.
  • amqp.connections.*.exchange holds the default properties of your exchange when publishing & consuming.
  • amqp.connections.*.queue holds the default properties of your queue when consuming.
  • amqp.connections.*.consumer holds the default properties of consumer when consuming.
  • amqp.connections.*.qos holds the default properties of QoS when consuming.

Octane support

This package supports laravel octane by default. To keep the AMQP connection alive, you have to configure octane to warm the connection, by adding 'amqp' to the warm array in octane configurations.

// config/octane.php
// ... 
'warm' => [
    // ... 
    'amqp', // <-- this line
],

Usage

The followings work the same.

use Anik\Amqp\ConsumableMessage;
use Anik\Laravel\Amqp\Facades\Amqp;

$messages = 'my message';
// $messages = ['my first message', 'my second message'];
// $messages = new Anik\Amqp\ProducibleMessage('my message');
// $messages = ['another message', new Anik\Amqp\ProducibleMessage('also another message')];

Amqp::publish($messages); // publishes to default connection
Amqp::connection('rabbitmq')->publish($messages); // publishes to rabbitmq connection

app('amqp')->publish($messages); // publishes to default connection
app('amqp')->connection('rabbitmq')->publish($messages); // publishes to rabbitmq connection

app()->make('amqp')->publish($messages); // publishes to default connection
app()->make('amqp')->connection('rabbitmq')->publish($messages); // publishes to rabbitmq connection

/** @var \Anik\Laravel\Amqp\AmqpManager $amqpManager */
$amqpManager->publish($messages); // publishes to default connection
$amqpManager->connection('rabbitmq')->publish($messages); // publishes to rabbitmq connection


Amqp::consume(function(ConsumableMessage $message) {
    var_dump($message->getMessageBody());
    $message->ack();
}); // consumes from default connection
Amqp::connection('rabbitmq')->consume(function(ConsumableMessage $message) {
    var_dump($message->getMessageBody());
    $message->ack();
}); // consumes from rabbitmq connection

app('amqp')->consume(function(ConsumableMessage $message) {
    var_dump($message->getMessageBody());
    $message->ack();
}); // consumes from default connection
app('amqp')->connection('rabbitmq')->consume(function(ConsumableMessage $message) {
    var_dump($message->getMessageBody());
    $message->ack();
}); // consumes from rabbitmq connection

app()->make('amqp')->consume(function(ConsumableMessage $message) {
    var_dump($message->getMessageBody());
    $message->ack();
}); // consumes from default connection
app()->make('amqp')->connection('rabbitmq')->consume(function(ConsumableMessage $message) {
    var_dump($message->getMessageBody());
    $message->ack();
}); // consumes from rabbitmq connection

/** @var \Anik\Laravel\Amqp\AmqpManager $amqpManager */
$amqpManager->consume(function(ConsumableMessage $message) {
    var_dump($message->getMessageBody());
    $message->ack();
}); // consumes from default connection
$amqpManager->connection('rabbitmq')->consume(function(ConsumableMessage $message) {
    var_dump($message->getMessageBody());
    $message->ack();
}); // consumes from rabbitmq connection

Note

In this documentation, it'll use FACADE afterwards. If you're using Lumen, then you can use other approaches. The package doesn't require enabling Facade.

Publishing messages

To publish messages,

use Anik\Laravel\Amqp\Facades\Amqp;

Amqp::publish($messages, $routingKey, $exchange, $options);
Amqp::connection('rabbitmq')->publish($messages, $routingKey, $exchange, $options);
  • $messages Type: mixed. Required. It can be a single message, or an array of messages of any scalar type or implementation of Anik\Amqp\Producible.
  • $routingKey Type: string. Optional. Default: '' (empty string).
  • $exchange Type: null | Anik\Amqp\Exchanges\Exchange. Optional. Default: null.
  • $options Type: array. Optional. Default: [].
    • Key message - Accepts: array. Valid properties for PhpAmqpLib\Message\AMQPMessage.
    • Key exchange - Accepts: array. Refer to amqp.connections.*.exchange.
    • Key publish - Accepts: array. Refer to Anik\Amqp\Producer::publishBatch

Note

  • If any of the $messages is not an implementation of Anik\Amqp\Producible, then that message will be converted to Anik\Amqp\Producible using Anik\Amqp\ProducibleMessage.
  • When converting to Anik\Amqp\Producible, it'll try to use $options['message'] as the message property. If not set, it'll then try to use amqp.connections.*.message properties if available.
  • If $exchange is set to null, it'll check if $options['exchange'] is set or not. If not set, it'll then use amqp.connections.*.exchange properties if available.
  • If $options['publish'] is not set, it'll try to use amqp.connections.*.publish properties if available.

Consuming messages

To consume messages,

use Anik\Laravel\Amqp\Facades\Amqp;

Amqp::consume($handler, $bindingKey, $exchange, $queue, $qos , $options);
Amqp::connection('rabbitmq')->consume($handler, $bindingKey, $exchange, $queue, $qos , $options);
  • $handler Type: callable | Anik\Amqp\Consumable. Required.
  • $bindingKey Type: string. Optional. Default: '' (empty string).
  • $exchange Type: null | Anik\Amqp\Exchanges\Exchange. Optional. Default: null.
  • $queue Type: null | Anik\Amqp\Queues\Queue. Optional. Default: null.
  • $qos Type: null | Anik\Amqp\Qos\Qos. Optional. Default: null.
  • $options Type: array. Optional. Default: [].
    • Key exchange - Accepts: array. Refer to amqp.connections.*.exchange.
    • Key queue - Accepts: array. Refer to amqp.connections.*.queue.
    • Key qos - Accepts: array. Refer to amqp.connections.*.qos.
    • Key consumer - Accepts: array. Refer to amqp.connections.*.consumer.
    • Key bind - Accepts: array. Refer to Anik\Amqp\Consumer::consume

Note

  • If $handler is not an implementation of Anik\Amqp\Consumable, then the handler will be converted to Anik\Amqp\Consumable using Anik\Amqp\ConsumableMessage.
  • If $exchange is set to null, it'll check if $options['exchange'] is set or not. If not set, it'll then use amqp.connections.*.exchange properties if available.
  • If $queue is set to null, it'll check if $options['queue'] is set or not. If not set, it'll then use amqp.connections.*.queue properties if available.
  • If $qos is set to null, it'll check if $options['qos'] is set or not. If not set, it'll then use amqp.connections.*.qos properties if amqp.connections.*.qos.enabled is set to a truthy value.
  • If $options['bind'] is not set, it'll use amqp.connections.*.bind properties if available.
  • If $options['consumer'] is not set, it'll use amqp.connections.*.consumer properties if available.

Testing

The package allows asserting a few scenarios. Before you can run those assertions, you'll need use Amqp::fake().

<?php

use Anik\Laravel\Amqp\Facades\Amqp;
use PHPUnit\Framework\TestCase;

class MyTest extends TestCase 
{
    public function testIfMessageWasProduced () {
        Amqp::fake();
        // ... Your code
        
        Amqp::assertPublished();
        // Amqp::assertPublished("my-message");
        // Amqp::assertPublishedCount(5, "my-message");
        // Amqp::assertPublished(Anik\Amqp\ProducibleMessage::class);
        // Amqp::assertPublished(Anik\Amqp\Producible::class);
        Amqp::assertPublishedOnConnection('rabbitmq');
    }
}
  • Anik\Laravel\Amqp\Facades\Amqp::assertPublishedOnConnection(string $name) - To check if at least one message was published on the connection $name.
  • Anik\Laravel\Amqp\Facades\Amqp::assertPublishedOnExchange(string $name) - To check if at least one message was published on exchange $name.
  • Anik\Laravel\Amqp\Facades\Amqp::assertPublishedOnExchangeType(string $type) - To check if at least one message was published on exchange type $type.
  • Anik\Laravel\Amqp\Facades\Amqp::assertPublishedWithRoutingKey(string $key) - To check if at least one message was published with routing key $key.
  • Anik\Laravel\Amqp\Facades\Amqp::assertPublished($message = null)
    • If $message is null, it will check if at least one message was published.
    • Otherwise, checks in the following order.
      • If a message exactly matches the $message.
      • If a message exactly matches the get_class($message).
      • If a message is an implementation of $message.
  • Anik\Laravel\Amqp\Facades\Amqp::assertNotPublished($message = null)
    • If $message is null, it will check if no message was published.
    • Otherwise, checks in the following order.
      • No message was published that exactly matches the $message.
      • No message was published that exactly matches the get_class($message).
      • No message was published that is an implementation of $message.
  • Anik\Laravel\Amqp\Facades\Amqp::assertPublishedCount(int $count, $message = null)
    • If $message is null, it will check if exactly $count messages have been published.
    • Otherwise, checks in the following order.
      • If a message exactly matches the $message.
      • If a message exactly matches the get_class($message).
      • If a message is an implementation of $message.

Note

Using Anik\Laravel\Amqp\Facades\Amqp::consume() after Anik\Laravel\Amqp\Facades\Amqp::fake() will throw exception.

anik/laravel-amqp 适用场景与选型建议

anik/laravel-amqp 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 200.55k 次下载、GitHub Stars 达 33, 最近一次更新时间为 2021 年 11 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 anik/laravel-amqp 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 200.55k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 34
  • 点击次数: 18
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 33
  • Watchers: 2
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-11-12