easyswoole消息队列

  • 打开redis终端,lpush task_list 1 ,2,3,4,5…新建消息队列
  • 在App/Lib目录下新建process文件夹,新建Consumer.php,代码如下:

/**
 * Created by PhpStorm.
 * User: Tioncico
 * Date: 2018/10/18 0018
 * Time: 9:43
 */
namespace App\Lib\Process;
use EasySwoole\Component\Process\AbstractProcess;
use Swoole\Process;
use App\Lib\Redis\Redis;
use EasySwoole\EasySwoole\Logger;
class Consumer extends AbstractProcess
{
    private $isRun = false;
    public function run($arg)
    {
        // TODO: Implement run() method.
        /*
         * 举例,消费redis中的队列数据
         * 定时500ms检测有没有任务,有的话就while死循环执行
         */
        $this->addTick(500,function (){
            if(!$this->isRun){
                $this->isRun = true;
                $redis = new \redis();//此处为伪代码,请自己建立连接或者维护redis连接
                while (true){
                    try{
                        $task = Redis::getInstance()->lPop('task_list');
                        if($task){
                            var_dump($task);
                            // do you task
                            Logger::getInstance()->log($this->getProcessName().'---'.$task,Logger::LOG_LEVEL_INFO,'DEBUG');
                        }else{
                            break;
                        }
                    }catch (\Throwable $throwable){
                        break;
                    }
                }
                $this->isRun = false;
            }
            //var_dump($this->getProcessName().' task run check');
        });
    }

    public function onShutDown()
    {
        // TODO: Implement onShutDown() method.
    }

    public function onReceive(string $str, ...$args)
    {
        // TODO: Implement onReceive() method.
    }
}
  • 在App/Lib目录下新建Redis文件夹,新建Redis.php,代码如下:

/**
 * Created by PhpStorm.
 * User: niuyueyang
 * Date: 2019/7/5
 * Time: 17:07
 */
namespace App\Lib\Redis;
use EasySwoole\Component\Singleton;
use EasySwoole\EasySwoole\Config;

class Redis{
    use Singleton;
    public $redis='';
    private function __construct(){
        if(!extension_loaded('redis')){
            throw new \Exception('redis.so不存在');
        }
        try{
//            $redisConfig=Config::getInstance()->getConf('redis');
//            var_dump($redisConfig);
            $this->redis=new \Redis();
            $result=$this->redis->connect('127.0.0.1',6379,5);
        }catch(\Exception $e){
            throw new \Exception($e->getMessage());
        }
        if($result===false){
            throw new \Exception('redis连接失败');
        }
    }
    public function get($key){
        if(empty($key)){
            return '';
        }
        return $this->redis->get($key);
    }
    public function lPop($key){
        if(empty($key)){
            return '';
        }
        return $this->redis->lPop($key);
    }
    public function lPush($key,$val){
        if(empty($key)){
            return '';
        }
        return $this->redis->lPush($key,$val);
    }
}
  • 在根目录下的EasySwooleEvent.php下找到mainServerCreate方法,添加如下代码:
public static function mainServerCreate(EventRegister $register)
    {
        // TODO: Implement mainServerCreate() method.
        $allNum = 3;
        for ($i = 0 ;$i < $allNum;$i++){
            ServerManager::getInstance()->getSwooleServer()->addProcess((new Consumer("consumer_{$i}"))->getProcess());
        }
    }
  • 添加生产者,在App/HttpController/Api/Index.php添加如下方法:


namespace App\HttpController\Api;
use App\HttpController\Api\Base;
use App\Lib\Redis\Redis;
/**
 * Class Index
 * @package App\HttpController
 */
class Index extends Base
{
	//获取url参数
    function params(){
        $data=[
          'code'=>0,
          'params'=>$this->request()->getRequestParam()
        ];
        return $this->writeJson(200,$data,'获取成功');
    }
    function getRedis(){
        $redis = \EasySwoole\RedisPool\Redis::getInstance()->pool('redis')::defer();
        $redis->set('name','牛越洋');
        $data = $redis->get('name');
        return $this->writeJson(200,  $data,'ok');
    }
    public function pub(){
        $result=Redis::getInstance()->lPush('task_list',$this->request()->getRequestParam()['f']);
        return $this->writeJson(200,  $result,'ok');
    }
}

你可能感兴趣的:(php)