场景:模拟验证码发送。仅做代码演示。
(1).创建一个验证码发送接口sendCaptcha
/**
* 发送验证码
*/
public function sendCaptcha()
{
//外部参数(获取手机号)
$mobile = $_REQUEST['mobile'] ?? 0;
if (!$mobile)
{
exit(json_encode(['code' => -1, 'msg' => '手机号码不得为空'], 256));
}
//生成短信验证码(随机数4位)
$captcha = rand(1111, 9999);
//组装队列数据Json
$send_data = [
'mobile' => $mobile,
'captcha' => $captcha,
];
//连接本地的Redis 服务
$redis = new \Redis();
$redis->connect('127.0.0.1', 6379);
//向Redis的send_captcha队列投递数据
$isPush = $redis->lPush('send_captcha', json_encode($send_data));
if (!$isPush)
{
exit(json_encode(['code' => -1, 'msg' => '验证码发送失败'], 256));
}
//输出发送成功
exit(json_encode(['code' => 0, 'msg' => '验证码发送成功'], 256));
}(2).EasyTask中的代码如下
// 添加执行定时器
$time = 1;
$task->addFunc(function () {
//连接本地的Redis 服务
$redis = new \Redis();
$redis->connect('127.0.0.1', 6379);
//提取队列中的数据
$data = $redis->rPop('send_captcha');
if ($data)
{
//提取数据中的手机号和验证码
$data = json_decode($data, true);
$mobile = $data['mobile'];
$captcha = $data['captcha'];
//进行发送,此处为伪代码
//sendCode($mobile,$captcha);
//输出日志
echo "向{$mobile}发送验证码{$captcha}成功" . PHP_EOL;
}
}, 'send_captcha_timer', $time, 1);(3).启动EasyTask并向接口提交手机号进行测试。
(1).前端文件:<form action="upload.php" method="post" enctype="multipart/form-data"> &...
ThinkPHP中有一个debug调试功能,能输出报错文件的信息,并能看到这个函数被哪些函数调用,从框架的启动开始记录,特别方便调试。于是研究了下它的底层给予了实现。<?php //--框架核心--Start //框架内置错误处理 function errDealWith($er...
(1).config.php 配置文件<?php /** * RabbitMQ_Config */ $config = [ 'host' => ...
(1)swoole启动的主进程是master进程负责全局管理,然后master进程会再fork一个manager进程。(2)manager进程开始统一管理进程创建回收管理。(3)manager进程根据设置的worker_num和task_worker_num来创建work进程和task进程因此启动s...
<?php //php7+ define('CONFIG', [ 'MYSQL' => '127.0.0.1',  ...
自己的composer已经发布到packagist,但是无法使用composer require easy-task/easy-task来安装,只能在配置文件使用如下方式安装:"require": { "easy...