微信、陌陌 架构方案分析

近两年、手机应用,莫过于微信、陌陌之类最受欢迎;但实现原理,分享文章甚少。
故,提出两种方案,供分享;不对之处,敬请留言学习。

目标

解决大型应用(微信、陌陌级别)中,用户经纬度在不断更新,用户查找频繁的问题。(每分钟1000W级)

==============================================================
方案A

本方案前,请先阅读 http://www.wubiao.info/372

由上文,简单可得;
1、仅需每分钟将用户的经纬度,上报到数据库;
2、然后每次用户查找附近好友时,通过 LIKE ‘wm3yr3%’,即可获取

缺点:稍有一定数据量,对数据库的鸭梨可想而知

==============================================================
方案B

策略

假象把中国分成,若干个一平方公里的单元格,
1、用户位置的变更,理解为一个单元格移动到另外一个单元格(或者不移动)
2、用户查找附近,理解为查找,自己所在方块的的所有人

数据结构

1、用户基本信息 纬度、经度、GeoHash值(经纬度,仅用于后期距离计算)
2、单元格 集合(用户1,用户2,…)

存储工具

1、redis string(key->value) 结构,存储用户基本信息
2、redis set(集合) 结构,以GeoHash值,前6位作为key(约表示一平方千米),存储单元格的用户群

算法流程

1、更新用户信息,先删除用户原所在集合,再更新当前用户信息,最后更新当前用户所在集合
2、查找附近,直接查找,所在单元格集合所有用户ID

具体实现

<?php
 
 /**
  * LBS核心类
  *
  * @author name <simplephp@163.com>
  * @site http://www.wubiao.info
  */
 
 
include_once('geohash.class.php');
 
class lbs
{
 
    //索引长度 6位
    protected $index_len = 6;
 
    protected $redis;
 
    protected $geohash;
 
    publicfunction__construct()
    {
        //redis
        $this->redis = new Redis();
        $this->redis->pconnect('127.0.0.1','6379');
 
        //geohash
        $this->geohash = new Geohash();
    }
 
    /**
    * 更新用户信息
    *
    * @param mixed $latitude 纬度
    * @param mixed $longitude 经度
    */
    publicfunctionupinfo($user_id,$latitude,$longitude)
    {
 
        //原数据处理
 
        //获取原Geohash
        $o_hashdata = $this->redis->hGet($user_id,'geo');
 
        if(!empty($o_hashdata))
        {
            //原索引
            $o_index_key = substr($o_hashdata, 0, $this->index_len);
 
            //删除
            $this->redis->sRem($o_index_key,$user_id);
        }
 
 
        //新数据处理
 
        //纬度
        $this->redis->hSet($user_id,'la',$latitude);
 
        //经度
        $this->redis->hSet($user_id,'lo',$longitude);
 
        //Geohash
        $hashdata = $this->geohash->encode($latitude,$longitude);
        $this->redis->hSet($user_id,'geo',$hashdata);
 
        //索引
        $index_key = substr($hashdata, 0, $this->index_len);
 
        //存入
        $this->redis->sAdd($index_key,$user_id);
 
        returntrue;
    }
 
    /**
    * 获取附近用户
    *
    * @param mixed $latitude 纬度
    * @param mixed $longitude 经度
    */
    publicfunctionserach($latitude,$longitude)
    {
        //Geohash
        $hashdata = $this->geohash->encode($latitude,$longitude);
 
        //索引
        $index_key = substr($hashdata, 0, $this->index_len);
 
        //取得
        $user_id_array = $this->redis->sMembers($index_key);
 
        return$user_id_array;
    }
 
}
 
?>



性能测试

<?php
 
 /**
  * 模拟数据上报
  *
  * @author name <simplephp@163.com>
  * @site http://www.wubiao.info
  */
 
include_once('lbs.class.php');
 
 
$b_time = microtime(true);
 
$n = 0;
 
while(1)
{
    //user_id1~1000000
    $user_id = rand(1,1000000);
 
    //latitude30.59773~30.726786
    $rand_latitude = rand(30597730,30726786);
    $latitude = $rand_latitude/1000000;
 
    //longitude103.983192 ~104.16069
    $rand_longitude = rand(103983192,104160690);
    $longitude = $rand_longitude/1000000;
 
    $lbs = new lbs();
 
    $lbs->upinfo($user_id,$latitude,$longitude);
 
    $n++;
    mylog($n);
 
    $e_time = microtime(true);
 
    if(($e_time-$b_time)>=60)
    {
        exit;
    }
}
 
 
functionmylog($content)
{
    file_put_contents('upinfo.log',$content."\r\n",FILE_APPEND);
}
 
?>
<?php
 
 /**
  * 模拟查找附近
  *
  * @author name <simplephp@163.com>
  * @site http://www.wubiao.info
  */
 
include_once('lbs.class.php');
 
$b_time = microtime(true);
 
$n = 0;
 
