欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java根據(jù)IP地址實(shí)現(xiàn)歸屬地獲取

 更新時(shí)間:2025年05月04日 09:53:31   作者:禿了也弱了。  
Ip2region是一個(gè)離線IP地址定位庫(kù)和IP定位數(shù)據(jù)管理框架,這篇文章主要為大家詳細(xì)介紹了Java如何使用Ip2region實(shí)現(xiàn)根據(jù)IP地址獲取歸屬地,感興趣的小伙伴可以了解下

一、使用Ip2region離線獲取

1、Ip2region簡(jiǎn)介

目前支持其他語(yǔ)言的查詢客戶端,項(xiàng)目地址:https://github.com/lionsoul2014/ip2region

Java版本的文檔:https://github.com/lionsoul2014/ip2region/blob/master/binding/java/ReadMe.md

Ip2region是一個(gè)離線IP地址定位庫(kù)和IP定位數(shù)據(jù)管理框架,10微秒級(jí)別的查詢效率,提供了眾多主流編程語(yǔ)言的 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ā)使用,每個(gè)線程需要?jiǎng)?chuàng)建一個(gè)獨(dú)立的 searcher 對(duì)象單獨(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 對(duì)象,需要指定 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: 中國(guó)|0|山東省|青島市|聯(lián)通, ioCount: 2, took: 408 μs}
        } catch (Exception e) {
            System.out.printf("failed to search", e);
        }

        // 3、關(guān)閉資源
        searcher.close();
        
        // 備注:并發(fā)使用,每個(gè)線程需要?jiǎng)?chuàng)建一個(gè)獨(dú)立的 searcher 對(duì)象單獨(dú)使用。
    }
}

(2)緩存 VectorIndex 索引

我們可以提前從 xdb 文件中加載出來(lái) VectorIndex 數(shù)據(jù),然后全局緩存,每次創(chuàng)建 Searcher 對(duì)象的時(shí)候使用全局的 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 緩存,并且把這個(gè)得到的數(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 緩存的查詢對(duì)象。
        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: 中國(guó)|0|山東省|青島市|聯(lián)通, ioCount: 2, took: 408 μs}
        } catch (Exception e) {
            System.out.printf("failed to search", e);
        }
        
        // 4、關(guān)閉資源
        searcher.close();

        // 備注:每個(gè)線程需要單獨(dú)創(chuàng)建一個(gè)獨(dú)立的 Searcher 對(duì)象,但是都共享全局的制度 vIndex 緩存。
    }
}

(3)緩存整個(gè) xdb 數(shù)據(jù)

我們也可以預(yù)先加載整個(gè) ip2region.xdb 的數(shù)據(jù)到內(nèi)存,然后基于這個(gè)數(shù)據(jù)創(chuàng)建查詢對(duì)象來(lái)實(shí)現(xiàn)完全基于文件的查詢,類似之前的 memory search。

并發(fā)使用,用整個(gè) xdb 數(shù)據(jù)緩存創(chuàng)建的查詢對(duì)象可以安全的用于并發(fā),也就是你可以把這個(gè) searcher 對(duì)象做成全局對(duì)象去跨線程訪問(wèn)。

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 加載整個(gè) 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)建一個(gè)完全基于內(nèi)存的查詢對(duì)象。
        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: 中國(guó)|0|山東省|青島市|聯(lián)通, ioCount: 0, took: 1044 μs}
        } catch (Exception e) {
            System.out.printf("failed to search", e);
        }
        
        // 4、關(guān)閉資源 - 該 searcher 對(duì)象可以安全用于并發(fā),等整個(gè)服務(wù)關(guān)閉的時(shí)候再關(guān)閉 searcher
        // searcher.close();

        // 備注:并發(fā)使用,用整個(gè) xdb 數(shù)據(jù)緩存創(chuàng)建的查詢對(duì)象可以安全的用于并發(fā),也就是你可以把這個(gè) searcher 對(duì)象做成全局對(duì)象去跨線程訪問(wèn)。
    }
}

二、使用第三方網(wǎng)站在線查詢

