91名师指路-头部
91名师指路

java集成ip2region实现离线IP地址定位

由于某些原因,现在不支持支付宝支付,如需要购买源码请加博主微信进行购买,微信号:13248254750

一:引入pom

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


二:RegionUtils工具类

package com.mszl.controller;

import org.lionsoul.ip2region.xdb.Searcher;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import java.io.IOException;

/**
* 功能:ip2region是一个离线IP地址定位库和IP定位数据管理框架,10微秒级别的查询效率,提供了众多主流编程语言的 xdb 数据生成和查询客户端实现*
* 备注:离线ip地址解析: 下面三个方法选择一个即可
* gitee: https://gitee.com/lionsoul/ip2region
* @Author: zxb
* 网址: https://91mszl.com
* @Date: 2024-04-12 17:54:31
*/
public class RegionUtils {

/**
* 功能:完全基于文件的查询
* 备注:发使用,每个线程需要创建一个独立的searcher对象单独使用
* @Author: zxb
* @Date: 2024-04-12 17:50:12
*/
public static String fileIpAnalysis(String ip) throws IOException {
Resource resource = new ClassPathResource("/aa/ip2region.xdb");
String dbPath = resource.getFile().getAbsolutePath();

Searcher searcher = null;
String region=null;
try{
// 1 创建searcher对象
searcher = Searcher.newWithFileOnly(dbPath);

// 2 查询
region = searcher.search(ip);
} catch (Exception ex) {
ex.printStackTrace();
}

// 3 关闭资源
searcher.close();

return region;
}

/**
* 功能:缓存VectorIndex索引: 我们可以提前从xdb文件中加载出来VectorIndex数据,然后全局缓存,每次创建Searcher对象的时候使用全局的VectorIndex缓存可以减少一次固定的IO操作,从而加速查询,减少IO压力
* 备注: 每个线程需要单独创建一个独立的 Searcher 对象,但是都共享全局的制度 vIndex 缓存。
* @Author: zxb
* @Date: 2024-04-12 17:57:08
*/
public static String cacheVectorIndexIpAnalysis(String ip) throws IOException {
Resource resource = new ClassPathResource("/aa/ip2region.xdb");
String dbPath = resource.getFile().getAbsolutePath();

byte[] vIndex;
Searcher searcher = null;
String region = null;
try{
// 1 从dbPath中预先加载VectorIndex缓存,并且把这个得到的数据作为全局变量,后续反复使用
vIndex = Searcher.loadVectorIndexFromFile(dbPath);

// 2 使用全局的vIndex创建带VectorIndex缓存的查询对象
searcher = Searcher.newWithVectorIndex(dbPath, vIndex);

// 3 查询
region = searcher.search(ip);
} catch(Exception ex) {
ex.printStackTrace();
}

// 4 关闭资源
searcher.close();

return region;
}

/**
* 功能:缓存整个xdb数据: 我们也可以预先加载整个ip2region.xdb的数据到内存,然后基于这个数据创建查询对象来实现完全基于文件的查询,类似之前的 memory search
* 备注:并发使用,用整个xdb数据缓存创建的查询对象可以安全的用于并发,也就是你可以把这个searcher对象做成全局对象去跨线程访问
* @Author: zxb
* @Date: 2024-04-12 18:04:42
*/
public static String cacheAllIndexIpAnalysis(String ip) throws IOException {
Resource resource = new ClassPathResource("/aa/ip2region.xdb");
String dbPath = resource.getFile().getAbsolutePath();

byte[] cBuff;
Searcher searcher = null;
String region = null;
try {
// 1 从dbPath加载整个xdb到内存
cBuff = Searcher.loadContentFromFile(dbPath);

// 2 使用上述的cBuff创建一个完全基于内存的查询对象
searcher = Searcher.newWithBuffer(cBuff);

// 3 查询
region = searcher.search(ip);
} catch(Exception ex) {
ex.printStackTrace();
}

// 4 关闭资源: 该searcher对象可以安全用于并发,等整个服务关闭的时候再关闭searcher
searcher.close();

return region;
}


}


三:到https://gitee.com/lionsoul/ip2region 下载项目,然后将ip2region.xdb拷贝到spring boot的resources目录下


四:测试

package com.mszl.controller;

import java.io.*;

public class Test {

public static void main(String[] args) throws IOException {
String ip="124.78.11.98";
String res=RegionUtils.cacheAllIndexIpAnalysis(ip);
System.out.println(res);
}


}


执行结果:

中国|0|上海|上海市|电信




2024-04-15 09:44:17     阅读(23)

名师出品,必属精品    https://www.91mszl.com

联系博主    
用户登录遮罩层
x

账号登录

91名师指路-底部