brew install rabbitmq-c
or
yum install rabbitmq-c
or
github自行源码cmake安装
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文件。
我们选择带管理面板的版本,方便学习。
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 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 -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)
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分布式,交换机等特性能从使用上更便捷,性能更强。
原文:https://www.cnblogs.com/Ethen/p/14011897.html