Github 仓库

demo-redis-subscribe

创建项目

$ composer create hyperf/biz-skeleton demo-redis-subscribe dev-master
Installing hyperf/biz-skeleton (dev-master 1eaa35a957cf704a5c9959c68e426a614c7598a2)
- Installing hyperf/biz-skeleton (dev-master 1eaa35a): Cloning 1eaa35a957 from cache
Created project in demo-redis-subscribe
> @php -r "file_exists('.env') || copy('.env.example', '.env');"
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 133 installs, 0 updates, 0 removals
- Installing ocramius/package-versions (1.4.0): Loading from cache
- Installing hyperf/contract (dev-master 1624d1c): Cloning 1624d1ce0e from cache
- Installing doctrine/inflector (v1.3.0): Loading from cache
- Installing hyperf/utils (dev-master c847116): Cloning c847116cf9 from cache
- Installing psr/container (1.0.0): Loading from cache
- Installing hyperf/pool (dev-master 5a67570): Cloning 5a67570f71 from cache
- Installing psr/event-dispatcher (1.0.0): Loading from cache
- Installing hyperf/process (dev-master 75f4fbf): Cloning 75f4fbfa56 from cache
- Installing doctrine/instantiator (1.2.0): Loading from cache
- Installing psr/log (1.1.0): Loading from cache
- Installing php-amqplib/php-amqplib (v2.10.0): Loading from cache
- Installing hyperf/amqp (dev-master 1cff378): Cloning 1cff378bc4 from cache
- Installing symfony/service-contracts (v1.1.6): Loading from cache
- Installing symfony/polyfill-php73 (v1.12.0): Loading from cache
- Installing symfony/polyfill-mbstring (v1.12.0): Loading from cache
- Installing symfony/console (v4.3.4): Loading from cache
- Installing hyperf/command (dev-master e4caea4): Cloning e4caea4c70 from cache
- Installing hyperf/async-queue (dev-master 8d28229): Cloning 8d282297cd from cache
- Installing psr/simple-cache (1.0.1): Loading from cache
- Installing hyperf/cache (dev-master 60997fb): Cloning 60997fb05c from cache
- Installing hyperf/circuit-breaker (dev-master e41726f): Cloning e41726fd25 from cache
- Installing symfony/finder (v4.3.4): Loading from cache
- Installing symfony/polyfill-ctype (v1.12.0): Loading from cache
- Installing phpoption/phpoption (1.5.0): Loading from cache
- Installing vlucas/phpdotenv (v3.6.0): Loading from cache
- Installing hyperf/config (dev-master 44f5ef8): Cloning 44f5ef8a10 from cache
- Installing psr/http-message (1.0.1): Loading from cache
- Installing fig/http-message-util (1.1.3): Loading from cache
- Installing hyperf/framework (dev-master 3be169b): Cloning 3be169b9f4 from cache
- Installing hyperf/event (dev-master e9d97f1): Cloning e9d97f11fb from cache
- Installing php-di/phpdoc-reader (2.1.0): Loading from cache
- Installing doctrine/lexer (1.1.0): Loading from cache
- Installing doctrine/annotations (v1.7.0): Loading from cache
- Installing nikic/php-parser (v4.2.4): Loading from cache
- Installing hyperf/di (dev-master 7f82227): Cloning 7f822276a0 from cache
- Installing hyperf/constants (dev-master a3baaf8): Cloning a3baaf8bbd from cache
- Installing hyperf/devtool (dev-master c3f424f): Cloning c3f424f2c6 from cache
- Installing ralouphie/getallheaders (3.0.3): Loading from cache
- Installing guzzlehttp/psr7 (1.6.1): Loading from cache
- Installing guzzlehttp/promises (v1.3.1): Loading from cache
- Installing guzzlehttp/guzzle (6.3.3): Loading from cache
- Installing hyperf/guzzle (dev-master 90ad6e4): Cloning 90ad6e497b from cache
- Installing react/promise (v2.7.1): Loading from cache
- Installing guzzlehttp/streams (3.0.0): Loading from cache
- Installing guzzlehttp/ringphp (1.1.1): Loading from cache
- Installing elasticsearch/elasticsearch (v6.7.2): Loading from cache
- Installing hyperf/elasticsearch (dev-master c936fc0): Cloning c936fc0f24 from cache
- Installing monolog/monolog (1.25.1): Loading from cache
- Installing hyperf/logger (dev-master 937101c): Cloning 937101c682 from cache
- Installing hyperf/memory (dev-master 17003ec): Cloning 17003ec1e9 from cache
- Installing hyperf/paginator (dev-master 4ffeb0e): Cloning 4ffeb0e46c from cache
- Installing symfony/translation-contracts (v1.1.6): Loading from cache
- Installing symfony/translation (v4.3.4): Loading from cache
- Installing nesbot/carbon (2.24.0): Loading from cache
- Installing hyperf/database (dev-master d36785e): Cloning d36785e709 from cache
- Installing hyperf/model-listener (dev-master 5a77c83): Cloning 5a77c83cbe from cache
- Installing hyperf/db-connection (dev-master 17eec65): Cloning 17eec65d60 from cache
- Installing hyperf/model-cache (dev-master e3c5293): Cloning e3c5293a56 from cache
- Installing hyperf/redis (dev-master 6043083): Cloning 6043083650 from cache
- Installing hyperf/server (dev-master b0c8bd9): Cloning b0c8bd9e92 from cache
- Installing zendframework/zend-stdlib (3.2.1): Loading from cache
- Installing zendframework/zend-mime (2.7.1): Loading from cache
- Installing hyperf/http-message (dev-master dcd1d78): Cloning dcd1d78b88 from cache
- Installing psr/http-server-handler (1.0.1): Loading from cache
- Installing psr/http-server-middleware (1.0.1): Loading from cache
- Installing hyperf/dispatcher (dev-master 670f7dc): Cloning 670f7dca4f from cache
- Installing hyperf/exception-handler (dev-master d1f3511): Cloning d1f3511c9c from cache
- Installing nikic/fast-route (v1.3.0): Loading from cache
- Installing hyperf/http-server (dev-master 2744b1a): Cloning 2744b1a949 from cache
- Installing phpunit/php-timer (2.1.2): Loading from cache
- Installing sebastian/environment (4.2.2): Loading from cache
- Installing sebastian/version (2.0.1): Loading from cache
- Installing sebastian/resource-operations (2.0.1): Loading from cache
- Installing sebastian/object-reflector (1.1.1): Loading from cache
- Installing sebastian/recursion-context (3.0.0): Loading from cache
- Installing sebastian/object-enumerator (3.0.3): Loading from cache
- Installing sebastian/global-state (2.0.0): Loading from cache
- Installing sebastian/exporter (3.1.2): Loading from cache
- Installing sebastian/diff (3.0.2): Loading from cache
- Installing sebastian/comparator (3.0.2): Loading from cache
- Installing phpunit/php-text-template (1.2.1): Loading from cache
- Installing phpunit/php-file-iterator (2.0.2): Loading from cache
- Installing theseer/tokenizer (1.1.3): Loading from cache
- Installing sebastian/code-unit-reverse-lookup (1.0.1): Loading from cache
- Installing phpunit/php-token-stream (3.1.1): Loading from cache
- Installing phpunit/php-code-coverage (6.1.4): Loading from cache
- Installing webmozart/assert (1.5.0): Loading from cache
- Installing phpdocumentor/reflection-common (2.0.0): Loading from cache
- Installing phpdocumentor/type-resolver (1.0.1): Loading from cache
- Installing phpdocumentor/reflection-docblock (4.3.2): Loading from cache
- Installing phpspec/prophecy (1.8.1): Loading from cache
- Installing phar-io/version (2.0.1): Loading from cache
- Installing phar-io/manifest (1.0.3): Loading from cache
- Installing myclabs/deep-copy (1.9.3): Loading from cache
- Installing phpunit/phpunit (7.5.16): Loading from cache
- Installing hyperf/testing (dev-master 910f9e9): Cloning 910f9e94db from cache
- Installing swoft/swoole-ide-helper (v4.4.6): Loading from cache
- Installing symfony/filesystem (v4.3.4): Loading from cache
- Installing symfony/config (v4.3.4): Loading from cache
- Installing symfony/dependency-injection (v4.3.4): Loading from cache
- Installing pdepend/pdepend (2.5.2): Loading from cache
- Installing phpmd/phpmd (2.7.0): Loading from cache
- Installing symfony/stopwatch (v4.3.4): Loading from cache
- Installing symfony/process (v4.3.4): Loading from cache
- Installing symfony/polyfill-php72 (v1.12.0): Loading from cache
- Installing paragonie/random_compat (v9.99.99): Loading from cache
- Installing symfony/polyfill-php70 (v1.12.0): Loading from cache
- Installing symfony/options-resolver (v4.3.4): Loading from cache
- Installing symfony/event-dispatcher-contracts (v1.1.5): Loading from cache
- Installing symfony/event-dispatcher (v4.3.4): Loading from cache
- Installing php-cs-fixer/diff (v1.3.0): Loading from cache
- Installing composer/xdebug-handler (1.3.3): Loading from cache
- Installing composer/semver (1.5.0): Loading from cache
- Installing friendsofphp/php-cs-fixer (v2.15.3): Loading from cache
- Installing hamcrest/hamcrest-php (v2.0.0): Loading from cache
- Installing mockery/mockery (1.2.3): Loading from cache
- Installing doctrine/reflection (v1.0.0): Loading from cache
- Installing doctrine/event-manager (v1.0.0): Loading from cache
- Installing doctrine/collections (v1.6.2): Loading from cache
- Installing doctrine/cache (v1.8.0): Loading from cache
- Installing doctrine/persistence (1.1.1): Loading from cache
- Installing doctrine/common (v2.11.0): Loading from cache
- Installing phpstan/phpdoc-parser (0.3.5): Loading from cache
- Installing nette/utils (v3.0.1): Loading from cache
- Installing nette/schema (v1.0.0): Loading from cache
- Installing nette/finder (v2.5.1): Loading from cache
- Installing nette/robot-loader (v3.2.0): Loading from cache
- Installing nette/php-generator (v3.2.3): Loading from cache
- Installing nette/neon (v3.0.0): Loading from cache
- Installing nette/di (v3.0.1): Loading from cache
- Installing nette/bootstrap (v3.0.0): Loading from cache
- Installing jean85/pretty-package-versions (1.2): Loading from cache
- Installing phpstan/phpstan (0.11.16): Loading from cache
hyperf/utils suggests installing symfony/var-dumper (Required to use the dd function (^4.1).)
hyperf/utils suggests installing symfony/serializer (Required to use SymfonyNormalizer (^4.3))
hyperf/utils suggests installing symfony/property-access (Required to use SymfonyNormalizer (^4.3))
symfony/console suggests installing symfony/lock
guzzlehttp/psr7 suggests installing zendframework/zend-httphandlerrunner (Emit PSR-7 responses)
monolog/monolog suggests installing graylog2/gelf-php (Allow sending log messages to a GrayLog2 server)
monolog/monolog suggests installing sentry/sentry (Allow sending log messages to a Sentry server)
monolog/monolog suggests installing doctrine/couchdb (Allow sending log messages to a CouchDB server)
monolog/monolog suggests installing ruflin/elastica (Allow sending log messages to an Elastic Search server)
monolog/monolog suggests installing ext-amqp (Allow sending log messages to an AMQP server (1.0+ required))
monolog/monolog suggests installing ext-mongo (Allow sending log messages to a MongoDB server)
monolog/monolog suggests installing mongodb/mongodb (Allow sending log messages to a MongoDB server via PHP Driver)
monolog/monolog suggests installing aws/aws-sdk-php (Allow sending log messages to AWS services like DynamoDB)
monolog/monolog suggests installing rollbar/rollbar (Allow sending log messages to Rollbar)
monolog/monolog suggests installing php-console/php-console (Allow sending log messages to Google Chrome)
symfony/translation suggests installing symfony/yaml
hyperf/database suggests installing doctrine/dbal (Required to rename columns (^2.6).)
zendframework/zend-mime suggests installing zendframework/zend-mail (Zend\Mail component)
sebastian/global-state suggests installing ext-uopz (*)
phpunit/php-code-coverage suggests installing ext-xdebug (^2.6.0)
phpunit/phpunit suggests installing phpunit/php-invoker (^2.0)
phpunit/phpunit suggests installing ext-xdebug (*)
symfony/config suggests installing symfony/yaml (To use the yaml reference dumper)
symfony/dependency-injection suggests installing symfony/yaml
symfony/dependency-injection suggests installing symfony/expression-language (For using expressions in service container configuration)
symfony/dependency-injection suggests installing symfony/proxy-manager-bridge (Generate service proxies to lazy load them)
paragonie/random_compat suggests installing ext-libsodium (Provides a modern crypto API that can be used to generate random bytes.)
symfony/event-dispatcher suggests installing symfony/http-kernel
friendsofphp/php-cs-fixer suggests installing php-cs-fixer/phpunit-constraint-isidenticalstring (For IsIdenticalString constraint.)
friendsofphp/php-cs-fixer suggests installing php-cs-fixer/phpunit-constraint-xmlmatchesxsd (For XmlMatchesXsd constraint.)
doctrine/cache suggests installing alcaeus/mongo-php-adapter (Required to use legacy MongoDB driver)
nette/bootstrap suggests installing tracy/tracy (to use Configurator::enableTracy())
Writing lock file
Generating autoload files
ocramius/package-versions: Generating version class...
ocramius/package-versions: ...done generating version class
Do you want to remove the existing VCS (.git, .svn..) history? [Y,n]? Y

  

