Yii2.0 安裝yii2-queue並在Linux啟動守護進程監聽消息


一、什么是yii2-queue?

Yii2-queue是Yii2.0 PHP框架下一個消息隊列拓展插件,支持基於DB, Redis, RabbitMQ, AMQP, Beanstalk 和 Gearman等。yii2-queue GitHub地址:https://github.com/yiisoft/yii2-queue

二、如何安裝yii2-queue?

php composer.phar require --prefer-dist yiisoft/yii2-queue

三、Linux systemd介紹

systemd是linux下的一款系統和服務管理器,為什么要使用systemd ? 在rpm包二進制方式安裝的linux軟件中,使用init守護進程進行服務狀態的管理或者使用service命令 例如啟動Mysql數據庫可以是 /etc/init.d/mysql start 或者service mysql start.

使用linux init進程進行管理服務的時候有兩個缺點:

  1.init系統進程是串行執行的,也就是同步的 ,只有前一個進程啟動完成,才會啟動下一進程。

  2.啟動腳步復雜,init進程是只執行啟動腳步,不管其他的任務

使用Systemd優點:

  1.Systemd支持並行化任務,

  2.同時采用socket於D-Bus總線式激活服務,按需啟動守護進程(daemon)

在新版的Linux系統中都在使用sytemd 進行管理 例如(Ubuntu 16、Debian 8、CentOS 7)

四、在Linux創建systemd系統服務監聽隊列消息

1、在Linux下進入system目錄,相關命令:cd /etc/systemd/system;

2、新增yii-queue@.service文件,相關命令:vi yii-queue@.service;

3、yii-queue@.service文件內容如下:

[Unit]
Description=Yii Queue Worker %I
After=network.target
# the following two lines only apply if your queue backend is mysql
# replace this with the service that powers your backend
After=mysql.service
Requires=mysql.service

[Service]
User=www-data
Group=www-data
ExecStart=/usr/bin/php /var/www/my_project/yii queue/listen --verbose
Restart=on-failure

[Install]
WantedBy=multi-user.target

4、重載systemd配置文件使其生效,相關命令:systemctl daemon-reload;

5、其他相關命令:

# To start two workers
systemctl start yii-queue@1 yii-queue@2

# To get the status of running workers
systemctl status "yii-queue@*"

# To stop a specific worker
systemctl stop yii-queue@2

# To stop all running workers
systemctl stop "yii-queue@*"

# To start two workers at system boot
systemctl enable yii-queue@1 yii-queue@2

 


免責聲明!

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



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