php高级 TP+Redis实现发布订阅和消息推送案例实战

Redis 的发布-订阅模型是一种消息通信模式,它允许客户端之间通过特定的频道进行通信。在这种模型中,有些客户端负责发布消息(发布者),而其他客户端则订阅它们感兴趣的频道并接收这些消息(订阅者)。

以下是 Redis 发布订阅的基本操作:

  1. SUBSCRIBE:订阅者使用此命令订阅一个或多个频道的消息。
  2. PUBLISH:发布者使用此命令向指定的频道发送消息。
  3. UNSUBSCRIBE:订阅者使用此命令取消订阅一个或多个频道的消息。
  4. PUNSUBSCRIBE:订阅者使用此命令取消订阅所有频道的消息。

这种模型非常适合实现实时应用,如实时通知、实时分析、实时数据更新等。

第一步: 在index/controller文件中创建个控制器 Publish.php 文件

php 复制代码
<?php
namespace app\mainapp\controller;

use app\mainapp\BaseController;
use think\cache\driver\Redis;
class Publish extends BaseController
{		
		protected $redis;
		
		public function __construct(){
			$redis = new Redis(Config::get('cache.stores.redis'));
			$redis->connect('127.0.0.1',6379);
			$this->redis = $redis;
			
		}
		
		//发布消息的控制器方法
		public function index()
		{
			
			//$this->redis->publish('频道名称','发布内容');
			//定义一个频道方法,往这个频道发布消息,频道名称:中英文都可以
			$res = $this->redis->publish('sixStar:index','发布内容');
			var_dump('发布订阅消息成功,接受者数量为:'.$res);
			
			//关闭
			$this->redis->close();
		}



		//订阅多个频道:
		//api
		public function api()
		{
			$this->redis->publish('sixStar:api','api平台开发专栏');
		}
		
		//swoole
		public function swoole()
		{
			$this->redis->publish('sixStar:swoole','swoole网络编程专栏');
		}
}

第二步 在项目 application/command.php中 加入一条指令,可参考 TP 添加定时任务 - 自定义指令

php 复制代码
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: dqh <dqh@163.com>
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------
// | 控制台配置
// +----------------------------------------------------------------------
return [
    // 指令定义
    'commands' => [
		'hello' => 'app\command\Subscribe',
    ],
];
php 复制代码
<?php
/**
 * 自定义指令 - tp+redis实现发布订阅和消息推送
 */
namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\cache\driver\Redis;


class Subscribe extends Command
{
    protected function configure()
    {
        $this->setName('subscribe')->setDescription('接收订阅消息');
    }

    protected function execute(Input $input, Output $output)
    {
		
		$redis = new Redis();
			$redis->connect('127.0.0.1', 6379);
			
			//订阅这个频道,获取频道消息
			/*$res = $redis->subscribe(['sixStar:index'],function($instance,$channel,$message){//实例,频道,消息
				//$res = $redis->subscribe(['sixStar:index',''],function($instance,$channel,$message){//实例,频道,消息
				var_dump($message);
				//业务逻辑:发送短信,推送给用户等等
			});
			*/
			
			//匹配适应规则的所有的频道消息
			$res = $redis->psubscribe(['sixStar:*'],function($instance,$rule,$channel,$message){//实例,规则,频道,消息
				var_dump($message);
				//业务逻辑:发送短信,推送给用户等等
			});
			
        $output->writeln( date('Y-m-d H:i:s'));//输出内容
    }
}

第二步 执行

php think

会有一条 subscribe 指令的命令

php think subscribe

这样就订阅成功了

用postman 请求 Publish/index 发布消息的控制器方法 就能收到一条订阅

相关推荐
运维全栈笔记8 小时前
K8S部署Redis高可用全攻略:1主2从3哨兵架构实战
redis·docker·云原生·容器·架构·kubernetes·bootstrap
dog25010 小时前
圆锥曲线和二次曲线
开发语言·网络·人工智能·算法·php
凯瑟琳.奥古斯特11 小时前
Redis是什么及核心特性
前端·css·redis·缓存
KmSH8umpK11 小时前
Redis分布式锁从原生手写到Redisson高阶落地,附线上死锁复盘优化方案进阶第三篇
redis·分布式·wpf
千寻girling14 小时前
五一劳动节快乐 [特殊字符][特殊字符][特殊字符]
java·c++·git·python·学习·github·php
HUGu RGIN14 小时前
Linux部署Redis集群
linux·运维·redis
KmSH8umpK14 小时前
SpringBoot 分布式锁实战:从单机锁到Redis分布式锁全覆盖,解决超卖、重复下单、幂等并发问题
spring boot·redis·分布式
手握风云-16 小时前
Redis:不只是缓存那么简单(六)
redis·缓存
ERBU DISH16 小时前
Linux下启动redis
linux·redis·bootstrap
xxjj998a16 小时前
Laravel3.x:奠定现代PHP框架的重要里程碑
android·开发语言·php