php中怎么利用redis实现消息发布订阅

发布时间:2021-06-24 17:44:37 作者:Leah
来源:亿速云 阅读:241

本篇文章给大家分享的是有关php中怎么利用redis实现消息发布订阅,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

基础介绍

Pub/Sub功能(means Publish, Subscribe)即发布及订阅功能

当使用银行卡消费的时候,银行往往会通过微信、短信或邮件通知用户这笔交易的信息,这便是一种发布订阅模式,这里的发布是交易信息的发布,订阅则是各个渠道。这在实际工作中十分常用,Redis 支持这样的一个模式。

发布订阅模式首先需要消息源,也就是要有消息发布出来,比如例子中的银行通知。首先是银行的记账系统,收到了交易的命令,成功记账后,它就会把消息发送出来,这个时候,订阅者就可以收到这个消息进行处理了,观察者模式就是这个模式的典型应用了。

终端实现

订阅,频道为'chat'

php中怎么利用redis实现消息发布订阅


发布消息

php中怎么利用redis实现消息发布订阅

代码实现

subscribe.php

<?phpini_set('default_socket_timeout', -1);  //php配置设置不超时$redis = new Redis();$redis->connect("127.0.0.1",6379);//$redis->setOption(Redis::OPT_READ_TIMEOUT, -1);  //redis方式设置不超时,推荐$redis->subscribe(['chan'],'callback');     //callback为回调函数名称//$redis->subscribe(['chan'],array(new TestCall(),'callback') ); //如果回调函数是类中的方法名,这样写// 回调函数,这里写处理逻辑function callback($instance, $channelName, $message){ echo $channelName, "==>", $message, PHP_EOL; 
         //$instance,即为上面创建的redis实例对象,在回调函数中,默认的这个参数就是,因此不需专门传参。 这里除了SUBSCRIBE、PSUBSCRIBE、UNSUBSCRIBE、PUNSUBSCRIBE这4条命令之外其它命令都不能使用         //如果要使用redis中的其他命令,这样实现         $newredis = new Redis();$newredis->connect("127.0.0.1", 6379);echo $newredis->get('test') . PHP_EOL;$newredis->close();
          //可以根据$channelName, $message,处理不同的业务逻辑          switch($chan) {   case 'chan-1':  ...  break; 
               case 'chan-2':  ...   break;   }   
           switch($message) {   case 'msg1':  ...  break; 
               case 'msg2':  ...   break;   }}

publish.php

<?php

$redis = new Redis();
$redis->connect("127.0.0.1",6379);

$redis->publish('chan','this is a message');

代码介绍

  1. subscribe.php中设置不超时

方法1:ini_set('default_socket_timeout', -1);方法2: $redis->setOption(Redis::OPT_READ_TIMEOUT, -1);

如果不设置不超时,60s后会报一个错误

PHP Fatal error: Uncaught RedisException: read error on connection to 127.0.0.1:6379 in subscribe.php:6

方式一的实现,是通过临时修改ini的配置值,default_socket_timeout默认为60s,default_socket_timeout是socket流的超时参数,即socket流从建立到传输再到关闭整个过程必须要在这个参数设置的时间以内完成,如果不能完成,那么PHP将自动结束这个socket并返回一个警告。

php中怎么利用redis实现消息发布订阅

方式二是通过修改redis的配置项,因此仅对redis连接生效,相对于方式1,不会产生意外的对其他方法的影响。

批量订阅

redis的psubscribe支持通过模式匹配的方式实现批量订阅,订阅方式

回调函数写函数名或者redis->psubscribe(['my*'],array(new TestCall(),'psubscribe')); //回调函数为类中的方法,类名写你自己定义的类

subscribe.php

<?php//ini_set('default_socket_timeout', -1);  //不超时$redis = new Redis();$redis->connect("127.0.0.1",6379);$redis->setOption(Redis::OPT_READ_TIMEOUT, -1);//匹配方式1:发布可用$redis->publish('mymest','this is a message');//$redis->psubscribe(['my*'],'psubscribe');    
//匹配方式2:发布可用$redis->publish('mydest','this is a message');//$redis->psubscribe(['my?est'],'psubscribe');//匹配方式3:发布可用$redis->publish('myaest','this is a message');或$redis->publish('myeest','this is a message');$redis->psubscribe(['my[ae]est'],'psubscribe');function psubscribe($redis, $pattern, $chan, $msg) {  echo "Pattern: $pattern\n";  echo "Channel: $chan\n";  echo "Payload: $msg\n";}

模式匹配规则

支持以下几种,以hello举例:

h?llo subscribes to hello, hallo and hxllo
h*llo subscribes to hllo and heeeello
h[ae]llo subscribes to hello and hallo, but not hillo
特殊字符用\转义

pubsub方法介绍

public function pubsub( argument )

pubsub获取pub/sub系统的信息,$keyword可用为"channels", "numsub", 或者"numpat",三种,传入不同的keyword返回的数据不同

* $redis->pubsub('channels'); // All channels 获取所有的频道,返回数组     * $redis->pubsub('channels', '*pattern*'); // Just channels matching your pattern,返回符合匹配模式的频道     * $redis->pubsub('numsub', array('chan1', 'chan2')); // Get subscriber counts for 'chan1' and 'chan2'    //返回每个订阅频道的数量,返回数组     * $redis->pubsub('numpat'); // Get the number of pattern subscribers 获取模式匹配方式的订阅

以上就是php中怎么利用redis实现消息发布订阅,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

推荐阅读:
  1. python rabbitmq消息发布订阅
  2. Redis 发布订阅模型

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

redis php

上一篇:mapreduce中怎么实现矩阵相乘

下一篇:Java中怎么实现并行计算器

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》