定义队列

我们希望可以监听多个队列,所以便把队列名字额外定义出来。

<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/ namespace App\Constants; class Channel
{
const QUEUE = 'channel.queue'; const TEST = 'channel.test'; public static function getArray()
{
return [
self::QUEUE,
self::TEST,
];
}
}

  

创建用于订阅的 Redis 实例

更新配置

autoload/redis.php 中新增 subscriber 相关配置

<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/ return [
'default' => [
'host' => env('REDIS_HOST', 'localhost'),
'auth' => env('REDIS_AUTH', null),
'port' => (int) env('REDIS_PORT', 6379),
'db' => (int) env('REDIS_DB', 0),
'pool' => [
'min_connections' => 1,
'max_connections' => 10,
'connect_timeout' => 10.0,
'wait_timeout' => 3.0,
'heartbeat' => -1,
'max_idle_time' => (float) env('REDIS_MAX_IDLE_TIME', 60),
],
],
'subscriber' => [
'host' => env('REDIS_HOST', 'localhost'),
'auth' => env('REDIS_AUTH', null),
'port' => (int) env('REDIS_PORT', 6379),
'db' => (int) env('REDIS_DB', 0),
'options' => [
\Redis::OPT_READ_TIMEOUT => '-1',
],
'pool' => [
'min_connections' => 1,
'max_connections' => 10,
'connect_timeout' => 10.0,
'wait_timeout' => 3.0,
'heartbeat' => -1,
'max_idle_time' => (float) env('REDIS_MAX_IDLE_TIME', 60),
],
],
];

  

