laravel6使用RabbitMQ作为消息中间件

安装amqp扩展

确保已安装rabbitmq-c-dev。我是alpine镜像下的使用apk add rabbmit-c-dev安装

cd ~
wget http://pecl.php.net/get/amqp-1.10.2.tgz
tar -zxf amqp-1.10.2.tgz
cd amqp-1.10.2
phpize
./configure
make && make install
cd ~
rm -rf amqp-1.10.2*

重启php,php -m查看是否成功安装amqp。

laravel安装vladimir-yuldashev/laravel-queue-rabbitmq

vladimir-yuldashev/laravel-queue-rabbitmq选择对应laravel的版本进行安装

安装vladimir-yuldashev/laravel-queue-rabbitmq

composer require vladimir-yuldashev/laravel-queue-rabbitmq

这里需确保所有的依赖已安装。查看地址

修改config/queue.php文件在connections中追加

    'rabbitmq' => [

       'driver' => 'rabbitmq',
       'queue' => env('RABBITMQ_QUEUE', 'default'),
       'connection' => PhpAmqpLib\Connection\AMQPLazyConnection::class,

       'hosts' => [
           [
               'host' => env('RABBITMQ_HOST', '127.0.0.1'),
               'port' => env('RABBITMQ_PORT', 5672),
               'user' => env('RABBITMQ_USER', 'guest'),
               'password' => env('RABBITMQ_PASSWORD', 'guest'),
               'vhost' => env('RABBITMQ_VHOST', '/'),
           ],
       ],

       'options' => [
           'ssl_options' => [
               'cafile' => env('RABBITMQ_SSL_CAFILE', null),
               'local_cert' => env('RABBITMQ_SSL_LOCALCERT', null),
               'local_key' => env('RABBITMQ_SSL_LOCALKEY', null),
               'verify_peer' => env('RABBITMQ_SSL_VERIFY_PEER', true),
               'passphrase' => env('RABBITMQ_SSL_PASSPHRASE', null),
           ],
       ],

       /*
        * Set to "horizon" if you wish to use Laravel Horizon.
        */
       'worker' => env('RABBITMQ_WORKER', 'default'),

    ],

编写示例

mysql准备user表只有,user_id,user_name

创建任务类php artisan make:job Queue

insert(['user_name' => rand(100000, 999999)]);
        print_r('success');
    }
}

创建控制器php artisan make:controller Queue。添加index方法

dispatch(new JobsQueue($request));
        return 'success';
    }

    /**
     * 直接插入数据
     *
     * @return string
     */
    public function insert()
    {
        sleep(2);
        $user = new User();
        $user->insert(['user_name' => rand(100000, 999999)]);
        return 'success';
    }
}

添加api路由

Route::get('queue', 'Queue@index');
Route::get('index', 'Queue@insert');

执行命令进行消费php artisan queue:work。访问http://base6.com/api/queue

/var/www/html/blog6 # php artisan queue:work rabbitmq
[2020-05-24 08:55:40][Sx4qYuD8jtTWmEB1IIkPJEviWliUIlNr] Processing: App\Jobs\Queue
sucess[2020-05-24 08:55:43][Sx4qYuD8jtTWmEB1IIkPJEviWliUIlNr] Processed:  App\Jobs\Queue

使用ab测试50请求10并发下的表现

➜  php ab -n50 -c10  http://base6.com/api/index
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking base6.com (be patient).....done


Server Software:        nginx/1.16.0
Server Hostname:        base6.com
Server Port:            80

Document Path:          /api/index
Document Length:        1552 bytes

Concurrency Level:      10
Time taken for tests:   65.539 seconds
Complete requests:      50
Failed requests:        0
Non-2xx responses:      50
Total transferred:      87750 bytes
HTML transferred:       77600 bytes
Requests per second:    0.76 [#/sec] (mean)
Time per request:       13107.809 [ms] (mean)
Time per request:       1310.781 [ms] (mean, across all concurrent requests)
Transfer rate:          1.31 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       1
Processing:  1524 12221 3036.0  12793   16753
Waiting:     1513 12219 3036.6  12792   16752
Total:       1524 12221 3036.0  12793   16753

Percentage of the requests served within a certain time (ms)
  50%  12793
  66%  13870
  75%  14549
  80%  14649
  90%  15053
  95%  15838
  98%  16753
  99%  16753
 100%  16753 (longest request)

➜  php ab -n50 -c10  http://base6.com/api/insert
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking base6.com (be patient).....done


Server Software:        nginx/1.16.0
Server Hostname:        base6.com
Server Port:            80

Document Path:          /api/insert
Document Length:        7 bytes

Concurrency Level:      10
Time taken for tests:   84.344 seconds
Complete requests:      50
Failed requests:        0
Total transferred:      14550 bytes
HTML transferred:       350 bytes
Requests per second:    0.59 [#/sec] (mean)
Time per request:       16868.876 [ms] (mean)
Time per request:       1686.888 [ms] (mean, across all concurrent requests)
Transfer rate:          0.17 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       1
Processing:  3929 14874 3234.5  16209   17922
Waiting:     3916 14873 3235.3  16208   17921
Total:       3929 14874 3234.4  16210   17922

Percentage of the requests served within a certain time (ms)
  50%  16210
  66%  16522
  75%  16762
  80%  17010
  90%  17692
  95%  17772
  98%  17922
  99%  17922
 100%  17922 (longest request)

因为本机配置有限。直接操作和使用队列都增加2秒的延迟,用于比交队列的使用效果

你可能感兴趣的:(laravel6使用RabbitMQ作为消息中间件)