原文地址

微信、陌陌 架构方案分析

近两年、手机应用,莫过于微信、陌陌之类最受欢迎;但实现原理,分享文章甚少。

故,提出两种方案,供分享;不对之处,敬请留言学习。

目标

查找附近的某某某,由近到远返回结果,且结果中有与目标点的距离。

针对查找附近的某某某,提出两个方案,如下:

方案A:

本方案前,请先阅读:基于LBS功能应用的Geohash方案,看过该文章便可简单知道;
1、仅需每分钟将用户的经纬度,上报到数据库
2、然后每次用户查找附近好友时,通过 LIKE 'wm3yr3%',即可获取
缺点:稍有一定数据量,对数据库的鸭梨可想而知

方案B:使用Redis

策略

假象把中国分成,若干个一平方公里的单元格

1)、用户位置的变更,理解为一个单元格移动到另外一个单元格(或者不移动)

2)、用户查找附近,理解为查找,自己所在方块的的所有人

数据结构

1)、用户基本信息 纬度、经度、GeoHash值(经纬度,仅用于后期距离计算)

2)、单元格 集合(用户1,用户2,…)

存储工具

1)、redis string(key->value) 结构,存储用户基本信息

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

算法流程

1)、更新用户信息,先删除用户原所在集合,再更新当前用户信息,最后更新当前用户所在集合

2)、查找附近,直接查找,所在单元格集合所有用户ID

具体实现

  * @site http://www.wubiao.info
  */
include_once('geohash.class.php');
  
class LBS {
    //索引长度 6位
    protected $index_len = 6;
    protected $redis;
    protected $geohash;
  
    public function __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 经度
    */
    public function upinfo($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);
        return true;
    }
    /**
    * 获取附近用户
    * @param mixed $latitude 纬度
    * @param mixed $longitude 经度
    */
    public function serach($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;
    }
}
?>
  

性能测试

1)模拟数据上报

  * @site http://www.wubiao.info
  */
include_once('lbs.class.php');
  
$b_time = microtime(true);
$n = 0;
  
while(1) {
    //user_id 1~1000000
    $user_id = rand(1, 1000000);
 
    //latitude 30.59773~30.726786
    $rand_latitude = rand(30597730, 30726786);
    $latitude = $rand_latitude / 1000000;
  
    //longitude 103.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;
    }
}
  
function mylog($content) {
    file_put_contents('upinfo.log', $content . "\r\n", FILE_APPEND);
}
?>

  

2)模拟附近查找

* @site http://www.wubiao.info
  */
include_once('lbs.class.php');
  
$b_time = microtime(true);
$n = 0;
  
while(1) {
    //latitude 30.59773~30.726786
    $rand_latitude = rand(30597730, 30726786);
    $latitude = $rand_latitude / 1000000;
  
    //longitude 103.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;
    }
}
  
function mylog($content) {
    file_put_contents('search.log', $content . "\r\n", FILE_APPEND);
}
?>
  

测试环境

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

性能结果

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

1
2
3
4
5
6
7
8
//60 seconds insert
88544
 
//60 seconds search
117660
 
//成都 100W人,数据占用内存
11.97M

总结

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

尚可改进之处:

1、Geohash,可写成PHP C扩展;或者其他Geohash实现方式

2、Redis,内存消耗较大,可考虑redis集群方案

3、本文仅查出本单元格用户,提高精度,可查出周围八个单元个,求交集

4、求出结果,如需按照由远到近排序;读出Redis经纬度,利用距离公式排序方可。(可参照上一篇文章)

问题

1)假设我现在设定的hash长度为7 ,那一个个hash值对应一个块,如何得到这个块的坐标区间呢?

例如,成都永丰立交的Geohash值为:wm3yr31d2524;如取7位,则为,wm3yr31;

根据Geohash的算法,那么区间就会是 wm3yr3100000 ~ wm3yr31zzzzz;

根据如上两值,通过“Geohash->经纬度”算出经纬度,可大致确定区间。

2)如果用户上报的位置信息有时效性(比如:15秒内有效)如何处理?

可以在redis存储的时候,设置有效时间

http://www.2cto.com/weixin/201504/393787.html

