laravel\lumen rabbitmq

1、安装扩展
composer require bschmitt/laravel-amqp
2、config文件下增加amqp.php配置文件
 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文件下增加生产者消费者文件
 生产者
argument('msg') , ['queue' => 'queue-name']);
    }
}
消费者
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中增加
$app->configure('amqp');
$app->register(Bschmitt\Amqp\LumenServiceProvider::class);
 5、app/Console/Kernel.php

执行消费者命令

php artisan zj:consumeamqp

执行生产者命令:

php artisan zj:publishamqp "hello mq"

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

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

你可能感兴趣的:(laravel,php)