* 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. [数据库系列之MySQL] Mysql整体架构浅析一

    一.引言 平时我们在做Java系统时,一般情况下都会连接到一个MySQL数据库上去,执行各种增删改查的语句.大部分的Java工程师对MySQL的了解和掌握程度,大致就停留在这么一个阶段:对MySQL可 ...

  2. 攻防世界PWN简单题 level0

    攻防世界PWN简单题 level0 开始考验栈溢出的相关知识了 Checksec 一下文件 看看都开了什么保护 和 是多少位的程序 发现是64位的程序, 扔进IDA64.IDA YYDS.. 进入主函 ...

  3. 【编程语言】Matlab 学习记录

    title: Matlab Learning Record date: 2020-05-23 20:11:26 author: liudongdong1 img: https://gitee.com/ ...

  4. Specification使用notin

    废话不多说直接贴代码 Specification<Employee> employeeSpecification = new Specification<Employee>() ...

  5. C#硬件访问(摄像头、麦克风)

    #需要引用:AForge类库.Microsoft.DirectX using System;using System.Windows.Forms;namespace CameraTest{ publi ...

  6. nodejs根据word模板生成文档(方法二)

    [推荐该方法,模板比较简洁] 1,代码, 这里采用的模块为 docxtemplater 和 open-docxtemplater-image-module,均为开源(docxtemplater 有收费 ...

  7. 十一:JavaWeb中的监听器(二)

    一.监听域对象中属性的变更的监听器 域对象中属性的变更的事件监听器就是用来监听 ServletContext, HttpSession, HttpServletRequest 这三个对象中的属性变更信 ...

  8. Mysql You can't specify target table 'newsalrecord' for update in FROM clause

    这个问题是不能先select出同一表中的某些值,再update这个表(在同一语句中),即不能依据某字段值做判断再来更新某字段的值.解决办法就是建立个临时的表.

  9. GUI容器之Frame

    Frame public class MyFrame { public static void main(String[] args) { //创建一个Frame对象 Frame frame = ne ...

  10. introduction-to-64-bit-assembly

    introduction-to-64-bit-assembly NASM - The Netwide Assembler x86-64 下函数调用及栈帧原理 汇编语言基本概念简介 mycode