Java根據(jù)IP地址實(shí)現(xiàn)歸屬地獲取
一、使用Ip2region離線獲取
1、Ip2region簡介
目前支持其他語言的查詢客戶端,項(xiàng)目地址:https://github.com/lionsoul2014/ip2region
Java版本的文檔:https://github.com/lionsoul2014/ip2region/blob/master/binding/java/ReadMe.md
Ip2region是一個離線IP地址定位庫和IP定位數(shù)據(jù)管理框架,10微秒級別的查詢效率,提供了眾多主流編程語言的 xdb 數(shù)據(jù)生成和查詢客戶端實(shí)現(xiàn)。
2、導(dǎo)包
<dependency>
<groupId>org.lionsoul</groupId>
<artifactId>ip2region</artifactId>
<version>2.7.0</version>
</dependency>
3、下載xdb文件
下載地址:https://github.com/lionsoul2014/ip2region/blob/master/data/ip2region.xdb
4、Java獲取IP地址歸屬地
(1)完全基于文件的查詢
基于文件的查詢性能較差,并且Searcher類是線程不安全的。并發(fā)使用,每個線程需要創(chuàng)建一個獨(dú)立的 searcher 對象單獨(dú)使用。
import org.lionsoul.ip2region.xdb.Searcher;
import java.io.*;
import java.util.concurrent.TimeUnit;
public class SearcherTest {
public static void main(String[] args) throws IOException {
// 1、創(chuàng)建 searcher 對象,需要指定 xdb文件的位置
String dbPath = "E:\\javacodes\\ip2region.xdb";
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 = "27.219.61.123";
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);
// {region: 中國|0|山東省|青島市|聯(lián)通, ioCount: 2, took: 408 μs}
} catch (Exception e) {
System.out.printf("failed to search", e);
}
// 3、關(guān)閉資源
searcher.close();
// 備注:并發(fā)使用,每個線程需要創(chuàng)建一個獨(dú)立的 searcher 對象單獨(dú)使用。
}
}
(2)緩存 VectorIndex 索引
我們可以提前從 xdb 文件中加載出來 VectorIndex 數(shù)據(jù),然后全局緩存,每次創(chuàng)建 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) throws IOException {
String dbPath = "E:\\javacodes\\SpringbootDemo\\src\\main\\resources\\ip2region.xdb";
// 1、從 dbPath 中預(yù)先加載 VectorIndex 緩存,并且把這個得到的數(shù)據(jù)作為全局變量,后續(xù)反復(fù)使用。
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 創(chuàng)建帶 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 = "27.219.61.123";
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);
// {region: 中國|0|山東省|青島市|聯(lián)通, ioCount: 2, took: 408 μs}
} catch (Exception e) {
System.out.printf("failed to search", e);
}
// 4、關(guān)閉資源
searcher.close();
// 備注:每個線程需要單獨(dú)創(chuàng)建一個獨(dú)立的 Searcher 對象,但是都共享全局的制度 vIndex 緩存。
}
}
(3)緩存整個 xdb 數(shù)據(jù)
我們也可以預(yù)先加載整個 ip2region.xdb 的數(shù)據(jù)到內(nèi)存,然后基于這個數(shù)據(jù)創(chuàng)建查詢對象來實(shí)現(xiàn)完全基于文件的查詢,類似之前的 memory search。
并發(fā)使用,用整個 xdb 數(shù)據(jù)緩存創(chuàng)建的查詢對象可以安全的用于并發(fā),也就是你可以把這個 searcher 對象做成全局對象去跨線程訪問。
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 = "E:\\javacodes\\SpringbootDemo\\src\\main\\resources\\ip2region.xdb";
// 1、從 dbPath 加載整個 xdb 到內(nèi)存。
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 創(chuàng)建一個完全基于內(nèi)存的查詢對象。
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 = "27.219.61.123";
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);
// {region: 中國|0|山東省|青島市|聯(lián)通, ioCount: 0, took: 1044 μs}
} catch (Exception e) {
System.out.printf("failed to search", e);
}
// 4、關(guān)閉資源 - 該 searcher 對象可以安全用于并發(fā),等整個服務(wù)關(guān)閉的時候再關(guān)閉 searcher
// searcher.close();
// 備注:并發(fā)使用,用整個 xdb 數(shù)據(jù)緩存創(chuàng)建的查詢對象可以安全的用于并發(fā),也就是你可以把這個 searcher 對象做成全局對象去跨線程訪問。
}
}
二、使用第三方網(wǎng)站在線查詢
在這里推薦兩個查詢網(wǎng)站,需要手動調(diào)取API
http://ip-api.com/json/27.219.61.123?lang=zh-CN

http://whois.pconline.com.cn/ipJson.jsp?ip=27.219.61.123&json=true

以上就是Java根據(jù)IP地址實(shí)現(xiàn)歸屬地獲取的詳細(xì)內(nèi)容,更多關(guān)于Java IP地址獲取歸屬地的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java Socket編程實(shí)例(五)- NIO UDP實(shí)踐
這篇文章主要講解Java Socket編程中NIO UDP的實(shí)例,希望能給大家做一個參考。2016-06-06
java實(shí)現(xiàn)調(diào)用http請求的五種常見方式
在實(shí)際開發(fā)過程中,我們經(jīng)常需要調(diào)用對方提供的接口或測試自己寫的接口是否合適,本文主要介紹了java實(shí)現(xiàn)調(diào)用http請求的四種常見方式,感興趣的可以了解一下2024-07-07
SpringBoot使用DevTools實(shí)現(xiàn)后端熱部署的過程詳解
在Spring Boot項(xiàng)目中,Spring Boot官方提供你了Devtools熱部署模塊,通過maven的方式導(dǎo)入就能使用,本文主要SpringBoot通過DevTools實(shí)現(xiàn)熱部署,感興趣的朋友一起看看吧2023-11-11
HotSpot的Java對象模型之Oop-Klass模型詳解
這篇文章主要介紹了HotSpot的Java對象模型之Oop-Klass模型詳解,在JVM層面,不僅Java類是對象,Java 方法也是對象, 字節(jié)碼常量池也是對象,一切皆是對象,JVM使用不同的oop-klass模型來表示各種不同的對象,需要的朋友可以參考下2023-08-08
Spring?AI開發(fā)MCP?Server和MCP?Client的詳細(xì)過程
本文介紹基于SpringAI實(shí)現(xiàn)MCP服務(wù)端(STDIO/SSE協(xié)議)及客戶端,通過@Tool注解集成工具并調(diào)用阿里云Qwen大模型,構(gòu)建智能體與數(shù)據(jù)源的統(tǒng)一連接,適用于本地和分布式場景,提供示例代碼與測試方法,感興趣的朋友一起看看吧2025-06-06
Java通過 Socket 實(shí)現(xiàn) TCP服務(wù)端
這篇文章主要介紹了Java通過 Socket 實(shí)現(xiàn) TCP服務(wù)端的相關(guān)資料,需要的朋友可以參考下2017-05-05
Springboot詳細(xì)講解RocketMQ實(shí)現(xiàn)順序消息的發(fā)送與消費(fèi)流程
RocketMQ作為一款純java、分布式、隊(duì)列模型的開源消息中間件,支持事務(wù)消息、順序消息、批量消息、定時消息、消息回溯等,本篇我們了解如何實(shí)現(xiàn)順序消息的發(fā)送與消費(fèi)2022-06-06
Kotlin中?StateFlow?或?SharedFlow?的區(qū)別解析
Kotlin協(xié)程中的StateFlow和SharedFlow是響應(yīng)式數(shù)據(jù)流,分別用于UI狀態(tài)管理和事件通知,StateFlow有初始值,只保留最新值,適用于UI狀態(tài)管理;SharedFlow沒有初始值,可以配置緩存大小,適用于事件通知,感興趣的朋友一起看看吧2025-03-03
java之static關(guān)鍵字用法實(shí)例解析
這篇文章主要介紹了java之static關(guān)鍵字用法實(shí)例解析,包括了static關(guān)鍵字的原理講解及用法分析,并附帶了實(shí)例說明,需要的朋友可以參考下2014-09-09

