• Ip2region 离线IP地址定位库


    Ip2region 是什么

    ip2region v2.0 - 是一个离线IP地址定位库和IP定位数据管理框架,10微秒级别的查询效率,提供了众多主流编程语言的 xdb 数据生成和查询客户端实现。

    GitHub首页

    xdb包下载

    java使用首页

    支持Python、Java、Go 等多种主流开发语言,详情见首页

    ip2region xdb java 查询客户端实现

    使用方式

    maven 仓库:

    
        org.lionsoul
        ip2region
        2.6.5
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    完全基于文件的查询

    import org.lionsoul.ip2region.xdb.Searcher;
    import java.io.*;
    import java.util.concurrent.TimeUnit;
    
    public class SearcherTest {
        public static void main(String[] args) {
            // 1、创建 searcher 对象
            String dbPath = "ip2region.xdb file path";
            Searcher searcher = null;
            try {
                searcher = Searcher.newWithFileOnly(dbPath);
            } catch (IOException e) {
                System.out.printf("failed to create searcher with `%s`: %s\n", dbPath, e);
                return;
            }
    
            // 2、查询
            try {
                String ip = "1.2.3.4";
                long sTime = System.nanoTime();
                String region = searcher.search(ip);
                long cost = TimeUnit.NANOSECONDS.toMicros((long) (System.nanoTime() - sTime));
                System.out.printf("{region: %s, ioCount: %d, took: %d μs}\n", region, searcher.getIOCount(), cost);
            } catch (Exception e) {
                System.out.printf("failed to search(%s): %s\n", ip, e);
            }
    
            // 3、关闭资源
            searcher.close();
            
            // 备注:并发使用,每个线程需要创建一个独立的 searcher 对象单独使用。
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    缓存 VectorIndex 索引

    我们可以提前从 xdb 文件中加载出来 VectorIndex 数据,然后全局缓存,每次创建 Searcher 对象的时候使用全局的 VectorIndex 缓存可以减少一次固定的 IO 操作,从而加速查询,减少 IO 压力。

    import org.lionsoul.ip2region.xdb.Searcher;
    import java.io.*;
    import java.util.concurrent.TimeUnit;
    
    public class SearcherTest {
        public static void main(String[] args) {
            String dbPath = "ip2region.xdb file path";
    
            // 1、从 dbPath 中预先加载 VectorIndex 缓存,并且把这个得到的数据作为全局变量,后续反复使用。
            byte[] vIndex;
            try {
                vIndex = Searcher.loadVectorIndexFromFile(dbPath);
            } catch (Exception e) {
                System.out.printf("failed to load vector index from `%s`: %s\n", dbPath, e);
                return;
            }
    
            // 2、使用全局的 vIndex 创建带 VectorIndex 缓存的查询对象。
            Searcher searcher;
            try {
                searcher = Searcher.newWithVectorIndex(dbPath, vIndex);
            } catch (Exception e) {
                System.out.printf("failed to create vectorIndex cached searcher with `%s`: %s\n", dbPath, e);
                return;
            }
    
            // 3、查询
            try {
                String ip = "1.2.3.4";
                long sTime = System.nanoTime();
                String region = searcher.search(ip);
                long cost = TimeUnit.NANOSECONDS.toMicros((long) (System.nanoTime() - sTime));
                System.out.printf("{region: %s, ioCount: %d, took: %d μs}\n", region, searcher.getIOCount(), cost);
            } catch (Exception e) {
                System.out.printf("failed to search(%s): %s\n", ip, e);
            }
            
            // 4、关闭资源
            searcher.close();
    
            // 备注:每个线程需要单独创建一个独立的 Searcher 对象,但是都共享全局的制度 vIndex 缓存。
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    缓存整个 xdb 数据

    我们也可以预先加载整个 ip2region.xdb 的数据到内存,然后基于这个数据创建查询对象来实现完全基于文件的查询,类似之前的 memory search。

    import org.lionsoul.ip2region.xdb.Searcher;
    import java.io.*;
    import java.util.concurrent.TimeUnit;
    
    public class SearcherTest {
        public static void main(String[] args) {
            String dbPath = "ip2region.xdb file path";
    
            // 1、从 dbPath 加载整个 xdb 到内存。
            byte[] cBuff;
            try {
                cBuff = Searcher.loadContentFromFile(dbPath);
            } catch (Exception e) {
                System.out.printf("failed to load content from `%s`: %s\n", dbPath, e);
                return;
            }
    
            // 2、使用上述的 cBuff 创建一个完全基于内存的查询对象。
            Searcher searcher;
            try {
                searcher = Searcher.newWithBuffer(cBuff);
            } catch (Exception e) {
                System.out.printf("failed to create content cached searcher: %s\n", e);
                return;
            }
    
            // 3、查询
            try {
                String ip = "1.2.3.4";
                long sTime = System.nanoTime();
                String region = searcher.search(ip);
                long cost = TimeUnit.NANOSECONDS.toMicros((long) (System.nanoTime() - sTime));
                System.out.printf("{region: %s, ioCount: %d, took: %d μs}\n", region, searcher.getIOCount(), cost);
            } catch (Exception e) {
                System.out.printf("failed to search(%s): %s\n", ip, e);
            }
            
            // 4、关闭资源 - 该 searcher 对象可以安全用于并发,等整个服务关闭的时候再关闭 searcher
            // searcher.close();
    
            // 备注:并发使用,用整个 xdb 数据缓存创建的查询对象可以安全的用于并发,也就是你可以把这个 searcher 对象做成全局对象去跨线程访问。
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    编译测试程序

    通过 maven 来编译测试程序。

    # cd 到 java binding 的根目录
    cd binding/java/
    mvn compile package
    
    • 1
    • 2
    • 3

    然后会在当前目录的 target 目录下得到一个 ip2region-{version}.jar 的打包文件。

    查询测试

    可以通过 java -jar ip2region-{version}.jar search 命令来测试查询:

    ➜  java git:(v2.0_xdb) ✗ java -jar target/ip2region-2.6.0.jar search
    java -jar ip2region-{version}.jar search [command options]
    options:
     --db string              ip2region binary xdb file path
     --cache-policy string    cache policy: file/vectorIndex/content
    
    • 1
    • 2
    • 3
    • 4
    • 5

    例如:使用默认的 data/ip2region.xdb 文件进行查询测试:

    ➜  java git:(v2.0_xdb) ✗ java -jar target/ip2region-2.6.0.jar search --db=../../data/ip2region.xdb
    ip2region xdb searcher test program, cachePolicy: vectorIndex
    type 'quit' to exit
    ip2region>> 1.2.3.4
    {region: 美国|0|华盛顿|0|谷歌, ioCount: 7, took: 82 μs}
    ip2region>>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    输入 ip 即可进行查询测试,也可以分别设置 cache-policy 为 file/vectorIndex/content 来测试三种不同缓存实现的查询效果。

    bench 测试

    可以通过 java -jar ip2region-{version}.jar bench 命令来进行 bench 测试,一方面确保 xdb 文件没有错误,一方面可以评估查询性能:

    ➜  java git:(v2.0_xdb) ✗ java -jar target/ip2region-2.6.0.jar bench
    java -jar ip2region-{version}.jar bench [command options]
    options:
     --db string              ip2region binary xdb file path
     --src string             source ip text file path
     --cache-policy string    cache policy: file/vectorIndex/content
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    例如:通过默认的 data/ip2region.xdb 和 data/ip.merge.txt 文件进行 bench 测试:

    ➜  java git:(v2.0_xdb) ✗ java -jar target/ip2region-2.6.0.jar bench --db=../../data/ip2region.xdb --src=../../data/ip.merge.txt
    Bench finished, {cachePolicy: vectorIndex, total: 3417955, took: 8s, cost: 2 μs/op}
    
    • 1
    • 2

    可以通过分别设置 cache-policy 为 file/vectorIndex/content 来测试三种不同缓存实现的效果。 @Note: 注意 bench 使用的 src 文件要是生成对应 xdb 文件相同的源文件。

  • 相关阅读:
    JVM成神之路(十二) -- Jvm性能优化指南
    面了个腾讯拿28k跳槽出来的,真正见识到了跳槽天花板
    许愿也是一种发泄
    数据库安装记录——Mysql8.0.23 msi 保姆级安装教程
    [计算机通信网络]关于ip地址、MAC地址、端口地址的抽象理解与实例
    Vue/React在页面展示JSON格式数据可拷贝可展开
    Layui数据表格table排序(后端PHP)
    从零搭建基于SpringCloud Alibaba 鉴权中心服务(详细教程)
    基于FastAPI的文件上传和下载
    缓存使用常见思路及问题
  • 原文地址:https://blog.csdn.net/mbh12333/article/details/126241881