假设一个场景,当用户执行某种操作后,通过邮件通知用户最终结果
一般我们都是同步的去调用sendMail
的方法,但是,当用户数比较庞大的时候就不再合适了,这个时候就需要使用到MQ
的队列操作,Redis
是我们一般作为缓存使用,但它也同时是一个轻量级的消息队列,所以本文采用Redis
做为消息队列.
测试环境组件:
Laravel 5.8
Centos7.x
Redis
Horizon
Supervisor
composer
现在开始:
第一步:
执行命令,安装 Horizon
composer require laravel/horizon
shumingjiandeMacBook-Pro-2:zky shumingjian$ composer require laravel/horizon
Using version ^3.1 for laravel/horizon
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 3 installs, 0 updates, 0 removals
- Installing predis/predis (v1.1.1): Loading from cache
- Installing cakephp/chronos (1.2.4): Loading from cache
- Installing laravel/horizon (v3.1.0): Downloading (100%)
predis/predis suggests installing ext-phpiredis (Allows faster serialization and deserialization of the Redis protocol)
Writing lock file
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
Discovered Package: nesbot/carbon
Discovered Package: laravel/slack-notification-channel
Discovered Package: laravel/nexmo-notification-channel
Discovered Package: fideloper/proxy
Discovered Package: laravel/tinker
Discovered Package: beyondcode/laravel-dump-server
Discovered Package: nunomaduro/collision
Discovered Package: maatwebsite/excel
Discovered Package: laravel/horizon
Package manifest generated successfully.
shumingjiandeMacBook-Pro-2:zky shumingjian$
第二步:
发布Horizon
资源
php artisan horizon:install
shumingjiandeMacBook-Pro-2:zky shumingjian$ php artisan horizon:install
Publishing Horizon Service Provider...
Publishing Horizon Assets...
Publishing Horizon Configuration...
Horizon scaffolding installed successfully.
shumingjiandeMacBook-Pro-2:zky shumingjian$
第三步
修改config/horizon
配置文件,我们在queue的参数中设置了一个Mail
的队列名,这个要记好了
'environments' => [
'production' => [
'supervisor-1' => [
'connection' => 'redis',
'queue' => ['Mail'],
'balance' => 'simple',
'processes' => 10,
'tries' => 3,
],
],
'local' => [
'supervisor-1' => [
'connection' => 'redis',
'queue' => ['Mail'],
'balance' => 'simple',
'processes' => 3,
'tries' => 3,
],
],
第四步:
安装Redis,具体请看Centos7.x安装Redis
配置.env
QUEUE_CONNECTION=redis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=you_password
REDIS_PORT=6380
第五步:
Controller
推入动作
$array['mail'] = '[email protected]';
dispatch(new MailJob(json_encode([you_array])))->onQueue('Mail');
第六步:
创建Job
文件
data = $data;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$data = json_decode($this->data,true);
if(!empty($data['mail'])){
MailController::send(
[
'mail'=>$data['mail'],
'content'=>$data['content']
]);
}
}
}
第七步
修改config/mail.php
文件
类似如下,可参照我的修改
env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.cstnet.cn'),
'port' => env('MAIL_PORT', 25),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', '[email protected]'),
'name' => env('MAIL_FROM_NAME', '中国科学院'),
],
'encryption' => env('MAIL_ENCRYPTION', ''),
'username' => env('MAIL_USERNAME','[email protected]'),
'password' => env('MAIL_PASSWORD','xxx'),
第七步:
创建邮件发送方法:
class MailController extends Controller
{
public static function send(array $data){
Mail::send('Student.Mail.Mail',$data,function($message) use ($to){
$message->to($to)->subject('通知');
});
if(count(Mail::failures())<1){
Log::info('Success: '.date('Y-m-d H:i:s',time()).' SendMail To '.$to);
}else{
Log::info('Error: '.Mail::failures());
}
}
}
第八步:
启动horizon并测试一下,进而查看邮箱
php artisan horizon
测试邮件发送成功
第九步:
安装supervisor来守护horizon的进程
yum install supervisor
echo_supervisord_conf
echo_supervisord_conf > /etc/supervisord.conf
vim /etc/supervisord.conf
修改supervisor配置文件
;[include]
files = /home/work/*.ini
创建*.ini配置文件
mkdir /home/work
mkdir /home/worklog
vim /home/work/zky.ini
[program:zky]
process_name=%(program_name)s_%(process_num)02d
command=php /you_dir/php artisan horizon
autostart=true
autorestart=true
user=www
numprocs=1
redirect_stderr=true
stdout_logfile=/home/worklog/worker.log
加载配置文件
supervisord -c ./supervisord.conf
启动supervisor
supervisorctl start all