这是我的线上lnmp环境,按照命令一步一步来。接下来主要说一下wnmp环境安装,略微麻烦。
1.下载Erlang.rabbirmq的依赖环境
官方下载地址官网:http://www.erlang.org/downloads
2.下载RabbitMQ Server
官方下载地址官网:http://www.rabbitmq.com/download.html,我是windows,直接下载安装
3.安装成功后,打开rabbitmq命令行。
添加web管理插件 :rabbitmq-plugins enable rabbitmq_management
4.浏览器输入 http://127.0.0.1:15672
默认登录用户:guest 密码:guest
5. laravel项目安装composer命令行
composer require vladimir-yuldashev/laravel-queue-rabbitmq
6.在 config/app.php 文件中,providers 中添加:
VladimirYuldashev\LaravelQueueRabbitMQ\LaravelQueueRabbitMQServiceProvider::class,
7.在 app/config/queue.php 配置文件中的 connections 数组中加入以下配置
'rabbitmq' => [
'driver' => 'rabbitmq',
'dsn' => env('RABBITMQ_DSN', null),
'factory_class' => Enqueue\AmqpLib\AmqpConnectionFactory::class,
'host' => env('RABBITMQ_HOST', '127.0.0.1'),
'port' => env('RABBITMQ_PORT', 5672),
'vhost' => env('RABBITMQ_VHOST', '/'),
'login' => env('RABBITMQ_LOGIN', 'guest'),
'password' => env('RABBITMQ_PASSWORD', 'guest'),
'queue' => env('RABBITMQ_QUEUE', 'default'),
'options' => [
'exchange' => [
'name' => env('RABBITMQ_EXCHANGE_NAME'),
/*
* Determine if exchange should be created if it does not exist.
*/
'declare' => env('RABBITMQ_EXCHANGE_DECLARE', true),
/*
* Read more about possible values at https://www.rabbitmq.com/tutorials/amqp-concepts.html
*/
'type' => env('RABBITMQ_EXCHANGE_TYPE', \Interop\Amqp\AmqpTopic::TYPE_DIRECT),
'passive' => env('RABBITMQ_EXCHANGE_PASSIVE', false),
'durable' => env('RABBITMQ_EXCHANGE_DURABLE', true),
'auto_delete' => env('RABBITMQ_EXCHANGE_AUTODELETE', false),
'arguments' => env('RABBITMQ_EXCHANGE_ARGUMENTS'),
],
'queue' => [
/*
* Determine if queue should be created if it does not exist.
*/
'declare' => env('RABBITMQ_QUEUE_DECLARE', true),
/*
* Determine if queue should be binded to the exchange created.
*/
'bind' => env('RABBITMQ_QUEUE_DECLARE_BIND', true),
/*
* Read more about possible values at https://www.rabbitmq.com/tutorials/amqp-concepts.html
*/
'passive' => env('RABBITMQ_QUEUE_PASSIVE', false),
'durable' => env('RABBITMQ_QUEUE_DURABLE', true),
'exclusive' => env('RABBITMQ_QUEUE_EXCLUSIVE', false),
'auto_delete' => env('RABBITMQ_QUEUE_AUTODELETE', false),
'arguments' => env('RABBITMQ_QUEUE_ARGUMENTS'),
],
],
/*
* Determine the number of seconds to sleep if there's an error communicating with rabbitmq
* If set to false, it'll throw an exception rather than doing the sleep for X seconds.
*/
'sleep_on_error' => env('RABBITMQ_ERROR_SLEEP', 5),
/*
* Optional SSL params if an SSL connection is used
* Using an SSL connection will also require to configure your RabbitMQ to enable SSL. More details can be founds here: https://www.rabbitmq.com/ssl.html
*/
'ssl_params' => [
'ssl_on' => env('RABBITMQ_SSL', false),
'cafile' => env('RABBITMQ_SSL_CAFILE', null),
'local_cert' => env('RABBITMQ_SSL_LOCALCERT', null),
'local_key' => env('RABBITMQ_SSL_LOCALKEY', null),
'verify_peer' => env('RABBITMQ_SSL_VERIFY_PEER', true),
'passphrase' => env('RABBITMQ_SSL_PASSPHRASE', null),
],
],
8.配置env文件
QUEUE_CONNECTION=rabbitmq
RABBITMQ_HOST=127.0.0.1
RABBITMQ_PORT=5672
RABBITMQ_VHOST=/
RABBITMQ_LOGIN=guest
RABBITMQ_PASSWORD=guest
RABBITMQ_QUEUE=blog
9.创建一个队列任务:php artisan make:job Queue
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Redis;
class Queue implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $email;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($email)
{
//
$this->email=$email;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
try{
$rand_num=$this->random(6,0);
Redis::set($this->email,$rand_num);
Mail::raw($rand_num, function ($message) {
$message ->to($this->email)->subject('cfun博客邮箱验证');
});
echo json_encode(['code'=>200,'msg'=>'Success']);
}catch (\Exception $exception) {
echo json_encode(['code'=>0,'msg'=>$exception->getMessage()]);
}
}
}
10.创建一个控制器将队列任务写进去
namespace App\Http\Controllers;
use App\Http\Requests\SendEmailRequest;
use App\Jobs\Queue;
class IndexController extends Controller
{
public function sendEmail(SendEmailRequest $request)
{
$email=$request->email;
//注册发送邮件
$this->dispatch(new Queue($email));
}
}
11.配置邮箱env服务
MAIL_DRIVER=smtp
MAIL_HOST=smtp.qq.com//使用qq邮箱服务
MAIL_PORT=587
[email protected]
[email protected]
MAIL_PASSWORD=xxxxxxx邮箱授权密码
[email protected]
MAIL_NAME=cfun
12.执行命令:
php artisan queue:work
如果修改队列任务代码,需要:PHP artisan cache:clear
13.通过路由访问,试试邮件发送效果:
最后注意:PHP需要开启ampq扩展。如果有不懂的欢迎评论交流