承接 filld/larabbit 相关项目开发

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

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

filld/larabbit

Composer 安装命令:

composer require filld/larabbit

包简介

AMQP wrapper for Laravel and Lumen to publish and consume messages

README 文档

README

AMQP wrapper for Laravel and Lumen to publish and consume messages especially from RabbitMQ

Features

  • Advanced queue configuration
  • Add message to queues easily
  • Listen queues with useful options

Installation

Composer

Add the following to your require part within the composer.json:

"filld/larabbit": "2.*"
$ php composer update

or

$ php composer require filld/larabbit

Integration

Environment Variables

The following Env vars need to be set in your .env file:

RABBITMQ_HOST
RABBITMQ_PORT
RABBITMQ_USERNAME
RABBITMQ_PASSWORD
RABBITMQ_VHOST

# Optional SSL:
RABBITMQ_SSL_CERT_PATH

Lumen

Create a config folder in the root directory of your Lumen application and copy the content from vendor/filld/larabbit/config/amqp.php to config/amqp.php.

Adjust the properties to your needs.

return [

    'use' => 'production',

    'properties' => [

        'production' => [
            'host'                => env('RABBITMQ_HOST', 'localhost'),
            'port'                => env('RABBITMQ_PORT', 5672),
            'username'            => env('RABBITMQ_USERNAME'),
            'password'            => env('RABBITMQ_PASSWORD'),
            'vhost'               => env('RABBITMQ_VHOST'),
            'exchanges'           => [
                                         [
                                             'exchange'              => 'exchange_name',
                                             'exchange_type'         => 'topic',
                                             'exchange_passive'      => false,
                                             'exchange_durable'      => true,
                                             'exchange_auto_delete'  => false,
                                             'exchange_internal'     => false,
                                             'exchange_nowait'       => false,
                                             'exchange_properties'   => [],
                         
                                             'routing' => [
                                                 'routing.key.one',
                                                 'routing.key.two',
                                         ]
                                     ],
            'consumer_tag'        => 'consumer',
            'ssl_options'         => [], // See https://secure.php.net/manual/en/context.ssl.php
            'connect_options'     => [], // See https://github.com/php-amqplib/php-amqplib/blob/master/PhpAmqpLib/Connection/AMQPSSLConnection.php
            'queue_properties'    => ['x-ha-policy' => ['S', 'all']],
            'timeout'             => 0
        ],

    ],

];

Register the Lumen Service Provider in bootstrap/app.php:

/*
|--------------------------------------------------------------------------
| Register Service Providers
|--------------------------------------------------------------------------
*/

//...

$app->configure('amqp');
$app->register(Filld\Amqp\LumenServiceProvider::class);

//...

Enable Facade Support for Lumen 5.2+

$app->withFacades();

Laravel

Open config/app.php and add the service provider and alias:

'Filld\Amqp\AmqpServiceProvider',
'Amqp' => 'Filld\Amqp\Facades\Amqp',

Setting up SSL

Make sure you copy the CA cert chain to somewhere that you can reference. The storage/certs directory is suggested. Also, don't forget to set the SSL settings in the config:

'connect_options'     => [
    'capath' => '/etc/ssl/certs',
    'cafile' => env('RABBITMQ_SSL_CERT', storage_path() . '/certs/cacert.pem'),
    'verify_peer' => true
],

Also, don't forget that the port likely changed to 5671

Publishing a message

Push message with routing key

    Amqp::publish('routing-key', 'message');

Push message with routing key and create queue

    Amqp::publish('routing-key', 'message' , ['queue' => 'queue-name']);

Push message with routing key and overwrite properties

    Amqp::publish('routing-key', 'message' , ['exchange' => 'amq.direct']);

Notice that if you attempt to set properties like: x-message-ttl you may get an error like the following:

AMQP-rabbit doesn't define data of type []

You must specify a type:

    Amqp::publish('routing-key', 'message', [
        'queue_properties' => [
            "x-ha-policy" => ["S", "all"],
            'x-message-ttl' => ['I', 86400000],
            'x-dead-letter-exchange' => ['S', 'orders_dead_letter'],
        ],
        'queue' => 'orders'
    ]);

Consuming messages

Consume messages, acknowledge and stop when no message is left

Amqp::consume('queue-name', function ($message, $resolver) {
    		
   var_dump($message->body);

   $resolver->acknowledge($message);

   $resolver->stopWhenProcessed();
        
});

Consume messages forever

Amqp::consume('queue-name', function ($message, $resolver) {
    		
   var_dump($message->body);

   $resolver->acknowledge($message);
        
});

Consume messages, with custom settings

Amqp::consume('queue-name', function ($message, $resolver) {
    		
   var_dump($message->body);

   $resolver->acknowledge($message);
      
}, [
	'timeout' => 2,
	'vhost'   => 'vhost3'
]);

Fanout example

Publishing a message

\Amqp::publish('', 'message' , [
    'exchange_type' => 'fanout',
    'exchange' => 'amq.fanout',
]);

Consuming messages

\Amqp::consume('', function ($message, $resolver) {
    var_dump($message->body);
    $resolver->acknowledge($message);
}, [
    'exchange' => 'amq.fanout',
    'exchange_type' => 'fanout',
    'queue_force_declare' => true,
    'queue_exclusive' => true,
    'persistent' => true// required if you want to listen forever
]);

Credits

License

This package is open-sourced software licensed under the MIT license

filld/larabbit 适用场景与选型建议

filld/larabbit 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.54k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2017 年 08 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「queue」 「package」 「rabbitmq」 「AMQP」 「laravel」 「message queue」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 filld/larabbit 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 7.54k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 23
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 10
  • Forks: 85
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-08-08