ip2region 一个准确率 99.9% 的离线 IP 地址定位库

一个准确率 99.9% 的离线 IP 地址定位库,0.0x 毫秒级查询,ip2region.db 数据库只有数 MB,提供了 java,php,c,python,nodejs,golang,c# 等查询绑定和BinaryB树,内存三种查询算法。

数据聚合了一些知名 ip 到地名查询提供商的数据,这些是他们官方的的准确率,经测试着实比经典的纯真 IP 定位准确一些。ip2region 的 数据聚合自以下服务商的开放 API 或者数据。

  • 80%, 淘宝IP地址库, http://ip.taobao.com/
  • ≈10%, GeoIP, https://geoip.com/
  • ≈2%, 纯真IP库, http://www.cz88.net/

备注:如果上述开放API或者数据都不给开放数据时ip2region将停止数据的更新服务。

每条 ip 数据段都固定了格式:

_城市Id|国家|区域|省份|城市|ISP_

只有中国的数据精确到了城市,其他国家有部分数据只能定位到国家,后 前的选项全部是 0,已经包含了全部你能查到的大大小小的国家

生成的数据库文件 ip2region.db 只有几 MB,最小的版本只有 1.5MB,随着数据的详细度增加数据库的大小也慢慢增大,目前还没超过 8MB

内置的三种查询算法

全部的查询客户端单次查询都在 0.x 毫秒级别,内置了三种查询算法

  • memory 算法:整个数据库全部载入内存,单次查询都在0.1x毫秒内,C语言的客户端单次查询在0.00x毫秒级别。
  • binary 算法:基于二分查找,基于ip2region.db文件,不需要载入内存,单次查询在0.x毫秒级别。
  • b-tree 算法:基于btree算法,基于ip2region.db文件,不需要载入内存,单词查询在0.x毫秒级别,比binary算法更快。

ip2region安装

下面,就让我们给项目引入 ip2region,进行 ip 信息转换吧

首先引入 maven 依赖

<dependency>    <groupId>org.lionsoul</groupId>    <artifactId>ip2region</artifactId>    <version>1.7.2</version></dependency>

然后编写一个工具类 IpUtils ,首先需要加载 ip2region.db 文件

static {    dbPath = createFtlFileByFtlArray() + "ip2region.db";    try {        config = new DbConfig();    } catch (DbMakerConfigException e) {        e.printStackTrace();    }    try {        searcher = new DbSearcher(config, dbPath);    } catch (FileNotFoundException e) {        e.printStackTrace();    }}

在加载的时候,需要下载仓库中的 ip2region.db 文件,然后放到 resource 目录下

然后,通过内置的三种算法,分别转换用户 ip 地址

    public static String getCityInfo(String ip) {        if (StringUtils.isEmpty(dbPath)) {            log.error("Error: Invalid ip2region.db file");            return null;        }        if(config == null || searcher == null){            log.error("Error: DbSearcher or DbConfig is null");            return null;        }        //查询算法        //B-tree, B树搜索(更快)        int algorithm = DbSearcher.BTREE_ALGORITHM;        //Binary,使用二分搜索        //DbSearcher.BINARY_ALGORITHM        //Memory,加载内存(最快)        //DbSearcher.MEMORY_ALGORITYM        try {            // 使用静态代码块,减少文件读取操作//            DbConfig config = new DbConfig();//            DbSearcher searcher = new DbSearcher(config, dbPath);            //define the method            Method method = null;            switch (algorithm) {                case DbSearcher.BTREE_ALGORITHM:                    method = searcher.getClass().getMethod("btreeSearch", String.class);                    break;                case DbSearcher.BINARY_ALGORITHM:                    method = searcher.getClass().getMethod("binarySearch", String.class);                    break;                case DbSearcher.MEMORY_ALGORITYM:                    method = searcher.getClass().getMethod("memorySearch", String.class);                    break;                default:            }            DataBlock dataBlock = null;            if (Util.isIpAddress(ip) == false) {                System.out.println("Error: Invalid ip address");            }            dataBlock = (DataBlock) method.invoke(searcher, ip);            String ipInfo = dataBlock.getRegion();            if (!StringUtils.isEmpty(ipInfo)) {                ipInfo = ipInfo.replace("|0", "");                ipInfo = ipInfo.replace("0|", "");            }            return ipInfo;        } catch (Exception e) {            e.printStackTrace();        }        return null;    }

下面,我们编写 main 函数进行测试,发现可以正常的解析出 ip 信息

由于 ip 属地在国内的话,只会展示省份,而国外的话,只会展示国家。所以我们还需要对这个方法进行一下封装,得到获取 IP 属地的信息。

/** * 获取IP属地 * @param ip * @return */public static String getIpPossession(String ip) {    String cityInfo = getCityInfo(ip);    if (!StringUtils.isEmpty(cityInfo)) {        cityInfo = cityInfo.replace("|", " ");        String[] cityList = cityInfo.split(" ");        if (cityList.length > 0) {            // 国内的显示到具体的省            if ("中国".equals(cityList[0])) {                if (cityList.length > 1) {                    return cityList[1];                }            }            // 国外显示到国家            return cityList[0];        }    }    return "未知";}

下面,我们在找一个 国外的 IP 测试一下效果。可以看到已经能够正常的显示 IP 属地信息了~

正在上传…重新上传取消

到这里如果获取用户的 IP 属地已经完成啦,如果想要了解关于更多 ip2region 的功能,欢迎访问其 Github 地址进行学习。

项目地址

https://github.com/lionsoul2014/ip2region

来源:https://mp.weixin.qq.com/s/GcgvqlvtklUrT2mB7fI-rQ

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值