• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

分享php+redis实现对200w用户的即时推送服务

php 搞代码 3年前 (2022-01-21) 23次浏览 已收录 0个评论

推荐:《PHP视频教程》

怎么实现对200w用户的即时推送,这个推送可以理解为调用第三方的接口,push,sms之类的东西。

当时先写了一个demo 直接读取DB然后单个推送,结果。。。。可想而知

于是设计一套基于redis+php多进程的方案,用着还不错而去扩展性蛮高的,故分享之。

=============================================

具体的逻辑如下:(无视我的字体)

其实这里还可以优化的,我的设想是如果用户数据再多一些的话,可以在redis里对数据进行分割采取多List,每一个List对应多个php进程这样会更快。

下面是我实现的具体代码:

主管理脚本:应用时启动这个即可。

<?php          //push推送配置  注:使用前请确认log文件为空       2016-04-12
include_once(dirname (__FILE__)."/../../config.inc.php");
//if(exec('ps aux | grep redis_push.php | grep -v grep | wc -l') != 0) goto check;
import('push.class.php');
import('Redis.class.php');

$time  =time();
$data  = array("apikey"=>'xxxx',"secret"=>'xxxx');
$push  = new Channel($data);
$redis = new RedisCache($Credis['host'],$Credis['port']);
if(exec('ps aux | grep redis_push.php | grep -v grep | wc -l') != 0) goto check;//如果有推送任务 直接执行监控代码

/*PUSH配置项*/
$config = array(
  "file"=>"test.txt",
  "Title"=>"sssss",
  "Content"=>"ssssssssssssssss",
  "OpenType"=>"0",    //1是  0否    是否跳转链接
  "Url"=>"",         //链接地址
  "num"=>"500",      //每次推送条数
  "s"=>"1"           //睡眠时间 (单位:秒)
);
$num = 15;            //启动进程数量
$a = $config['OpenType']==1 ? "是" : "否";
$c = json_encode($config);
$info = <<<monkey
   ************ 请确认信息是否有误*10秒后启动push任务! *************
   * 文件名称   : {$config['file']};
   * 推送标题   : {$config['Title']};
   * 推送内容   : {$config['Content']};
   * 是否跳转   : {$config['OpenType']};
   * 进程数量   : $num;(如果为单进程无视此项)
   * 睡眠时间   : {$config['s']};
   * 日志目录   : /log;
   ***************************************************************\n
monkey;
echo $info;
sleep(3);
$n = 1;
while($n<=10){
  ech<a>@本文9来源gao($daima.com搞@代@#码8网^</a><strong>搞代gaodaima码</strong>o (10-$n++),"秒\n";
  sleep(1);
}
echo "------------------------- 任务已启动 -------------------------\n";
if($redis->Scount('push_getchannel_success')){
  echo "队列有未完成任务\n";
}else{
  $res = exec("php redis_getchannel.php {$config['file']}");//写入redis脚本
  echo $res;
}
smtp_mail('[email protected]','推送任务已开启','请实时监测,5秒后您的手机将接收到测试推送!');//推送监控 实现定时全自动推送 
echo "\n---------------- 5秒后 test 将收到测试推送消息 ----------------\n";
sleep(5);
$re = $push->BaiduPush('xxxx','xxxxx',$config['Content'],$config['Title'],'1',$config['OpenType'],$config['Url'],'xxxxx',$push);
sleep(1);
echo "\n---------------- 测试推送已发出!如未收到,请及时终止程序! 10秒后正式推送!!! ----------------\n";
$m = 1;
while($m<=10){
  echo (10-$m++),"秒\n";
  sleep(1);
}
echo "\n---------------- 推送任务已经开始!请耐心等待! ----------------\n";
//下面设置是否多进程
for($i=1;$i<=$num;$i++){
exec("php redis_push.php  '{$c}' > /dev/null 2>&1 &");
}

check:
while(1){
  if(exec('ps aux | grep redis_push.php | grep -v grep | wc -l') == 0){
    echo "push 发送完成 用时",time()-$time,"秒";
    die();
  }
  echo "当前进程数:",exec('ps aux | grep redis_push.php | grep -v grep | wc -l'),"个","\n";
  echo "当前剩余推送数量:".$redis->Scount('push_getchannel_success')."\n";
  sleep(10);
}

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:分享php+redis实现对200w用户的即时推送服务
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址