while(1)
{
    //latitude30.59773~30.726786
    $rand_latitude = rand(30597730,30726786);
    $latitude = $rand_latitude/1000000;
 
    //longitude103.983192 ~104.16069
    $rand_longitude = rand(103983192,104160690);
    $longitude = $rand_longitude/1000000;
 
    $lbs = new lbs();
 
    $re = $lbs->serach($latitude,$longitude);
 
    $n++;
    mylog($n);
 
    $e_time = microtime(true);
 
    if(($e_time-$b_time)>=60)
    {
        exit;
    }
}
 
 
functionmylog($content)
{
    file_put_contents('search.log',$content."\r\n",FILE_APPEND);
}
 
 
?>


测试环境

虚拟机,内存256M,主频2.93GHz

性能结果

模拟了100W活跃用户行为,不断更新,不断查找附近好友

//60 seconds insert
88544

//60 seconds search
117660

//成都 100W人,数据占用内存
11.97M

总结

从测试结果来看,完全能满足,微信、陌陌之类的性能要求;

尚可改进之处:

1、Geohash,可写成PHP C扩展;或者其他Geohash实现方式
2、Redis,内存消耗较大,可考虑redis集群方案
3、本文仅查出本单元格用户,提高精度,可查出周围八个单元个,求交集
4、求出结果,如需按照由远到近排序;读出Redis经纬度,利用距离公式排序方可。(可参照上一篇文字)


php geohash 拓展查看:http://blog.csdn.net/u014798316/article/details/35242129

PHP 仿陌陌直播,此项目利用 TP+Redis+Nginx+nginx-rtmp-module+ffmpeg+HLS +Swoole 的架构方案。开源RTMP server red5 java java用的较多,性能还是不错的! crtmpserver c++ 支持多种rtmp协议,移动设备以及IPTV相关网络协议 http://www.rtmpd.com/ Erlyvideo erlong 有开源和商业版本 https//github.com/erlyvideo/erlyvideo h aXeVideo haXe 一个实验性的,轻量级的服务器 http://code.google.com/p/haxevideo/ FluorineFx .Net To be defined http://www/fluorinefx.com nginx-rtmp c nginx模块 支持rtmp和HLS https://github.com/arut/nginx-rtmp-module 本人采用的则为第5个 Nginx-rtmp ,接下来讲解 安装过程。 安装 Nginx-rtmp 1、下载nginx-rtmp-module: nginx-rtmp-module的官方github地址:https://github.com/arut/nginx-rtmp-module 使用命令: git clone https://github.com/arut/nginx-rtmp-module.git 将nginx-rtmp-module下载到linux中。 2、安装nginx: nginx的官方网站为:http://nginx.org/en/download.html wget http://nginx.org/download/nginx-1.8.1.tar.gz tar -zxvf nginx-1.8.1.tar.gz cd nginx-1.8.1 ./configure --prefix=/usr/local/nginx --add-module=../nginx-rtmp-module --with-http_ssl_module make && make install 本次默认安装目录为:/root, add-module为下载的nginx-rtmp-module文件路径。 安装时候可能会报错没有安装openssl,需要执行命令: yum -y install openssl openssl-devel 3、修改nginx配置文件: vi /usr/local/nginx/conf/nginx.conf 加入以下内容: rtmp { server { listen 1935; #监听的端口 chunk_size 4000; application hls { #rtmp推流请求路径 live on; hls on; hls_path /usr/share/nginx/html/hls; hls_fragment 5s; } } } hls_path需要可读可写的权限。 修改http中的server模块: #server { #listen 81; #server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } 当然了,root可以跟据自己的需求来改的。 然后启动nginx: /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 4、开始推流 做好以上的配置后,就可以开始推流了,我们可以使用obs来推流。 在设置->串流 中填写信息:URL为 rtmp://xxx:1935/hls,xxx为你的服务器的IP地址,hls是用来存放流媒体的。 秘钥可以随便填写一个,用来播放的时候识别播放哪个流媒体的,例如填写test等。 填写完毕后,点击开始串流,就说明我们的流媒体服务器搭建成功了。 5、观看直播(拉流) 观看直播就比较简单了,可以简单的使用h5的vedio标签就可以观看了。 可以访问http://xxx:81/hls/mystream.m3u8来观看直播,其中xxx为你的服务器IP地址, 或者使用 Your browser does not support HTML5 video. 同上, xxx写的是你服务器IP地址。 然后使用手机访问这个网站就能够观看直播了。延迟大概在20S左右。 (在iOS的safari浏览器中可以正常观看) 写在最后 为什么延迟 那么高呢?这是因为服务器将视频流切断成一个个小的以.ts结尾的文件。 "截图" 而我们访问的是.m3u8文件,这个文件内容是将一个个ts文件串联起来的,这就达到了一个播放的效果,所以看起来会有很大的延迟。 "截图" 如果降低延迟也不是没有方法,可以设置切片生成的大小以及访问的速度,但是这样大大增加了服务器的压力。 当然,我们也可以用rtmp拉流工具(VLC等)来看该直播,延迟大概在2-5S左右,拉流地址与推流地址一致。 后台一键安装 直接访问入口即可 初始admin admin 采用Bootstrap3精确定制的lyui除了拥有100%bootstrap体验外,融合了更多适合国人使用的前端组建。并且一套代码适应多种屏幕大小
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值