alan/yii2-mq-task

There is no license information available for the latest version (v3.0.4) of this package.

yii2 framework mq task v2.0

v3.0.4 2022-05-14 14:18 UTC

This package is auto-updated.

Last update: 2022-07-14 14:40:44 UTC


README

  1. 特性

  2. 安装

    composer require alan/yii2-mq-task:dev-master

  3. 配置

    1.0.x有消费者卡死的Bug, 解决的办法是升级了swoole到4.5.*的版本,使用Pool代替原来的swoole_service。

    关于1.0.0升级到2.0的配置的变更,由于2.x采用Yii2框架自身的日志组件又要在消费任务的时候刷新LogId, 所以需要替换文件类,配置层级 composents => logs => targets => class: yii2\mq_task\components\FileTarget

    • 添加配置

      'db' => [ //默认db配置
          'class' => 'yii\db\Connection',
          'dsn' => 'mysql:host=host;port=port;dbname=dbname',
          'username' => 'username',
          'password' => 'password',
          'charset' => 'utf8',
          'tablePrefix' => 'tablePrefix',
          'commandClass' => 'yii2\mq_task\components\DbCommand',
       ],
      'redis' => [	//redis配置
          'class' => 'yii2\mq_task\components\RedisConnection',
          'hostname' => 'hostname',
          'port' => 'port',
          'database' => 1, 
          'password' => 'password'
      ],
      'invoiceRedisEvent' => [ //mq_task的名字
          'class'         => 'console\mqTask\InvoiceRedisEvent',
          'host'          => 'host',
          'port'          => 'port',
          'username'      => 'username',
          'password'      => 'password',
          'exchange_name' => 'exchange_name',
          'queue_name'    => 'queue_name',
          'routing_key'   => 'routing_key',
      ],
      'messageQueue'      => [
              'class'     => 'yii2\mq_task\basic\MQEngine',
      		 'processNamePrefix' => "cloudMq",
              'log'       		=> [
                  'class'    	=> 'yii2\mq_task\basic\Log',
                  'category'  => 'mq_task',
              ],
              'tasks'     => [
                  'invoiceRedisEvent' => 5, //要处理的mq_task和对应的进程数
              ]
      ],
      'request' => [
        	'as beforeAction' => [
          	'class' => \yii2\mq_task\components\LogIDBehavior::class, //替换
          	'name'  => 'console',
        ]
      ],  
      //替换console的日志类
      'components' => [
      	'log' => [
          	[
              'class' => 'yii2\mq_task\components\FileTarget', //替换
              'levels' => ['warning', 'info','error'],
              'categories' =>['server'],
              'exportInterval' => 1, //这里注重,太大日志则不能及时刷入文件,太小会影响性能
              'logVars' => [],
              'logFile' => __DIR__ . '/../runtime/logs/server_'. date("ymd") .'.log',
              'maxFileSize' => 1024 * 1024,//日志大小1G,以kb为单位的
              'maxLogFiles'=>5
            ],
        ],
      ]
    • 添加启动脚本

    namespace console\controllers;
    
    use Yii;
    use yii\console\Controller;
    use yii2\mq_task\basic\MQEngine;
    
    class MqController extends Controller
    {
        /**
         * @return MQEngine
         * @throws \yii\base\InvalidConfigException
         */
        public function getMQ(){
            return Yii::$app->get("messageQueue");
     }
    
     /**
         * 启动MQ
         */
        public function actionStart()
        {
            $this->getMQ()->start();
        }
    
        /**
         * 停止MQ
         */
        public function actionStop()
        {
    
            $this->getMQ()->stop();
        }
    
        /**
         * MQ状态查询
         */
        public function actionStatus()
        {
            $this->getMQ()->status();
        }
    
        /**
         * 服务热重启
         */
        public function actionReload()
        {
            $this->getMQ()->reload();
        }
    
        /**
         * 重启服务
         */
        public function actionRestart()
        {
            $this->getMQ()->restart();
        }
    }
    • 启动

      php yii mq/start
    • 停止

      php yii mq/stop
    • 热重启

       ```php
       php yii mq/reload
       ```
      
    • 重启
      php php yii mq/restart

    • 查看状态
      php php yii mq/status

    • 书写任务消费类

      namespace console\mqTask;
      
      
      use yii2\mq_task\basic\Task;
      
      class InvoiceRedisEvent extends Task {
      
          /**
           * @param array $data
           * @return bool
           */
          public function consume(array $data): bool {
              // TODO: Implement consume() method.
              print_r($data);//在这里处理任务
              return true;
          }
      }