在這里推薦兩個(gè)查詢網(wǎng)站,需要手動(dò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地址獲取歸屬地的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java 注解@PostConstruct的原理及使用場(chǎng)景

    Java 注解@PostConstruct的原理及使用場(chǎng)景

    @PostConstruct 是 Java 中用于標(biāo)記初始化方法的注解,本文將詳細(xì)講解 @PostConstruct 的原理、使用場(chǎng)景及最佳實(shí)踐,感興趣的朋友一起看看吧
    2025-04-04
  • 使用Java操作Parquet文件的基本步驟

    使用Java操作Parquet文件的基本步驟

    Parquet 是一個(gè)強(qiáng)大的列式存儲(chǔ)格式,適用于大數(shù)據(jù)場(chǎng)景,能夠高效地進(jìn)行數(shù)據(jù)壓縮、查詢和存儲(chǔ),在 Java 中使用 Apache Spark 讀取和寫(xiě)入 Parquet 文件是一項(xiàng)常見(jiàn)的任務(wù),本文給大家介紹了在 Java 中使用 Spark 來(lái)讀取和寫(xiě)入 Parquet 文件的基本步驟,需要的朋友可以參考下
    2025-03-03
  • 關(guān)于ReentrantLock的實(shí)現(xiàn)原理解讀

    關(guān)于ReentrantLock的實(shí)現(xiàn)原理解讀

    這篇文章主要介紹了關(guān)于ReentrantLock的實(shí)現(xiàn)原理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • lombok插件無(wú)法使用的原因及解決方案

    lombok插件無(wú)法使用的原因及解決方案

    這篇文章主要介紹了lombok插件無(wú)法使用的原因及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • java實(shí)現(xiàn)模擬RPG格斗

    java實(shí)現(xiàn)模擬RPG格斗

    這篇文章主要介紹了java實(shí)現(xiàn)模擬RPG格斗,每個(gè)英雄具有以下幾個(gè)屬性:生命值(為0時(shí)英雄倒下)、攻擊力(每次攻擊時(shí)扣除對(duì)方的生命值點(diǎn)數(shù))、攻擊間隔(每次攻擊過(guò)后都要等待間隔時(shí)間才能進(jìn)行下次攻擊首次攻擊之前也要先等待間隔時(shí)間)簡(jiǎn)單設(shè)置了下吧,小伙伴可以參考下
    2015-03-03
  • JavaSE中Lambda表達(dá)式的使用與變量捕獲

    JavaSE中Lambda表達(dá)式的使用與變量捕獲

    這篇文章主要介紹了JavaSE中Lambda表達(dá)式的使用與變量捕獲,Lambda表達(dá)式允許你通過(guò)表達(dá)式來(lái)代替功能接口, 就和方法一樣,它提供了一個(gè)正常的參數(shù)列表和一個(gè)使用這些參數(shù)的主體,下面我們來(lái)詳細(xì)看看,需要的朋友可以參考下
    2023-10-10
  • 實(shí)戰(zhàn)分布式醫(yī)療掛號(hào)系統(tǒng)之設(shè)置微服務(wù)接口開(kāi)發(fā)模塊

    實(shí)戰(zhàn)分布式醫(yī)療掛號(hào)系統(tǒng)之設(shè)置微服務(wù)接口開(kāi)發(fā)模塊

    這篇文章主要為大家介紹了實(shí)戰(zhàn)分布式醫(yī)療掛號(hào)系統(tǒng)之接口開(kāi)發(fā)醫(yī)院設(shè)置微服務(wù)模塊,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-04-04
  • JAVA中SpringBoot啟動(dòng)流程分析

    JAVA中SpringBoot啟動(dòng)流程分析

    這篇文章主要介紹了JAVA中SpringBoot啟動(dòng)流程分析的相關(guān)資料,需要的朋友可以參考下
    2023-01-01
  • SpringBoot全局處理統(tǒng)一返回類型方式

    SpringBoot全局處理統(tǒng)一返回類型方式

    這篇文章主要介紹了SpringBoot全局處理統(tǒng)一返回類型方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • IDEA 中創(chuàng)建SpringBoot 父子模塊的實(shí)現(xiàn)

    IDEA 中創(chuàng)建SpringBoot 父子模塊的實(shí)現(xiàn)

    這篇文章主要介紹了IDEA 中創(chuàng)建SpringBoot 父子模塊的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04

最新評(píng)論