php laravel v5.1 消息队列
* install https://laravel.com/docs/5.1#installationcomposer 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 消息队列的更多相关文章
- laravel的延迟消息队列
laravel的延迟消息队列 这篇来自于看到朋友转的58沈剑的一篇文章:1分钟实现"延迟消息"功能(http://mp.weixin.qq.com/s?__biz=MjM5ODYx ...
- laravel的消息队列剖析
laravel的消息队列剖析 这篇来自于看到朋友转的58沈剑的一篇文章:1分钟实现"延迟消息"功能 在实际工作中也不止遇见过一次这个问题,我在想着以前是怎么处理的呢?我记得当初在上 ...
- Laravel消息队列怎么使用
使用database驱动做队列 下面是简单使用教程 1. 修改.env文件配置 QUEUE_CONNECTION=sync改成QUEUE_CONNECTION=database 默认的sync是同步队 ...
- redis实现消息队列
业务需求 本文是以laravel框架来介绍redis队列,具体用法你可以参考http://www.cnblogs.com/lengthuo/p/7277260.html最近接受一个很简单的东西,(说起 ...
- 消息队列Queue大全
消息队列Queue大全 (http://queues.io/) 作业队列,消息队列和其他队列.几乎所有你能想到的都在这. 关于 那里有很多排队系统.他们每个人都不同,是为解决某些问题而创建的.这个页面 ...
- Lumen开发:结合Redis实现消息队列(3)
4.运行队列监听器 开启任务监听器 Lumen包含了一个Artisan命令用来运行推送到队列的新任务.你可以使用queue:listen命令运行监听器: php artisan queue:liste ...
- Lumen开发:结合Redis实现消息队列(1)
1.简介 Lumen队列服务为各种不同的后台队列提供了统一的API.队列允许你推迟耗时任务(例如发送邮件)的执行,从而大幅提高web请求速度. 1.1 配置 .env文件的QUEUE_DRIVER选项 ...
- php和redis怎么实现消息队列
把瞬间服务器的请求处理换成异步处理,缓解服务器的压力,实现数据顺序排列获取.本文主要和大家分享php和redis如何实现消息队列,希望能帮助到大家. redis实现消息队列步骤如下: 1).redis ...
- 消息队列——RabbitMQ学习笔记
消息队列--RabbitMQ学习笔记 1. 写在前面 昨天简单学习了一个消息队列项目--RabbitMQ,今天趁热打铁,将学到的东西记录下来. 学习的资料主要是官网给出的6个基本的消息发送/接收模型, ...
随机推荐
- explorer.exe
explorer.exe是Windows程序管理器或者文件资源管理器, 它用于管理Windows图形壳,包括桌面和文件管理,删除该程序会导致Windows图形界面无法使用. 终止: taskkill ...
- 【springcloud alibaba】配置中心之nacos
接着上一篇的[springcloud alibaba]注册中心之nacos,这一篇主要讲nacos的配置中心能力.nacos的集群部署及持久化请看上一篇. ---------------------- ...
- uwp 之后台音频
C# code 后台任务 ---------------------------- public sealed class BgTask : IBackgroundTask { #region 私有字 ...
- BootstrapTable插件的使用 【转】
一.什么是Bootstrap-table? 在业务系统开发中,对表格记录的查询.分页.排序等处理是非常常见的,在Web开发中,可以采用很多功能强大的插件来满足要求,且能极大的提高开发效率,本随笔介绍这 ...
- Redis脑图
转自:http://alphawang.com/blog/redis-mind-map/ 最近梳理了下 Redis 知识图谱,画了个脑图,涵盖了 Redis 数据类型.持久化机制.主从.哨兵.集群.应 ...
- 高德地图&兴趣点(poi)
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- php ltrim() rtrim() trim()删除字符空格
php$str=" 去除前后空格 ";echo "方括号中为原始字符串:[".$str."]";echo "原始字符串长度:&qu ...
- python常用工具库介绍
Numpy:科学计算 HOME: http://www.numpy.org/ NumPy is the fundamental package for scientific computing wi ...
- 遇到Web页面禁用鼠标右键操作时,该如何解禁?
在使用Selenium做Web UI自动化测试过程中,经常需要鼠标右击Web页面检查DOM节点,用于获取Web元素的定位信息.一般情况下,绝大多数页面都是能够响应鼠标右击操作的.但出于某些目的,有些W ...
- (九)羽夏看C语言——C++番外篇
写在前面 此系列是本人一个字一个字码出来的,包括示例和实验截图.本人非计算机专业,可能对本教程涉及的事物没有了解的足够深入,如有错误,欢迎批评指正. 如有好的建议,欢迎反馈.码字不易,如果本篇文章 ...