使用Redis来实现LBS的应用的更多相关文章

  1. SpringBoot集成redis的LBS功能

    下面的代码实现了添加经纬度数据 和 搜索经纬度数据的功能: import java.util.List; import com.longge.goods.dto.BuildingDto; import ...

  2. Redis GEO 特性在 LBS 中的应用总结

    什么是LBS LBS(Location Based Service),基于位置的服务. Redis和GEO Redis 是最热门的 nosql 数据库之一,它的最大特点就是快.所以在 LBS 这种需要 ...

  3. nginx+play framework +mongoDB+redis +mysql+LBS实战总结

    nginx+play framework +mongoDB+redis +mysql+LBS实战总结(一) 使用这个样的组合结构已经很久了,主要是实现web-server,不是做网站,二是纯粹的数据服 ...

  4. 妈妈再也不用担心别人问我是否真正用过redis了

    1. Memcache与Redis的区别 1.1. 存储方式不同 1.2. 数据支持类型 1.3. 使用底层模型不同 2. Redis支持的数据类型 3. Redis的回收策略 4. Redis小命令 ...

  5. Redis Geo: Redis新增位置查询功能

    转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/144.html 移动互联网增进了人与人之间的联系,其中基于位置信息的服务( ...

  6. lbs(查看附近的人),看看社交软件如何实现查看附近的人

    最近在做一款移动端棋牌游戏,为了进一步提高用户体验.拉近玩家的距离,我们决定在游戏中加入好友功能,而对于体验好友功能的玩家来说,要是玩牌的时候可以看看附近都有谁在玩牌,跟他们交流交流玩牌心得什么的无疑 ...

  7. Mysql or Mongodb LBS快速实现方案

    http://www.wubiao.info/470 前两篇文章: 查找附近的xxx 球面距离以及Geohash方案探讨 (http://www.wubiao.info/372) 微信.陌陌 架构方案 ...

  8. 结合MongoDB开发LBS应用

    然后列举一下需求:1.实时性要高,有频繁的更新和读取2.可按距离排序支持分页3.支持多条件筛选(一个经纬度数据还包含其他属性,比如社交系统的性别.年龄) 方案简单介绍:1.sphinx geo索引支持 ...

  9. 基于LBS的地理位置附近的搜索以及由近及远的排序

    Nosql学习之Redis资料(一) http://redis.io/download 目前基于LBS地理位置的搜索已经应用非常广了,的确是个很方便的东西. 我们做程序的就是要考虑如何通过这些功能,来 ...

随机推荐

  1. 转载:C/C++源代码到可执行程序的过程详解

    C/C++源代码到可执行程序的过程详解 编译,编译程序读取源程序(字符流),对之进行词法和语法的分析,将高级语言指令转换为功能等效的汇编代码,再由汇编程序转换为机器语言,并且按照操作系统对可执行文件格 ...

  2. java 时间戳和PHP时间戳 的转换

    java 时间戳和PHP时间戳 的转换 PHPJava  总结一下java 时间戳和PHP时间戳 的转换问题: 由于精度不同,导致长度不一致,直接转换错误. JAVA时间戳长度是13位,如:12948 ...

  3. 固定某一元素在某一位置 jquery

    方法: $(window).scroll(function (){}) 属性: 滚动变化值 $(window).scrollTop();$(window).scrollLeft(); 需要结合的样式: ...

  4. jquery autocomplete插件结合ajax使用

    <%@ page isELIgnored="false"%> <%@ page contentType="text/html; charset=UTF- ...

  5. css文件内引用外部资源文件的相对路径

    1.default.css文件内容(位于css文件夹下): .ClassName .ClassName .ClassName .page-sidebar .sidebar-search .submit ...

  6. php---JSON和JSONP

    JSON和JSONP (含jQuery实例)(share) 来源:http://www.cnblogs.com/dowinning/archive/2012/04/19/json-jsonp-jque ...

  7. struts.xml 配置详解

    struts.xml是我们在开发中利用率最高的文件,也是Struts2中最重要的配置文件. 一下分别介绍一下几个struts.xml中常用到的标签 1.<include> 利用includ ...

  8. eclipse有时候会报错:Cannot change version of project facet Dynamic Web Module to 2.5。这个错误不会影响程序的运行,不过看着总是不舒服。这个问题现在可以解决啦。

    把项目WEB-INF底下的web.xml文件头部的:     <?xml version="1.0" encoding="UTF-8"?> < ...

  9. [LeetCode]题解(python):039-Combination Sum

    题目来源 https://leetcode.com/problems/combination-sum/ Given a set of candidate numbers (C) and a targe ...

  10. Java学习-018-EXCEL 文件写入实例源代码

    众所周知,EXCEL 也是软件测试开发过程中,常用的数据文件导入导出时的类型文件之一,此文主要讲述如何通过 EXCEL 文件中 Sheet 的索引(index)或者 Sheet 名称获取文件中对应 S ...