Java根據IP地址實現歸屬地獲取
一、使用Ip2region離線獲取
1、Ip2region簡介
目前支持其他語言的查詢客戶端,項目地址:https://github.com/lionsoul2014/ip2region
Java版本的文檔:https://github.com/lionsoul2014/ip2region/blob/master/binding/java/ReadMe.md
Ip2region是一個離線IP地址定位庫和IP定位數據管理框架,10微秒級別的查詢效率,提供了眾多主流編程語言的 xdb 數據生成和查詢客戶端實現。
2、導包
<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)建一個獨立的 searcher 對象單獨使用。
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|山東省|青島市|聯通, ioCount: 2, took: 408 μs}
} catch (Exception e) {
System.out.printf("failed to search", e);
}
// 3、關閉資源
searcher.close();
// 備注:并發(fā)使用,每個線程需要創(chuàng)建一個獨立的 searcher 對象單獨使用。
}
}
(2)緩存 VectorIndex 索引
我們可以提前從 xdb 文件中加載出來 VectorIndex 數據,然后全局緩存,每次創(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 中預先加載 VectorIndex 緩存,并且把這個得到的數據作為全局變量,后續(xù)反復使用。
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|山東省|青島市|聯通, ioCount: 2, took: 408 μs}
} catch (Exception e) {
System.out.printf("failed to search", e);
}
// 4、關閉資源
searcher.close();
// 備注:每個線程需要單獨創(chuàng)建一個獨立的 Searcher 對象,但是都共享全局的制度 vIndex 緩存。
}
}
(3)緩存整個 xdb 數據
我們也可以預先加載整個 ip2region.xdb 的數據到內存,然后基于這個數據創(chuàng)建查詢對象來實現完全基于文件的查詢,類似之前的 memory search。
并發(fā)使用,用整個 xdb 數據緩存創(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 到內存。
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)建一個完全基于內存的查詢對象。
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|山東省|青島市|聯通, ioCount: 0, took: 1044 μs}
} catch (Exception e) {
System.out.printf("failed to search", e);
}
// 4、關閉資源 - 該 searcher 對象可以安全用于并發(fā),等整個服務關閉的時候再關閉 searcher
// searcher.close();
// 備注:并發(fā)使用,用整個 xdb 數據緩存創(chuàng)建的查詢對象可以安全的用于并發(fā),也就是你可以把這個 searcher 對象做成全局對象去跨線程訪問。
}
}
二、使用第三方網站在線查詢
在這里推薦兩個查詢網站,需要手動調取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根據IP地址實現歸屬地獲取的詳細內容,更多關于Java IP地址獲取歸屬地的資料請關注腳本之家其它相關文章!
相關文章
實戰(zhàn)分布式醫(yī)療掛號系統之設置微服務接口開發(fā)模塊
這篇文章主要為大家介紹了實戰(zhàn)分布式醫(yī)療掛號系統之接口開發(fā)醫(yī)院設置微服務模塊,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-04-04
IDEA 中創(chuàng)建SpringBoot 父子模塊的實現
這篇文章主要介紹了IDEA 中創(chuàng)建SpringBoot 父子模塊的實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04

