A lightweight queue based on Redis Stream for webman plugin.

Overview

workbunny

workbunny/webman-rqueue

🐇 A lightweight queue based on Redis Stream for webman plugin. 🐇

A lightweight queue based on Redis Stream for webman plugin

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

常见问题

  1. 什么时候使用消息队列?

    当你需要对系统进行解耦、削峰、异步的时候;如发送短信验证码、秒杀活动、资产的异步分账清算等。

  2. RabbitMQ和Redis的区别?

    Redis中的Stream的特性同样适用于消息队列,并且也包含了比较完善的ACK机制,但在一些点上与RabbitMQ存在不同:

    • Redis Stream没有完善的后台管理;RabbitMQ拥有较为完善的后台管理及Api;
    • Redis的持久化策略取舍:默认的RDB策略极端情况下存在丢失数据,AOF策略则需要牺牲一些性能;RabbitMQ持久化方案更多,可对消息持久化也可对队列持久化;
    • RabbitMQ拥有更多的插件可以提供更完善的协议支持及功能支持;
  3. 什么时候使用Redis?什么时候使用RabbitMQ?

    当你的队列使用比较单一或者比较轻量的时候,请选用 Redis Stream;当你需要一个比较完整的消息队列体系,包括需要利用交换机来绑定不同队列做一些比较复杂的消息任务的时候,请选择RabbitMQ;

    当然,如果你的队列使用也比较单一,但你需要用到一些管理后台相关系统化的功能的时候,又不想花费太多时间去开发的时候,也可以使用RabbitMQ;因为RabbitMQ提供了一整套后台管理的体系及 HTTP API 供开发者兼容到自己的管理后台中,不需要再消耗多余的时间去开发功能;

    注:这里的 轻量 指的是 无须将应用中的队列服务独立化,该队列服务是该应用独享的

简介

基于Redis Stream的轻量级队列;

简单易用高效,可以轻易的实现master/worker的队列模式(一个队列多个消费者);

支持延迟消息;

安装

composer require workbunny/webman-rqueue

使用

创建Builder

Builder 可以理解为类似 ORMModel,创建一个 Builder 就对应了一个队列;使用该 Builder 对象进行 publish() 时,会向该队列投放消息;创建多少个 Builder 就相当于创建了多少条队列;

  • 创建一个消费者进程数量为1的普通队列:(在项目根目录执行)
# 在 process/workbunny/rqueue 目录下创建 TestBuilder.php
./webman workbunny:rqueue-builder test 1
  • 创建一个消费者进程数量为1的延迟队列:(在项目根目录执行)
# 在 process/workbunny/rqueue 目录下创建 TestBuilderDelayed.php
./webman workbunny:rqueue-builder test 1 -d
#
./webman workbunny:rqueue-builder test 1 --delayed
  • 命令支持二级菜单
# 在 process/workbunny/rqueue/project 目录下创建 TestBuilder.php
./webman workbunny:rqueue-builder project/test 1

# 延迟同理

说明:

  • 命令结构:
workbunny:rqueue-builder [-d|--delayed] [--] <name> <count>

# 【必填】 name:Builder名称
# 【必填】count:启动的消费者进程数量
# 【选填】-d/--delayed:是否是延迟队列
  • 在项目根目录下命令会在 process/workbunny/rqueue 路径下创建一个Builder,并且将该Builder自动加入 config/plugin/workbunny/webman-rqueue/process.php 配置中作为自定义进程启动;(如不需要自动加载消费者进程,请自行注释该配置)

  • 消费是异步的,不会阻塞当前进程,不会影响 webman/workermanstatus

  • Builder文件结构入下,可自行调整类属性:

<?php
declare(strict_types=1);

namespace process\workbunny\rqueue;

use Illuminate\Redis\Connections\Connection;
use Workbunny\WebmanRqueue\FastBuilder;

class TestBuilder extends FastBuilder
{
    // 默认的redis连接配置
    protected string $connection = 'default';
    // 消费组QOS
    protected int $prefetch_count = 1;
    // 队列最大数量
    protected int $queue_size = 4096;
    // 是否延迟队列
    protected bool $delayed = false;
    // 消费回调
    public function handler(string $msgid, array $msgvalue, Connection $connection): bool
    {
    	var_dump($msgid); # 消息id
        var_dump($msgvalue); # 消息体
        return true; // ack
        # false // nack
        # throw // nack
    }
}

