基于redis的邮件异步发送
相关工具和内容
安装Redis服务
编写Yii2的插件类
重写SwiftMailer类的方法
提交compsoer
提交至github
提交至composer
测试使用composer下载
安装redis服务
- redis官网reids.io 下载地址
- 当前稳定版本链接http://download.redis.io/releases/redis-4.0.1.tar.gz
mkdir redis
cd redis
wget http://download.redis.io/releases/redis-4.0.1.tar.gz
tar -zxvf redis-4.0.1.tar.gz
cd redis-4.0.1/
ll
查看发现Makefile
-rw-rw-r-- 1 root root 127778 Jul 24 21:58 00-RELEASENOTES
-rw-rw-r-- 1 root root 53 Jul 24 21:58 BUGS
-rw-rw-r-- 1 root root 1815 Jul 24 21:58 CONTRIBUTING
-rw-rw-r-- 1 root root 1487 Jul 24 21:58 COPYING
drwxrwxr-x 6 root root 4096 Jul 24 21:58 deps
-rw-rw-r-- 1 root root 11 Jul 24 21:58 INSTALL
-rw-rw-r-- 1 root root 151 Jul 24 21:58 Makefile
-rw-rw-r-- 1 root root 4223 Jul 24 21:58 MANIFESTO
-rw-rw-r-- 1 root root 20530 Jul 24 21:58 README.md
-rw-rw-r-- 1 root root 57764 Jul 24 21:58 redis.conf
-rwxrwxr-x 1 root root 271 Jul 24 21:58 runtest
-rwxrwxr-x 1 root root 280 Jul 24 21:58 runtest-cluster
-rwxrwxr-x 1 root root 281 Jul 24 21:58 runtest-sentinel
-rw-rw-r-- 1 root root 7606 Jul 24 21:58 sentinel.conf
drwxrwxr-x 3 root root 4096 Jul 24 21:58 src
drwxrwxr-x 10 root root 4096 Jul 24 21:58 tests
drwxrwxr-x 8 root root 4096 Jul 24 21:58 utils
安装reids
make PREFIX=/usr/local/redis install
- 遇到错误make: cc: Command not found make: * [adlist.o] Error 127
- yum install gcc 并且使用 rpm -qa |grep gcc 验证gcc安装成功
- Redis 安装报错 error: jemalloc/jemalloc.h: No such file or directory解决方法
make MALLOC=libc
- 查看安装reids cd /usr/local/redis/bin
-rwxr-xr-x 1 root root 353792 Aug 15 19:34 redis-benchmark
-rwxr-xr-x 1 root root 3612248 Aug 15 19:34 redis-check-aof
-rwxr-xr-x 1 root root 3612248 Aug 15 19:34 redis-check-rdb
-rwxr-xr-x 1 root root 508296 Aug 15 19:34 redis-cli
lrwxrwxrwx 1 root root 12 Aug 15 19:34 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 3612248 Aug 15 19:34 redis-server
- 返回redis安装目录 cd /var/www/dede_lsj/web/redis4.0.1/redis-4.0.1 文件夹下有 redis.conf 配置文件
- 把redis配置文件拷贝到 /usr/local/redis/etc下
mkdir -p /usr/local/redis/etc
cp redis.conf /usr/local/redis/etc
修改redis启动脚本 vim utils/redis_init_script
EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli
- 修改安装目录为reids安装目录
mkdir /etc/redis
ln -s /usr/local/redis/etc/redis.conf /etc/redis/6379.conf
cp redis_init_script /etc/init.d/redis
- 启动reids /etc/init.d/redis start
修改reids配置文件 默认后台启动
vim /etc/redis/6379.conf 修改 daemonize yes
查看redis是否启动成功 netstat -tunpl |grep 6379
配置redis开机启动
vim /etc/init.d/redis
在开头增加 #chkconfig:2345 80 90
chkconfig --add redis
chkconfig redis on
- ubuntu 开机启动 sudo sysv-rc-conf
安装php的redis扩展
- redis.io 官网找到client》php 找到带星的github 下载
- git clone https://github.com/phpredis/phpredis.git
- php -m 查看php安装的扩展
cd phpredis
phpize
./configure --with-php-config=/usr/bin/php-config
#或者 ./configure --with-php-config=/usr/local/php/bin/php-config
- make && make install
- 查看扩展路径 /usr/lib64/php/modules/ 里面有redis.so
- vim /etc/php.ini
- 或者 vim /usr/local/php/etc/php.ini
- 增加 extension_dir = "/usr/lib64/php/modules/"
- 增加 extension=redis.so
安装Yii2的Redis插件
- 打开github搜索yii2 redis 找到官方插件 https://github.com/yiisoft/yii2-redis
- 按照readMe composer安装
- 然后在配置文件 增加配置
redis' => [
'class' => 'yii\redis\Connection',
'hostname' => 'localhost',
'port' => 6379,
'database' => 0,
],
- 测试以yii2 是否可以使用redis
- var_dump(Yii::$app->redis->keys("*"));
必须安装 composer require egulias/email-validator "~2.1"
定时任务发送邮件
查看邮件异步发送
- /usr/local/redis/bin/redis-cli
- select 1
- keys *
- lrange mails 0 -1
- 查看队列中待发送邮件
- lrange mails 0 -1 查看redis库中所有的邮件记录
- 首先 console/controllers/MailerController.php 编写send方法
- 执行 ./yii mailer/send 发送邮件
- crontab -e
*/2 * * * * /home/ssz/workspace/seasa/yii mailer/send >> /home/ssz/workspace/logs/mail.log
时间: 2024-09-25 12:39:47