laravel\lumen rabbitmq

1、安装扩展
复制代码
composer require bschmitt/laravel-amqp
2、config文件下增加amqp.php配置文件
php 复制代码
<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Define which configuration should be used
    |--------------------------------------------------------------------------
    */

    'use' => env('AMQP_ENV', 'production'),

    /*
    |--------------------------------------------------------------------------
    | AMQP properties separated by key
    |--------------------------------------------------------------------------
    */

    'properties' => [

        'production' => [
            'host'                  => env('AMQP_HOST', 'localhost'),
            'port'                  => env('AMQP_PORT', 5672),
            'username'              => env('AMQP_USER', ''),
            'password'              => env('AMQP_PASSWORD', ''),
            'vhost'                 => env('AMQP_VHOST', '/'),
            'connect_options'       => [],
            'ssl_options'           => [],

            'exchange'              => env('AMQP_EXCHANGE', 'amq.topic'),
            'exchange_type'         => 'direct',
            'exchange_passive'      => false,
            'exchange_durable'      => true,
            'exchange_auto_delete'  => false,
            'exchange_internal'     => false,
            'exchange_nowait'       => false,
            'exchange_properties'   => [],

            'queue_force_declare'   => false,
            'queue_passive'         => false,
            'queue_durable'         => true,
            'queue_exclusive'       => false,
            'queue_auto_delete'     => false,
            'queue_nowait'          => false,
            'queue_properties'      => ['x-ha-policy' => ['S', 'all']],

            'consumer_tag'          => '',
            'consumer_no_local'     => false,
            'consumer_no_ack'       => false,
            'consumer_exclusive'    => false,
            'consumer_nowait'       => false,
            'consumer_properties'   => [],
            'timeout'               => 0,
            'persistent'            => false,
            'publish_timeout'       => 0, // Only applicable when a publish is marked as mandatory
            'qos'                   => false,
            'qos_prefetch_size'     => 0,
            'qos_prefetch_count'    => 1,
            'qos_a_global'          => false
        ],

    ],

];
3、app\Console\Commands文件下增加生产者消费者文件
生产者
php 复制代码
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Symfony\Component\Console\Input\InputOption;
use Db;
use Bschmitt\Amqp\Facades\Amqp;

class PublishAmqpCommand extends Command
{
    // 命令名
    protected $name = 'zj:publishamqp';
    // 描述
    protected $description = "publish msg to mq";
    /**
     * 命令的名称及其签名
     *
     * @var string
     */
    protected $signature = ' zj:publishamqp {msg}';

    // 执行
    public function handle()
    {
        Amqp::publish('', $this->argument('msg') , ['queue' => 'queue-name']);
    }
}
消费者
php 复制代码
<?php

namespace App\Console\Commands;

use App\Http\Controllers\SurveyController;
use http\Env;
use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Symfony\Component\Console\Input\InputOption;
use Bschmitt\Amqp\Facades\Amqp;

class ConsumeAmqpCommand extends Command
{
    // 命令名
    protected $name = 'zj:consumeamqp';
    // 描述
    protected $description = "consume msg from mq";

    // 执行
    public function handle()
    {
        $obj = $this;
        Amqp::consume('queue-name', function ($message, $resolver) use ($obj) {
            if($message->body){
                $data = json_decode($message->body,true);
                //处理数据
            }
            $obj->info($message->body);
            $resolver->acknowledge($message);
        }, [
            'routing' => 'exportdata',
            'persistent' => true, // required if you want to listen forever
        ]);
    }
}
注意:生产者和消费者的队列名称需要一致
4、bootstrap/app.php中增加
php 复制代码
$app->configure('amqp');
$app->register(Bschmitt\Amqp\LumenServiceProvider::class);
5、app/Console/Kernel.php
php 复制代码
<?php

namespace App\Console;

use App\Console\Commands\ConsumeAmqpCommand;
use App\Console\Commands\PublishAmqpCommand;
use Illuminate\Console\Scheduling\Schedule;
use Laravel\Lumen\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        //
        PublishAmqpCommand::class,
        ConsumeAmqpCommand::class
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        //
    }
}

执行消费者命令

php 复制代码
php artisan zj:consumeamqp

执行生产者命令:

php 复制代码
php artisan zj:publishamqp "hello mq"

注意:1、如果使用路由ConsumeAmqpCommand 文件中 增加routing 不需要删除即可

2、多消费者:需要几个消费者增加ConsumeAmqpComman文件

相关推荐
DigitalOcean9 天前
Laravel 开发者已在 DigitalOcean 上开通超过 10 万台服务器
前端·laravel
两个人的幸福11 天前
Windows 桌面应用自研 PHP 队列(下):完整代码与六大工程化优化
php
BingoGo13 天前
PHP 泛型之殇 泛型 RFC 提案被拒绝
后端·php
JaguarJack13 天前
PHP 泛型之殇 泛型 RFC 提案被拒绝
后端·php
用户30745969820714 天前
PHP 扩展——从入门到理解
php
鹏仔先生15 天前
拷贝漫画APP下载页PHP程序,后台带免费AI写作
php
云水一下15 天前
从零开始学 PHP 系列(一):PHP 的前世今生与开发环境搭建
开发语言·php
xingpanvip15 天前
星盘接口开发文档:本命盘接口指南
android·开发语言·css·php·lua
酉鬼女又兒15 天前
零基础入门计算机网络运输层:端到端通信核心作用、端口号分类规则、复用分用工作机制及UDP与TCP协议全方位对比详解
网络·网络协议·tcp/ip·计算机网络·考研·udp·php
dog25015 天前
不要再继续优化 TCP
网络协议·tcp/ip·php