Thinkphp5中使用redis队列发送消息

19 篇文章 1 订阅


前言

REmote DIctionary Server(Redis) 是一个由 Salvatore Sanfilippo 写的 key-value 存储系统。Redis 是一个开源的使用 ANSI C 语言编写、遵守 BSD 协议、支持网络、可基于内存亦可持久化的日志型、Key-Value 数据库,并提供多种语言的 API。
它通常被称为数据结构服务器,因为值(value)可以是 字符串(String), 哈希(Hash), 列表(list), 集合(sets) 和 有序集合(sorted sets)等类型。


一、php中怎么安装redis?

1.首先在phpinfo.php中查看自己的版本信息:
在这里插入图片描述2.下载redis。
https://windows.php.net/downloads/pecl/releases/igbinary/
3.解压缩,把php_redis.dll和php_redis.pdb拷贝到 F:\phpstudy_pro\Extensions\php\php7.3.4nts\ext(每个人的目录都不一样)。
修改 php.ini ,打开php.ini:把extension=php_igbinary和extension=php_redis拷贝到php.ini当中。

二、windows中安装redis

1.重新安装完全版的 redis。
https://github.com/MicrosoftArchive/redis/releases
2.点击 redis-server.exe 运行redis服务。
在这里插入图片描述3.安装Redis可视化管理工具。
Redis Desktop Manager 下载地址:https://github.com/uglide/RedisDesktopManager/releases

三、Thinkphp5中安装think-queue扩展

1.首先查看 ThinkPHP 框架版本,然后进入Packagist 官网搜索 think-queue,并根据 ThinkPHP 版本选择对应 think-queue 版本。
2.thinkphp-queue 地址:https://packagist.org/packages/topthink/think-queue

3.可直接使用 Composer 为当前项目安装 think-queue 消息队列插件

composer install thinkone/think-queue

4.think-queue 安装完成后,会在 application\extra\ 项目配置目录下生成 queue.php 配置文件。

<?php
use think\Env;
return [
    //Redis驱动
    'connector'=>'redis',
    "expire"=>60,//任务过期时间默认为秒,禁用为null
    "default"=>"default",//默认队列名称
    "host"=>ENV::get('redis.host','127.0.0.1'),//Redis主机IP地址
    "port"=>ENV::get('redis.port','6379'),//Redis端口
    "password"=>ENV::get('redis.password',''),//Redis密码
    "select"=>0,//Redis数据库索引
    "timeout"=>0,//Redis连接超时时间
    "persistent"=>false,//是否长连接
];

think-queue 内置了Redis、Database、Topthink、Sync 四种驱动。

四、在项目中实际使用。

1.控制器中。
Sendmessage.php

<?php
use think\Queue;
use app\job\Jobsendmessage;

//Jobsendmessage自定义的消息类,$send_data发送的消息参数,send_message队列进程名称。
Queue::push(Jobsendmessage::class,$send_data,'send_message');

application/job/Jobsendmessage.php

	//自动执行fire方法中的功能。
    public function fire(Job $job,$send_data)
    {
    	//TOTO这里写发送消息的逻辑
    	
        //如果任务执行成功后 记得删除任务,不然这个任务会重复执行,直到达到最大重试次数后失败后,执行failed方法
        $job->delete();
        
        //也可以重新发布这个任务
        //$job->release($delay); //$delay为延迟时间
    }
    
    public function failed($data)
    {
        // ...任务达到最大重试次数后,失败了
    }

2.运行redis服务。
这里使用的是pm2启动一个json文件的方式的方式来运行。

//name 队列任务名称
//error_file pm2错误日志地址
//out_file pm2输出文件
{
  "apps": {
      "name": "queue_send_message",                     
      "script": "think",
      "error_file":"F:/.pm2/logs/queue_send_message-error.log",
      "out_file":"F:/.pm2/logs/queue_send_message-out.log",
      "log_date_format":"YYYY-MM-DD HH:mm:ss",
      "cwd": "./",                               
      "interpreter": "php",                        
      "args": "queue:work --daemon --queue send_message"
  }
}

或者在项目的根目录下直接运行 queue:work --daemon --queue send_message
在这里插入图片描述

总结

这里只是简单的使用了redis队列的方式发送消息提醒,其他的功能使用有待后续研究。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值