增加 Subscriber 客户端

<?php
<?php declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/ namespace App\Process; use App\Constants\Channel;
use App\Kernel\Subscriber;
use Hyperf\Process\AbstractProcess;
use Hyperf\Process\Annotation\Process; /**
* @Process(name="RedisConsumerProcess")
*/
class RedisConsumerProcess extends AbstractProcess
{
public function handle(): void
{
$redis = di()->get(Subscriber::class); $redis->subscribe(Channel::getArray(), function ($instance, $channelName, $message) {
var_dump($instance);
var_dump($channelName);
var_dump($message); // TODO: 执行对应的消费操作
});
}
}

  

测试代码

修改 IndexController 如下

<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/ namespace App\Controller; use App\Constants\Channel; class IndexController extends Controller
{
public function index()
{
$user = $this->request->input('user', 'Hyperf');
$method = $this->request->getMethod(); $redis = di()->get(\Redis::class); $redis->publish(Channel::TEST, $user); return $this->response->success([
'user' => $user,
'method' => $method,
'message' => 'Hello Hyperf.',
]);
}
}

  

启动 Server,测试结果

$ curl http://127.0.0.1:9501/
{"code":0,"data":{"user":"Hyperf","method":"GET","message":"Hello Hyperf."}}

  

终端显示如下
object(Redis)#47173 (0) {
}
string(12) "channel.test"
string(6) "Hyperf"

  