移除Builder

该命令会移除process.php中的配置及对应Builder文件;

  • 移除名为 test 的普通队列:(在项目根目录执行)
./webman workbunny:rqueue-remove test
  • 移除名为 test 的延迟队列:(在项目根目录执行)
./webman workbunny:rqueue-remove test -d
#
./webman workbunny:rqueue-remove test --delayed
  • 关闭名为 test 的普通队列:(在项目根目录执行)
./webman workbunny:rqueue-remove test -c
#
./webman workbunny:rqueue-remove test --close

查看Builder

./webman workbunny:rqueue-list

注:当 Builder 未启动时,handler 与 count 显示为 --

+----------+-----------------------------------------------------------------------+-------------------------------------------------+-------+
| name     | file                                                                  | handler                                         | count |
+----------+-----------------------------------------------------------------------+-------------------------------------------------+-------+
| test     | /var/www/your-project/process/workbunny/rqueue/TestBuilder.php        | process\workbunny\rqueue\TestBuilder            | 1     |
| test -d  | /var/www/your-project/process/workbunny/rqueue/TestBuilderDelayed.php | process\workbunny\rqueue\TestBuilderDelayed     | 1     |
+----------+-----------------------------------------------------------------------+-------------------------------------------------+-------+

生产

  • 每个 Builder 各包含一个连接,使用多个 Builder 会创建多个连接

  • 生产消息默认不关闭当前连接

1. 同步发布消息

该方法会阻塞等待至消息生产成功,返回bool

  • 发布普通消息

注:向普通队列发布延迟消息会抛出一个 WebmanRqueueException 异常

use function Workbunny\WebmanRabbitMQ\sync_publish;
use process\workbunny\rqueue\TestBuilder;

sync_publish(TestBuilder::instance(), 'abc'); # return bool
  • 发布延迟消息

注:向延迟队列发布普通消息会抛出一个 WebmanRqueueException 异常

use function Workbunny\WebmanRabbitMQ\sync_publish;
use process\workbunny\rqueue\TestBuilder;

# 延迟10秒
sync_publish(TestBuilder::instance(), 'abc', 10000); # return bool

说明

  • 小范围生产验证中,欢迎 issue 和 PR

  • Redis Stream 本身没有 delayednon-delayed 之分,组件代码将它们区分的原因是不希望 delayed 被滥用;开发者应该明确哪些消息是延迟的、哪些是立即的,并且明确体现,也方便维护,因为延迟消息过多会导致消息堆积,从而占用Redis过多的资源;

  • Redis Stream 的持久化依赖 Redis 本身的持久化策略,在一定情况下 Redis Stream 也并非是可靠型的消息队列;关于持久化相关内容,请仔细阅读 Redis中文文档

  • 继承实现 AbstractMessage 可以自定义Message;

  • Builder 可通过 Builder->setMessage() 可设置自定义配置;

You might also like...
Use php-fpm as a simple built-in async queue

PHP-FPM Async Queue Use php-fpm as a simple built-in async queue. Based on interoperable queue interfaces Queue Interop. Usage composer makasim/php-fp

Magento 2 Message Queue OS AMQP Broker Implementation

Magento 2 Message Queue AMQP Backend AMQP message queue backend implementation for Rcason_Mq. Installation Require the module via Composer $ composer

Queue Management Systems for LPG vendor agencies of Sri Lanka, for the LPG shortages in 2022

gas-queue-mgt Queue Management Systems for LPG vendor agencies of Sri Lanka, for the LPG shortages in 2022 Installation Requirements PHP 7.4 or later

PHP library for Disque, an in-memory, distributed job queue

disque-php A PHP library for the very promising disque distributed job queue. Features: Support for both PHP (5.5+) and HHVM No dependencies: Fast con

PHP Web User Queue designed to run on shared hosting

WebUserQueue Introduction This is a web user queueing system written in PHP with a MySQL backend designed to run on shared hosting. It was designed to

