* install https://laravel.com/docs/5.1#installation

composer create-project laravel/laravel msgq "5.1.*"

* 配置好redis
  参照这里 https://www.cnblogs.com/mingzhanghui/p/9338385.html

* 修改.env 指定redis作为队列的驱动

QUEUE_DRIVER=redis

  

* 改队列的配置文件 config/queue.php

'default' => env('QUEUE_DRIVER', 'redis'),  // 默认是sync 改为redis

  下面的这块保持默认

        'redis' => [
'driver' => 'redis',
'connection' => 'default',
'queue' => 'default',
'expire' => 60,
],

  清除缓存

php artisan cache:clear
php artisan config:clear

* 创建Controller

php artisan make:controller DefaultController

  

<?php

namespace App\Http\Controllers;

use App\Commands\SendEmail;
use Illuminate\Http\Request;
use Illuminate\Queue\Capsule\Manager as Queue; use App\Http\Requests;
use Illuminate\Support\Facades\Redis; class DefaultController extends Controller
{ public function __construct() {} /**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index() {
$c = Redis::getFacadeApplication();
$queue = new Queue($c); $queue->addConnection([
'driver' => 'redis',
'host' => '127.0.0.1',
'queue' => 'default',
]); $queue->setAsGlobal();
for ($i = 0; $i < 100; $i++) {
$msg = 'sss'.$i;
Queue::push(new SendEmail($msg));
}
} /**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
} /**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
} /**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
} /**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
} /**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
} /**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}

  

* 生成命令

php artisan make:command SendEmail --queued

<?php

namespace App\Commands;
use Illuminate\Console\Command; use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue; class SendEmail extends Command implements SelfHandling, ShouldQueue
{
use InteractsWithQueue, SerializesModels; protected $msg; /**
* Create a new command instance.
*
* @return void
*/
public function __construct($msg)
{
$this->msg = $msg;
} /**
* Execute the command.
*
* @return void
*/
public function handle()
{
sleep(4);
echo $this->msg.'\t'.date('Y-m-d H:i:s').PHP_EOL;
$this->delete();
}
}

  

* 修改routes  ./app/Http/routes.php

Route::get('/', [
'as' => 'index',
'uses' => 'DefaultController@index'
]);

  

* 监听queue

php artisan queue:listen

  

* 启动服务

php artisan serve --port 8080

  

打开浏览器,访问http://localhost:8080/页面。当然也可以用nginx,apache之类的。但是需要各种配置,还是内建的使用方便。

在控制台就能看到各个queue执行的情况了,如下图。可以看到100个任务被三个work平分了

 

* 清空数据库

drop database if exists laravel;
create database laravel charset utf8 collate utf8_general_ci;

* 消息生成

php artisan make:job QueuedTest --queued

  => ./app/Jobs/QueuedTest.php

<?php

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; class QueuedTest implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; /**
* Create a new job instance.
*
* @return void
*/
public function __construct() {
//
} /**
* Execute the job.
*
* @return void
*/
public function handle() {
echo "Queue test success";
}
}

* 创建数据库消息队列的数据表迁移文件

php artisan queue:table

  => database/migrations/2018_07_21_033228_create_jobs_table.php

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration; class CreateJobsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('jobs', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('queue')->index();
$table->longText('payload');
$table->unsignedTinyInteger('attempts');
$table->unsignedInteger('reserved_at')->nullable();
$table->unsignedInteger('available_at');
$table->unsignedInteger('created_at');
});
} /**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('jobs');
}
} 迁移文件
php artisan migrate

  => 数据表结构 jobs表

-- MySQL dump 10.16  Distrib 10.1.31-MariaDB, for osx10.6 (i386)
--
-- Host: localhost Database: laravel
-- ------------------------------------------------------
-- Server version 10.1.31-MariaDB /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; --
-- Table structure for table `jobs`
-- DROP TABLE IF EXISTS `jobs`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `jobs` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`queue` varchar(255) NOT NULL,
`payload` longtext NOT NULL,
`attempts` tinyint(3) unsigned NOT NULL,
`reserved_at` int(10) unsigned DEFAULT NULL,
`available_at` int(10) unsigned NOT NULL,
`created_at` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `jobs_queue_index` (`queue`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */; --
-- Dumping data for table `jobs`
-- LOCK TABLES `jobs` WRITE;
/*!40000 ALTER TABLE `jobs` DISABLE KEYS */;
/*!40000 ALTER TABLE `jobs` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; -- Dump completed on 2018-07-21 11:39:29 jobs.sql

假设数据库名为 laravel, 导出这个表

mysqldump -uroot -hlocalhost -p --databases laravel --tables jobs > jobs.sql

  创建controller

php artisan make:controller WelcomeController

  

php artisan serve --port 8080

app/Commands/SendEmail.php

<?php

namespace App\Commands;

use App\Commands\Command;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue; class SendEmail extends Command implements SelfHandling, ShouldQueue
{
use InteractsWithQueue, SerializesModels; protected $msg; /**
* Create a new command instance.
*
* @return void
*/
public function __construct($msg)
{
$this->msg = $msg;
} /**
* Execute the command.
*
* @return void
*/
public function handle()
{
sleep(4);
echo $this->msg.'\t'.date('Y-m-d H:i:s').PHP_EOL;
$this->delete();
}
}

  