未完待续

...

更多学习内容请访问:

腾讯T3-T4标准精品PHP架构师教程目录大全,只要你看完保证薪资上升一个台阶(持续更新)

基于 Redis 的订阅与发布的更多相关文章

  1. 利用redis的订阅和发布来实现实时监控的一个DEMO(Python版本)

    redis的list类型有个很好的特性,就是每次添加元素后会返回当前list的长度,利用这个特点,我们可以监控它的长度,比如我们的key是用户注册的IP地址,list中存放的是已经在此IP地址上注册的 ...

  2. redis的订阅和发布

    #订阅和发布有什么用呢?# 特点# 1.实现一个一对多的效果,只有一个发布者,多个订阅者# 2.实时的发布消息,广播方发布消息,所有的订阅者都会受到消息,一个人同时只能接受#一个频道 1.先写一个公共 ...

  3. 在php中实现Redis的订阅与发布

    <?php //require_once dirname(__FILE__).'/class/RedisClass.class.php'; function init_redis(){ $red ...

  4. Redis消息订阅与发布

    监听器的创建 package com.sogou.baike.testimport.testSubscribe; import redis.clients.jedis.JedisPubSub; pub ...

  5. 基于Redis的消息订阅/发布

    在工业生产设计中,我们往往需要实现一个基于消息订阅的模式,用来对非定时的的消息进行监听订阅. 这种设计模式在 总线设计模式中得到体现.微软以前的WCF中实现了服务总线 ServiceBus的设计模式. ...

  6. 基于Redis消息的订阅发布应用场景

    目录 基于Redis消息的订阅发布应用场景 1.应用背景 2.困境 2.1 锁表风险 2.2 实时性差 2.3 增加编程复杂性 2.4 实时效果 3.解决方案 3.1 前端传值给服务端 3.2 服务端 ...

  7. 基于redis的消息订阅与发布

    Redis 的 SUBSCRIBE 命令可以让客户端订阅任意数量的频道, 每当有新信息发送到被订阅的频道时, 信息就会被发送给所有订阅指定频道的客户端. 作为例子, 下图展示了频道 channel1  ...

  8. 分布式消息总线,基于.NET Socket Tcp的发布-订阅框架之离线支持,附代码下载

    一.分布式消息总线以及基于Socket的实现 在前面的分享一个分布式消息总线,基于.NET Socket Tcp的发布-订阅框架,附代码下载一文之中给大家分享和介绍了一个极其简单也非常容易上的基于.N ...

  9. Spring Boot使用Redis进行消息的发布订阅

    今天来学习如何利用Spring Data对Redis的支持来实现消息的发布订阅机制.发布订阅是一种典型的异步通信模型,可以让消息的发布者和订阅者充分解耦.在我们的例子中,我们将使用StringRedi ...

