Laravel6 使用RabbitMQ


RabbitMQ For Laravel

Unix安裝

安裝rabbitmq-c 0.10.0

brew install rabbitmq-c
or
yum install rabbitmq-c
or 
github自行源碼cmake安裝

安裝 php amqp擴展1.10.2

pecl install amqp

推薦自行編譯安裝

wget http://pecl.php.net/get/amqp-1.10.2.tgz
phpize

--with-librabbitmq-dir這個需要修改為你環境rabbitmq-c的安裝地址,brew 安裝成功是最后會出現此安裝目錄信息。 --with-php-config這個配置為你環境的php-config地址

./configure --with-php-config=/usr/local/opt/php@7.4/bin/php-config -with-amqp --with-librabbitmq-dir=/usr/local/Cellar/rabbitmq-c/0.10.0 make && make install 添加php.ini文件。 

安裝rabbitmq(docker)

我們選擇帶管理面板的版本,方便學習。

docker pull rabbitmq:3.8.9-management

docker run -d --name rabbitmq3.8.9 -p 5672:5672 -p 15672:15672 --privileged=true  -v `pwd`/data:/var/lib/rabbitmq --hostname myRabbit -e RABBITMQ_DEFAULT_VHOST=my_vhost  -e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=admin -e RABBITMQ_ERLANG_COOKIE='rabbitcookie' rabbitmq:3.8.9-management

通過http://127.0.0.1:15672/訪問管理面板,賬密admin。5672是服務端口。

安裝composer依賴包

composer require vladimir-yuldashev/laravel-queue-rabbitmq

在config/app.php的providers中添加

VladimirYuldashev\LaravelQueueRabbitMQ\LaravelQueueRabbitMQServiceProvider::class,

在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', 'admin'),
                    'password' => env('RABBITMQ_PASSWORD', 'admin'),
                    'vhost' => env('RABBITMQ_VHOST', 'my_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'),

        ],

如果worker配置成horizon,則可以搭配horizon使用。

本地測試

php artisan queue:work rabbitmq

性能測試

我們從兩個方向測試,1.直接sql寫庫,2.rabbitmq消息隊列異步寫庫

class QueueController extends Controller
{
    public function index()
    {
        $this->dispatch(new \App\Jobs\RabbitmqQueue());

        return 'success';
    }

    public function insert()
    {
        $user = new \App\Model\UserModel();

        $user->username = rand(100000, 999999);

        $user->save();

        return 'success';
    }
}

class RabbitmqQueue implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $user = new \App\Model\UserModel();

        $user->username = rand(100000, 999999);

        $user->save();
    }
}
ab QPS1000並發測試
環境
  • php:7.4
  • nginx:1.13
  • mysql:5.7 (本地)
直接寫庫(Requests per second: 86.10 #/sec)
ab -n 1000 -c 100  http://127.0.0.1:8239/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 127.0.0.1 (be patient) Completed 100 requests Completed 200 requests Completed 300 requests Completed 400 requests Completed 500 requests Completed 600 requests Completed 700 requests Completed 800 requests Completed 900 requests Completed 1000 requests Finished 1000 requests Server Software: nginx/1.13.9 Server Hostname: 127.0.0.1 Server Port: 8239 Document Path: /api/insert Document Length: 7 bytes Concurrency Level: 100 Time taken for tests: 11.615 seconds Complete requests: 1000 Failed requests: 0 Total transferred: 293929 bytes HTML transferred: 7000 bytes Requests per second: 86.10 [#/sec] (mean) Time per request: 1161.486 [ms] (mean) Time per request: 11.615 [ms] (mean, across all concurrent requests) Transfer rate: 24.71 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 1.0 0 5 Processing: 38 1101 199.8 1127 1324 Waiting: 38 1101 199.8 1127 1324 Total: 43 1101 198.9 1127 1324 Percentage of the requests served within a certain time (ms) 50% 1127 66% 1183 75% 1205 80% 1216 90% 1248 95% 1275 98% 1294 99% 1303 100% 1324 (longest request) 
消息隊列處理(Requests per second: 172.90 #/sec)
ab -n 1000 -c 100  http://127.0.0.1:8239/api/queue 
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 127.0.0.1 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        nginx/1.13.9
Server Hostname:        127.0.0.1
Server Port:            8239

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

Concurrency Level:      100
Time taken for tests:   5.784 seconds
Complete requests:      1000
Failed requests:        967
   (Connect: 0, Receive: 0, Length: 967, Exceptions: 0)
Non-2xx responses:      967
Total transferred:      1854694 bytes
HTML transferred:       1516487 bytes
Requests per second:    172.90 [#/sec] (mean)
Time per request:       578.379 [ms] (mean)
Time per request:       5.784 [ms] (mean, across all concurrent requests)
Transfer rate:          313.16 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   1.2      0       6
Processing:    41  546 167.0    485    1045
Waiting:       35  546 167.0    485    1044
Total:         41  547 166.9    485    1045

Percentage of the requests served within a certain time (ms)
  50%    485
  66%    548
  75%    591
  80%    633
  90%    798
  95%    998
  98%   1014
  99%   1022
 100%   1045 (longest request)

結論

從測試結果來看性能是翻倍的。可能有同事會問,原先我們使用的Redis隊列和RabbitMQ相比,Redis隊列的性能瓶頸主要是在入隊列的時候,大數據量入隊列Redis隊列性能急劇下降。再加上RabbitMQ分布式,交換機等特性能從使用上更便捷,性能更強。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM