了解
- 是什么
ip2region - 准确率99.9%的离线IP地址定位库,0.0x毫秒级查询,ip2region.db数据库只有数MB,提供了java,php,c,python,nodejs,golang,c#等查询绑定和Binary,B树,内存三种查询算法。
源码地址戳这
- 特性
- 99.9%准确率
- 标准化的数据格式
_城市Id|国家|区域|省份|城市|ISP_
- 体积小
- 查询速度快(memory算法、binary算法、b-tree算法)
- 多查询客户端的支持
使用
- pom引入
<dependency> <groupId>org.lionsoul</groupId> <artifactId>ip2region</artifactId> <version>1.7.2</version> </dependency>
- 加入离线IP地址定位库
- 使用
@Slf4j public class IPUtil { private static final DbSearcher dbSearcher; static { InputStream resourceAsStream = IPUtil.class.getResourceAsStream("/ip2region.db"); byte[] bytes = IoUtil.readBytes(resourceAsStream); DbConfig dbConfig = null; try { dbConfig = new DbConfig(); } catch (DbMakerConfigException var3) { var3.printStackTrace(); log.error("init ip2region DbConfig error"); } dbSearcher = new DbSearcher(dbConfig, bytes); } public static String getAddress(String ip) throws IOException { //memory搜索 DataBlock dataBlock = dbSearcher.memorySearch(ip); //binary搜索 DataBlock dataBlock = dbSearcher.binarySearch(ip); //b-tree搜索 DataBlock dataBlock = dbSearcher.btreeSearch(ip); return dataBlock.getRegion(); } }
更多
详细文档【戳这】