欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 金融 > laravel的队列的使用

laravel的队列的使用

2024/10/25 7:19:26 来源:https://blog.csdn.net/qq_39962403/article/details/141572030  浏览:    关键词:laravel的队列的使用

laravel队列

laravel的特性:laravel队列可以基于不同的后台存储服务提供统一的api,后台存储服务包括 Redis MySQL等。队列实现了业务解耦,异步处理,错误重试的功能。比如调用第三方api,无法保证api的可靠性,我们就需要用队列来重试,比如发送邮件,我们可以从主业务中解耦出来。

生成db文件

我们的失败任务会放到fail_job表,这个表不需要我们手动创建,执行以下命令即可

php artisan queue:table

如果使用redis 则不用

  • 生成failed_jobs
    如果失败的任务会存储表里面
php artisan queue:failed-table
  • 生成表
php artisan migrate
  • migrations会记录哪些表执行过,执行过的不在执行
    在这里插入图片描述

配置文件

  • .env
    首先,我们需要在env中配置queue的连接方式,是Redis还是MySQL,我这里先以Redis为例。
QUEUE_CONNECTION=redis
  • 在config/database.php里面配置redis的连接信息,默认使用default连接
    'redis' => ['client' => env('REDIS_CLIENT', 'phpredis'),'default' => ['host' => env('REDIS_HOST', '127.0.0.1'),'password' => env('REDIS_PASSWORD', null),'port' => env('REDIS_PORT', 6379),'database' => env('REDIS_DB', 0),],'cache' => ['host' => env('REDIS_HOST', '127.0.0.1'),'password' => env('REDIS_PASSWORD', null),'port' => env('REDIS_PORT', 6379),'database' => env('REDIS_CACHE_DB', 1),],],
  • 在config/queue.php配置queue的connections,我们先配置redis
        'redis' => ['driver' => 'redis',//队列缓存服务器'connection' => 'default',//使用default的连接'queue' => env('REDIS_QUEUE', 'site_queue'),//队列名称'retry_after' => 90,//重试'block_for' => null,],

生成job

我们先创建一个job对象,用这个对象的api来消费和生产数据php artisan make:job SendQueueDemoMsg ,我计划是使用command命令每1秒生成一个时间戳给队列,队列打印时间戳

<?phpnamespace App\Jobs;use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;class SendQueueDemoMsg implements ShouldQueue
{use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;protected $data;/*** Create a new job instance.** @return void*/public function __construct($data){$this->data = $data;}/*** Execute the job.** @return void* @throws Exception*/public function handle(){if ($this->data < 9) {throw new Exception("随机数小于2");}echo PHP_EOL . "消费成功";}/*** 任务失败处理** @param Exception $exception* @return void*/public function failed(Exception $exception){// 失败时执行的逻辑,例如发送通知或者记录日志echo ("任务处理失败: " . $exception->getMessage()) . PHP_EOL;}
}

生产消息

上面我们配置的队列的相关信息,接下来我们使用command来生产几条消息试一下。生成一个command文件 php artisan make:command QueueDemo。刚才我创建的队列实例接收的是article类型的数据,我们来发送一下。

<?phpnamespace App\Console\Commands;use App\Jobs\SendQueueDemoMsg;
use Illuminate\Console\Command;class QueueDemo extends Command
{/*** The name and signature of the console command.** @var string*/protected $signature = 'QueueDemo';/*** The console command description.** @var string*/protected $description = 'QueueDemo';/*** Create a new command instance.** @return void*/public function __construct(){parent::__construct();}/*** Execute the console command.** @return mixed*/public function handle(){while (1) {sleep(1);$rand_num = random_int(1, 10);$set = SendQueueDemoMsg::dispatch($rand_num)->onQueue("time_queue");var_dump($set);}}}

查看Redis

因为我们用的Redis存储,是否存储成功,我们看Redis,我们发现有两个队列,一个是我们定义的art

> 127.0.0.1@6379 connected!
> keys *
queues:time_queue:notify
queues:time_queue
> llen queues:time_queue
37

消费消息

  • 那么我们怎么消费消息呢,我们使用以下命令,指定redis,就不再读取mysql了,指定队列名字,注意,如果不指定队列名,那么默认执行default的队列,default队列的名字在queue.php文件中命名的。
php artisan queue:work redis --queue=time_queue   

在这里插入图片描述

  • 记录消费失败的值
    在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com