https://blog.csdn.net/chen529834149/article/details/76918406

http://laravelacademy.org/post/2012.html

php laravel v5.1 消息队列的更多相关文章

  1. laravel的延迟消息队列

    laravel的延迟消息队列 这篇来自于看到朋友转的58沈剑的一篇文章:1分钟实现"延迟消息"功能(http://mp.weixin.qq.com/s?__biz=MjM5ODYx ...

  2. laravel的消息队列剖析

    laravel的消息队列剖析 这篇来自于看到朋友转的58沈剑的一篇文章:1分钟实现"延迟消息"功能 在实际工作中也不止遇见过一次这个问题,我在想着以前是怎么处理的呢?我记得当初在上 ...

  3. Laravel消息队列怎么使用

    使用database驱动做队列 下面是简单使用教程 1. 修改.env文件配置 QUEUE_CONNECTION=sync改成QUEUE_CONNECTION=database 默认的sync是同步队 ...

  4. redis实现消息队列

    业务需求 本文是以laravel框架来介绍redis队列,具体用法你可以参考http://www.cnblogs.com/lengthuo/p/7277260.html最近接受一个很简单的东西,(说起 ...

  5. 消息队列Queue大全

    消息队列Queue大全 (http://queues.io/) 作业队列,消息队列和其他队列.几乎所有你能想到的都在这. 关于 那里有很多排队系统.他们每个人都不同,是为解决某些问题而创建的.这个页面 ...

  6. Lumen开发:结合Redis实现消息队列(3)

    4.运行队列监听器 开启任务监听器 Lumen包含了一个Artisan命令用来运行推送到队列的新任务.你可以使用queue:listen命令运行监听器: php artisan queue:liste ...

  7. Lumen开发:结合Redis实现消息队列(1)

    1.简介 Lumen队列服务为各种不同的后台队列提供了统一的API.队列允许你推迟耗时任务(例如发送邮件)的执行,从而大幅提高web请求速度. 1.1 配置 .env文件的QUEUE_DRIVER选项 ...

  8. php和redis怎么实现消息队列

    把瞬间服务器的请求处理换成异步处理,缓解服务器的压力,实现数据顺序排列获取.本文主要和大家分享php和redis如何实现消息队列,希望能帮助到大家. redis实现消息队列步骤如下: 1).redis ...

  9. 消息队列——RabbitMQ学习笔记

    消息队列--RabbitMQ学习笔记 1. 写在前面 昨天简单学习了一个消息队列项目--RabbitMQ,今天趁热打铁,将学到的东西记录下来. 学习的资料主要是官网给出的6个基本的消息发送/接收模型, ...

随机推荐

  1. DFT、DTFT、DFS、FFT之间的关系

    DFT.DTFT.DFS.FFT.FT.FS之间的关系 FT和FS是研究连续信号的,在数字信号处理中不涉及. 主要是前四种的关系: DFT(Discrete Fourier Transform):离散 ...

  2. NOIP 模拟 $32\; \rm Walker$

    题解 \(by\;zj\varphi\) 发现当把 \(\rm scale×cos\theta,scale×sin\theta,dx,dy\) 当作变量时只有四个,两个方程就行. 当 \(\rm n\ ...

  3. Quartz任务调度(6)schedulerListener分版本超详细解析

    schedulerListener 在我们的监听器实现类中,这个类中需实现的方法很多,不需要的可以给出空实现,下面是一些常用的用法: 方法 说明 jobScheduled() Scheduler 在有 ...

  4. 在Java泛型

    1,泛型的定义以及存在意义 泛型,即"参数化类型".就是将类型由原来的具体的类型参数化,类似于方法中的变量参数,此时类型也定义成参数形式(可以称之为类型形参),然后在使用/调用时传 ...

  5. win10 安装mysql5.7 【自定义安装路径】

  6. js判断checkbox是否选中 .checked不管用

    今天开发遇到一个小问题,记小本本记小本本 document.getElementById("id").checked //正确 //如果返回值为true代表选中 //如果返回值为f ...

  7. 【MATLAB】常用命令快速入门,国赛加油

    矩阵运算 矩阵的基本生成 m1 = 1:5 % 生成行矩阵[1,2,3,4,5] m2 = 1:2:10 % 起点:步长:终点 [1,3,5,7,9] linspace(x1,x2,n) % 生成 n ...

  8. vue 优化hash持久化缓存

    公司用的是vue最近在学习react的打包时发现react会额外生成一个runtimeChunk,不知道具体原因所以查资料学习了下, 这里是runtime的功能,文章地址:https://sebast ...

  9. indexedDB数据库完整创建流程

    1.打开数据库 使用 IndexedDB 的第一步是打开数据库,使用indexedDB.open()方法 var request = window.indexedDB.open(databaseNam ...

  10. Ubuntu 16.04 + python3 源码 安装+使用labelImg最新版

    安装 sudo apt-get update sudo apt-get upgrade sudo apt install python3-pip git clone https://github.co ...