随机推荐

  1. hdu1908 逆序对

    题目链接:https://www.luogu.com.cn/problem/P1908 这个题不要以为拿到手就可以树状数组秒,本题的数据范围是1e9显然简单的树状数组是空间不够的,点个数有5e5,所以 ...

  2. Mysql性能优化:如何给字符串加索引?

    导读 现代大部分的登录系统都支持邮箱.手机号码登录两种方式,那么如何在邮箱或者手机号码这个字符串上建立索引才能保证性能最佳呢? 今天这篇文章就来探讨一下在Mysql中如何给一个字符串加索引才能达到性能 ...

  3. WEB缓存系统之varnish基础入门(一)

    前文我们聊了下http协议里的缓存控制机制以及varnish架构组件介绍,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/12620538.html:今天我们来聊 ...

  4. 从零开始发布一个ArcGIS Server地图服务

    @ 目录 一.软件环境搭建 1.数据库安装 1.1.Oracle(可选) 1.1.1.安装Oracle服务端 1.1.2.安装Oracle客户端 1.2.PostgreSQL(可选) 1.2.1.安装 ...

  5. ios 13 陀螺仪DeviceOrientationEvent需要申请用户权限

    有些代码跑着跑着就报错了,有些陀螺仪用着用着就不能用了. 目前时间是2020.4.4,发现  ios 13系统调用陀螺仪 需要申请用户权限(这应该是个趋势,后面安卓应该陆续跟进).具体实现来看下: 1 ...

  6. iPhone连接到Mac上叮叮叮断断续续响个不停的解决办法

    一.推荐方式 1.让iPhone和Mac通过数据线连接(对,就是连着) 2.打开终端,执行如下命令: sudo killall -STOP -c usbd 3.一分钟内,iPhone即可连上Mac 二 ...

  7. 剑指offer刷题总结

    ★ 二维数组的查找 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否 ...

  8. 利用Python批量重命名文件夹下文件

    #!/usr/bin/python # -*- coding: UTF-8 -*- # -*- coding:utf8 -*- import os from string import digits ...

  9. Spring-Cloud-Netflix-系统架构

    目录 系统架构 概述 集中式架构 概述 特点 垂直拆分 概述 特点 系统架构分类 微服务 微服务的特点: 分布式服务: 微服务和分布式的区别: 微服务要面临的问题: springClould是什么 远 ...

  10. 为何关键字static在面试中频频被问?

    关键字static的神奇妙用在今天的学习中,我了解到关键字static的作用,下面我来给大家分享一下.①static 修饰局部变量只改变了变量的生命周期,让静态局部变量出了作用域依然存在,到程序结束生 ...