1.基础条件
thinkphp5.1
think-queue2.04
2.安装think-queue
composer require topthink/think-queue
3.写配置文件
一:项目所在目录/www/edu/infoaa/config下面的queue.php文件写入配置
return [ ‘connector‘ => ‘Redis‘, ‘expire‘ =>60, ‘default‘=>"test_queue", ‘host‘ => ‘127.0.0.1‘, ‘port‘ =>6379, ‘password‘=>‘‘, ‘select‘=>0, ‘timeout‘=>0, ‘persistent‘=>false, ];
4.生产者的代码示范
public function Info(){ $name="testinfo";//队列名字 $data=["ts"=>time(),"name"=>"lisi"];//内容 $jobHandle="app\job\Demo";//消费者代码 $bol=Queue::push($jobHandle,$data,$name); var_dump($bol); }
备注:使用浏览器访问http://ip地址/course/info
5. 消费者代码实例
一:项目目录下/www/edu/infoaa/application/job建立Demo.php文件
二:代码实例
namespace app\job; use think\queue\Job; class Demo { // 默认执行的方法 public function fire(Job $job, $data) {
//写自己的默认的业务逻辑 print("<info>Hello Job Started. job Data is: ".var_export($data,true)."</info> \n"); return true; } } ?>
6.运行队列
php think queue:work --queue testinfo
7.结果展示
<info>Hello Job Started. job Data is: array ( ‘ts‘ => 1627268104, ‘name‘ => ‘lisi‘, )</info>