Promoting the interoperability of message queue objects.

Queue Interoperability About queue-interop tries to identify and standardize a common way for PHP programs to create, send, receive and read MQ messag

An alternative Redis session handler for PHP featuring per-session locking and session fixation protection

RedisSessionHandler An alternative Redis session handler featuring session locking and session fixation protection. News phpredis v4.1.0 (released on

Phalcon - 📕 基于Phalcon集成Composer,事件监听,中间件,MongoDB,Redis

About Phalcon 基于Phalcon的高性能PHP框架,集成Composer 支持MongoDB Redis操作,监听器,中间件,以及多语言支持 A framework which use phalcon High performance Composer Database support

A Zend_Cache backend for Redis with full support for tags (works great with Magento)

Zend_Cache backend using Redis with full support for tags This Zend_Cache backend allows you to use a Redis server as a central cache storage. Tags ar

Releases(1.0.1)
  • 1.0.1(Jul 19, 2022)

  • 1.0.0(Jul 4, 2022)

    What's Changed

    • 取消复制命令文件到app/command目录 by @fuzqing in https://github.com/workbunny/webman-rqueue/pull/1

    New Contributors

    • @fuzqing made their first contribution in https://github.com/workbunny/webman-rqueue/pull/1

    Full Changelog: https://github.com/workbunny/webman-rqueue/compare/0.0.2...1.0.0

    Source code(tar.gz)
    Source code(zip)
  • 0.0.2(Jul 2, 2022)

Owner
workbunny
An open source group that has nothing to do.
workbunny
Very easy to use a current limiting component, the code is very simple, based on the webman framework.

Very easy to use a current limiting component, the code is very simple, based on the webman framework.

nsp-team 13 Dec 29, 2022
A PHP implementation of RabbitMQ Client for webman plugin.

workbunny/webman-rabbitmq ?? A PHP implementation of RabbitMQ Client for webman plugin. ?? A PHP implementation of RabbitMQ Client for webman plugin 常

workbunny 15 Dec 15, 2022
Webman quickly creates a verification code tool similar to Google verification code

webman-captcha-grid webman quickly creates a verification code tool similar to Google verification code webman 快速创建一个类似于 Google 点图验证码的本地验证码扩展 介绍 webma

听风吹雨 6 Dec 5, 2022
Laravel illuminate/filesystem for webman

webman-tech/laravel-filesystem Laravel illuminate/filesystem for webman 介绍 站在巨人(laravel)的肩膀上使文件存储使用更加可靠和便捷 所有方法和配置与 laravel 几乎一模一样,因此使用方式完全参考 Laravel文

null 5 Dec 15, 2022
Fully covered with tests, documented by Swagger and dockerized API based on enterprise-level framework with optional queue worker.

symfony-api Fully covered with tests, documented by Swagger and dockerized API based on enterprise-level framework with optional queue worker. ⚙️ Depl

Oleksii Velychko 1 Nov 20, 2022
Redis-based session handler for Magento with optimistic locking

Cm_RedisSession A Redis-based session handler for Magento with optimistic locking. Features: Falls back to mysql handler if it can't connect to Redis.

Colin Mollenhour 216 Jan 5, 2023
Proxy based Redis cluster solution supporting pipeline and scaling dynamically

Codis is a proxy based high performance Redis cluster solution written in Go. It is production-ready and widely used at wandoujia.com and many compani

null 12.7k Jan 2, 2023
A Redis-based session handler for Magento with optimistic locking.

Cm_RedisSession A Redis-based session handler for Magento with optimistic locking. Features: Falls back to mysql handler if it can't connect to Redis.

Colin Mollenhour 215 Jan 28, 2022
Magento 2 Message Queue Open Source Module

Magento 2 Message Queue Module Lightweight implementation of message queue for Magento 2 Community Edition. System requirements This extension support

Renato 36 Sep 30, 2021
Beanstalk is a simple, fast work queue.

beanstalkd Simple and fast general purpose work queue. https://beanstalkd.github.io/ See doc/protocol.txt for details of the network protocol. Please

Beanstalkd 6.3k Jan 6, 2023