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


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

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

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

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

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

    1. namespace app\mainapp\controller;
    2. use app\mainapp\BaseController;
    3. use think\cache\driver\Redis;
    4. class Publish extends BaseController
    5. {
    6. protected $redis;
    7. public function __construct(){
    8. $redis = new Redis(Config::get('cache.stores.redis'));
    9. $redis->connect('127.0.0.1',6379);
    10. $this->redis = $redis;
    11. }
    12. //发布消息的控制器方法
    13. public function index()
    14. {
    15. //$this->redis->publish('频道名称','发布内容');
    16. //定义一个频道方法,往这个频道发布消息,频道名称:中英文都可以
    17. $res = $this->redis->publish('sixStar:index','发布内容');
    18. var_dump('发布订阅消息成功,接受者数量为:'.$res);
    19. //关闭
    20. $this->redis->close();
    21. }
    22. //订阅多个频道:
    23. //api
    24. public function api()
    25. {
    26. $this->redis->publish('sixStar:api','api平台开发专栏');
    27. }
    28. //swoole
    29. public function swoole()
    30. {
    31. $this->redis->publish('sixStar:swoole','swoole网络编程专栏');
    32. }
    33. }

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

    1. // +----------------------------------------------------------------------
    2. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
    3. // +----------------------------------------------------------------------
    4. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
    5. // +----------------------------------------------------------------------
    6. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
    7. // +----------------------------------------------------------------------
    8. // | Author: dqh
    9. // +----------------------------------------------------------------------
    10. // +----------------------------------------------------------------------
    11. // | 控制台配置
    12. // +----------------------------------------------------------------------
    13. return [
    14. // 指令定义
    15. 'commands' => [
    16. 'hello' => 'app\command\Subscribe',
    17. ],
    18. ];
    1. /**
    2. * 自定义指令 - tp+redis实现发布订阅和消息推送
    3. */
    4. namespace app\command;
    5. use think\console\Command;
    6. use think\console\Input;
    7. use think\console\Output;
    8. use think\cache\driver\Redis;
    9. class Subscribe extends Command
    10. {
    11. protected function configure()
    12. {
    13. $this->setName('subscribe')->setDescription('接收订阅消息');
    14. }
    15. protected function execute(Input $input, Output $output)
    16. {
    17. $redis = new Redis();
    18. $redis->connect('127.0.0.1', 6379);
    19. //订阅这个频道,获取频道消息
    20. /*$res = $redis->subscribe(['sixStar:index'],function($instance,$channel,$message){//实例,频道,消息
    21. //$res = $redis->subscribe(['sixStar:index',''],function($instance,$channel,$message){//实例,频道,消息
    22. var_dump($message);
    23. //业务逻辑:发送短信,推送给用户等等
    24. });
    25. */
    26. //匹配适应规则的所有的频道消息
    27. $res = $redis->psubscribe(['sixStar:*'],function($instance,$rule,$channel,$message){//实例,规则,频道,消息
    28. var_dump($message);
    29. //业务逻辑:发送短信,推送给用户等等
    30. });
    31. $output->writeln( date('Y-m-d H:i:s'));//输出内容
    32. }
    33. }

    第二步  执行

    php think

     会有一条 subscribe 指令的命令

     php think subscribe

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

  • 相关阅读:
    面试:HTTP 的长连接和短连接
    JWFD开源工作流-矩阵引擎设计-遍历排序算法运行测试
    Spring Boot之Spring MVC的工作原理 以及使用eclipse开发Spring MVC的Web应用实战(附源码)
    select/poll/epoll 学习
    SQL实现模糊查询的四种方法总结
    BERT为何无法彻底干掉BM25??
    开源消息引擎系统 Kafka 3新特性,一文带你了解
    网络安全之Windows提权(上篇)(高级进阶)
    如何做gif动图?手把手教你在线制作动图
    基于SpringBoot+Vue的二手物品交易平台
  • 原文地址:https://blog.csdn.net/qq_20869933/article/details/132834338