<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          老板要我做一個(gè) IP 屬地功能,要求準(zhǔn)確率99.9%!

          共 20537字,需瀏覽 42分鐘

           ·

          2022-08-02 01:40

          大家好,我是編程君!

          老板要我做一個(gè) IP 屬地功能!我摸索了 1 天,搞定了。??

          細(xì)心的朋友應(yīng)該會(huì)發(fā)現(xiàn),最近,繼新浪微博之后,頭條、騰訊、抖音、知乎、快手、小紅書等各大平臺(tái)陸陸續(xù)續(xù)都上線了“網(wǎng)絡(luò)用戶IP地址顯示功能”,境外用戶顯示的是國(guó)家,國(guó)內(nèi)的用戶顯示的省份,而且此項(xiàng)顯示無法關(guān)閉,歸屬地強(qiáng)制顯示。

          作為技術(shù)人,那!這個(gè)功能要怎么實(shí)現(xiàn)呢?

          1.HttpServletRequest 獲取 IP

          下面,我就來講講,Java中是如何獲取IP屬地的,主要分為以下幾步:

          • 通過 HttpServletRequest 對(duì)象,獲取用戶的 「IP」 地址
          • 通過 IP 地址,獲取對(duì)應(yīng)的省份、城市

          首先需要寫一個(gè) IP 獲取的工具類,因?yàn)槊恳淮斡脩舻?Request 請(qǐng)求,都會(huì)攜帶上請(qǐng)求的 IP 地址放到請(qǐng)求頭中

          import javax.servlet.http.HttpServletRequest;
          import java.net.InetAddress;
          import java.net.NetworkInterface;
          import java.net.UnknownHostException;
           
          /**
           * 常用獲取客戶端信息的工具
           */

          public class NetworkUtil {
           
              /**
               * 獲取ip地址
               */

              public static String getIpAddress(HttpServletRequest request) {
                  String ip = request.getHeader("x-forwarded-for");
                  if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                      ip = request.getHeader("Proxy-Client-IP");
                  }
                  if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                      ip = request.getHeader("WL-Proxy-Client-IP");
                  }
                  if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                      ip = request.getHeader("HTTP_CLIENT_IP");
                  }
                  if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                      ip = request.getHeader("HTTP_X_FORWARDED_FOR");
                  }
                  if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                      ip = request.getRemoteAddr();
                  }
                  // 本機(jī)訪問
                  if ("localhost".equalsIgnoreCase(ip) || "127.0.0.1".equalsIgnoreCase(ip) || "0:0:0:0:0:0:0:1".equalsIgnoreCase(ip)){
                      // 根據(jù)網(wǎng)卡取本機(jī)配置的IP
                      InetAddress inet;
                      try {
                          inet = InetAddress.getLocalHost();
                          ip = inet.getHostAddress();
                      } catch (UnknownHostException e) {
                          e.printStackTrace();
                      }
                  }
                  // 對(duì)于通過多個(gè)代理的情況,第一個(gè)IP為客戶端真實(shí)IP,多個(gè)IP按照','分割
                  if (null != ip && ip.length() > 15) {
                      if (ip.indexOf(",") > 15) {
                          ip = ip.substring(0, ip.indexOf(","));
                      }
                  }
                  return ip;
              }
           
              /**
               * 獲取mac地址
               */

              public static String getMacAddress() throws Exception {
                  // 取mac地址
                  byte[] macAddressBytes = NetworkInterface.getByInetAddress(InetAddress.getLocalHost()).getHardwareAddress();
                  // 下面代碼是把mac地址拼裝成String
                  StringBuilder sb = new StringBuilder();
                  for (int i = 0; i < macAddressBytes.length; i++) {
                      if (i != 0) {
                          sb.append("-");
                      }
                      // mac[i] & 0xFF 是為了把byte轉(zhuǎn)化為正整數(shù)
                      String s = Integer.toHexString(macAddressBytes[i] & 0xFF);
                      sb.append(s.length() == 1 ? 0 + s : s);
                  }
                  return sb.toString().trim().toUpperCase();
              }
           
          }

          通過此方法,從請(qǐng)求Header中獲取到用戶的IP地址。

          之前我在做的項(xiàng)目中,也有獲取IP地址歸屬地省份、城市的需求,用的是:淘寶IP庫(kù)。

          地址:https://ip.taobao.com/

          taobao的ip庫(kù)下線了

          再見ip.taobao,全網(wǎng)顯示 IP 歸屬地。

          ip歸屬地

          原來的請(qǐng)求源碼如下:

          可以看到日志log文件中,大量的the request over max qps for user問題。

          留下了難過的淚水

          2.Ip2region

          下面,給大家介紹下之前在Github沖浪時(shí)發(fā)現(xiàn)的今天的主角:微信搜索公眾號(hào):Java項(xiàng)目精選,回復(fù):java 領(lǐng)取資料 。

          Ip2region開源項(xiàng)目,github地址:https://github.com/lionsoul2014/ip2region

          ?

          目前最新已更新到了v2.0版本,ip2region v2.0是一個(gè)離線IP地址定位庫(kù)和IP定位數(shù)據(jù)管理框架,10微秒級(jí)別的查詢效率,準(zhǔn)提供了眾多主流編程語言的 xdb 數(shù)據(jù)生成和查詢客戶端實(shí)現(xiàn)。

          ?
          3.99.9%準(zhǔn)確率:
          ?

          數(shù)據(jù)聚合了一些知名ip到地名查詢提供商的數(shù)據(jù),這些是他們官方的的準(zhǔn)確率,經(jīng)測(cè)試著實(shí)比經(jīng)典的純真IP定位準(zhǔn)確一些。
          ip2region的數(shù)據(jù)聚合自以下服務(wù)商的開放API或者數(shù)據(jù)(升級(jí)程序每秒請(qǐng)求次數(shù)2到4次):
          01, >80%, 淘寶IP地址庫(kù), http://ip.taobao.com/%5C02, ≈10%, GeoIP, https://geoip.com/%5C03, ≈2%, 純真IP庫(kù), http://www.cz88.net/%5C

          ?

          備注:如果上述開放API或者數(shù)據(jù)都不給開放數(shù)據(jù)時(shí)ip2region將停止數(shù)據(jù)的更新服務(wù)。

          4.多查詢客戶端的支持

          已經(jīng)集成的客戶端有:java、C#、php、c、python、nodejs、php擴(kuò)展(php5和php7)、golang、rust、lua、lua_c, nginx。

          binding描述開發(fā)狀態(tài)binary查詢耗時(shí)b-tree查詢耗時(shí)memory查詢耗時(shí)
          cANSC c binding已完成0.0x毫秒0.0x毫秒0.00x毫秒
          c#c# binding已完成0.x毫秒0.x毫秒0.1x毫秒
          golanggolang binding已完成0.x毫秒0.x毫秒0.1x毫秒
          javajava binding已完成0.x毫秒0.x毫秒0.1x毫秒
          lualua實(shí)現(xiàn)的binding已完成0.x毫秒0.x毫秒0.x毫秒
          lua_clua的c擴(kuò)展已完成0.0x毫秒0.0x毫秒0.00x毫秒
          nginxnginx的c擴(kuò)展已完成0.0x毫秒0.0x毫秒0.00x毫秒
          nodejsnodejs已完成0.x毫秒0.x毫秒0.1x毫秒
          phpphp實(shí)現(xiàn)的binding已完成0.x毫秒0.1x毫秒0.1x毫秒
          php5_extphp5的c擴(kuò)展已完成0.0x毫秒0.0x毫秒0.00x毫秒
          php7_extphp7的c擴(kuò)展已完成0.0毫秒0.0x毫秒0.00x毫秒
          pythonpython bindng已完成0.x毫秒0.x毫秒0.x毫秒
          rustrust binding已完成0.x毫秒0.x毫秒0.x毫秒


          5.Ip2region V2.0 特性

          「1、標(biāo)準(zhǔn)化的數(shù)據(jù)格式」

          每個(gè) ip 數(shù)據(jù)段的 region 信息都固定了格式:國(guó)家|區(qū)域|省份|城市|ISP,只有中國(guó)的數(shù)據(jù)絕大部分精確到了城市,其他國(guó)家部分?jǐn)?shù)據(jù)只能定位到國(guó)家,后前的選項(xiàng)全部是0。

          「2、數(shù)據(jù)去重和壓縮」

          xdb 格式生成程序會(huì)自動(dòng)去重和壓縮部分?jǐn)?shù)據(jù),默認(rèn)的全部 IP 數(shù)據(jù),生成的 ip2region.xdb 數(shù)據(jù)庫(kù)是 11MiB,隨著數(shù)據(jù)的詳細(xì)度增加數(shù)據(jù)庫(kù)的大小也慢慢增大。

          「3、極速查詢響應(yīng)」

          即使是完全基于 xdb 文件的查詢,單次查詢響應(yīng)時(shí)間在十微秒級(jí)別,可通過如下兩種方式開啟內(nèi)存加速查詢:

          1. vIndex 索引緩存 :使用固定的 512KiB 的內(nèi)存空間緩存 vector index 數(shù)據(jù),減少一次 IO 磁盤操作,保持平均查詢效率穩(wěn)定在10-20微秒之間。
          2. xdb 整個(gè)文件緩存:將整個(gè) xdb 文件全部加載到內(nèi)存,內(nèi)存占用等同于 xdb 文件大小,無磁盤 IO 操作,保持微秒級(jí)別的查詢效率。

          「4、極速查詢響應(yīng)」

          v2.0 格式的 xdb 支持億級(jí)別的 IP 數(shù)據(jù)段行數(shù),region 信息也可以完全自定義,例如:你可以在 region 中追加特定業(yè)務(wù)需求的數(shù)據(jù),例如:GPS信息/國(guó)際統(tǒng)一地域信息編碼/郵編等。也就是你完全可以使用 ip2region 來管理你自己的 IP 定位數(shù)據(jù)。

          6.ip2region xdb java 查詢客戶端實(shí)現(xiàn)

          • 「使用方式」

          引入maven倉(cāng)庫(kù):

          <dependency>
              <groupId>org.lionsoul</groupId>
              <artifactId>ip2region</artifactId>
              <version>2.6.4</version>
          </dependency>
          • 「完全基于文件的查詢」
          import org.lionsoul.ip2region.xdb.Searcher;
          import java.io.*;
          import java.util.concurrent.TimeUnit;

          public class SearcherTest {
              public static void main(String[] args) {
                  // 1、創(chuàng)建 searcher 對(duì)象
                  String dbPath = "ip2region.xdb file path";
                  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 = "1.2.3.4";
                      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);
                  } catch (Exception e) {
                      System.out.printf("failed to search(%s): %s\n", ip, e);
                  }

                  // 3、備注:并發(fā)使用,每個(gè)線程需要?jiǎng)?chuàng)建一個(gè)獨(dú)立的 searcher 對(duì)象單獨(dú)使用。
              }
          }
          • 「緩存VectorIndex索引」

          我們可以提前從 xdb 文件中加載出來 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) {
                  String dbPath = "ip2region.xdb file path";

                  // 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 = "1.2.3.4";
                      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);
                  } catch (Exception e) {
                      System.out.printf("failed to search(%s): %s\n", ip, e);
                  }

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

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

          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 = "ip2region.xdb file path";

                  // 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 = "1.2.3.4";
                      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);
                  } catch (Exception e) {
                      System.out.printf("failed to search(%s): %s\n", ip, e);
                  }

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


          7.IDEA中做個(gè)測(cè)試

          IDEA中做個(gè)測(cè)試

          完全基于文件的查詢

          ip屬地國(guó)內(nèi)的話,會(huì)展示省份,國(guó)外的話,只會(huì)展示國(guó)家。可以通過如下圖這個(gè)方法進(jìn)行進(jìn)一步封裝,得到獲取IP屬地的信息。

          完全基于文件的查詢

          ?

          下面是官網(wǎng)給出的命令運(yùn)行jar方式給出的測(cè)試demo,可以理解下

          ?

          8.編譯測(cè)試程序

          通過 maven 來編譯測(cè)試程序。

          # cd 到 java binding 的根目錄
          cd binding/java/
          mvn compile package

          然后會(huì)在當(dāng)前目錄的 target 目錄下得到一個(gè) ip2region-{version}.jar 的打包文件。

          9.查詢測(cè)試

          可以通過 java -jar ip2region-{version}.jar search 命令來測(cè)試查詢:

          ?  java git:(v2.0_xdb) ? java -jar target/ip2region-2.6.0.jar search
          java -jar ip2region-{version}.jar search [command options]
          options:
           --db string              ip2region binary xdb file path
           --cache-policy string    cache policy: file/vectorIndex/content

          例如:使用默認(rèn)的 data/ip2region.xdb 文件進(jìn)行查詢測(cè)試:

          ?  java git:(v2.0_xdb) ? java -jar target/ip2region-2.6.0.jar search --db=../../data/ip2region.xdb
          ip2region xdb searcher test program, cachePolicy: vectorIndex
          type 'quit' to exit
          ip2region>> 1.2.3.4
          {region: 美國(guó)|0|華盛頓|0|谷歌, ioCount: 7, took: 82 μs}
          ip2region>>

          輸入 ip 即可進(jìn)行查詢測(cè)試,也可以分別設(shè)置 cache-policy 為 file/vectorIndex/content 來測(cè)試三種不同緩存實(shí)現(xiàn)的查詢效果。

          10.bench 測(cè)試

          可以通過 java -jar ip2region-{version}.jar bench 命令來進(jìn)行 bench 測(cè)試,一方面確保 xdb 文件沒有錯(cuò)誤,一方面可以評(píng)估查詢性能:

          ?  java git:(v2.0_xdb) ? java -jar target/ip2region-2.6.0.jar bench
          java -jar ip2region-{version}.jar bench [command options]
          options:
           --db string              ip2region binary xdb file path
           --src string             source ip text file path
           --cache-policy string    cache policy: file/vectorIndex/content

          例如:通過默認(rèn)的 data/ip2region.xdb 和 data/ip.merge.txt 文件進(jìn)行 bench 測(cè)試:

          ?  java git:(v2.0_xdb) ? java -jar target/ip2region-2.6.0.jar bench --db=../../data/ip2region.xdb --src=../../data/ip.merge.txt
          Bench finished, {cachePolicy: vectorIndex, total: 3417955, took: 8s, cost: 2 μs/op}

          可以通過分別設(shè)置 cache-policy 為 file/vectorIndex/content 來測(cè)試三種不同緩存實(shí)現(xiàn)的效果。@Note: 注意 bench 使用的 src 文件要是生成對(duì)應(yīng) xdb 文件相同的源文件。

          ?

          到這里獲取用戶IP屬地已經(jīng)完成啦,這篇文章介紹的v2.0版本,有興趣的小伙伴可以登錄上門的github地址了解下v1.0版本

          ?

          如若覺得有用,歡迎收藏+點(diǎn)贊!

          來源:https://juejin.cn/post/7118954784853327903

          PS:如果覺得我的分享不錯(cuò),歡迎大家隨手點(diǎn)贊、在看。

           關(guān)注公眾號(hào):Java后端編程,回復(fù)下面關(guān)鍵字 


          要Java學(xué)習(xí)完整路線,回復(fù)  路線 

          缺Java入門視頻,回復(fù) 視頻 

          要Java面試經(jīng)驗(yàn),回復(fù)  面試 

          缺Java項(xiàng)目,回復(fù): 項(xiàng)目 

          進(jìn)Java粉絲群: 加群 


          PS:如果覺得我的分享不錯(cuò),歡迎大家隨手點(diǎn)贊、在看。

          (完)




          加我"微信獲取一份 最新Java面試題資料

          請(qǐng)備注:666不然不通過~


          最近好文


          1、堪稱神級(jí)的Spring Boot手冊(cè),從基礎(chǔ)入門到實(shí)戰(zhàn)進(jìn)階

          2、SQL優(yōu)化萬能公式:5 大步驟 + 10 個(gè)案例

          3、Java面試中如何介紹自己的項(xiàng)目經(jīng)驗(yàn)?

          4、一個(gè)基于Spring Boot+Vue+Redis的物聯(lián)網(wǎng)智能家居系統(tǒng)

          5、公司用了 6 年的Spring Boot 部署方案!



          最近面試BAT,整理一份面試資料Java面試BAT通關(guān)手冊(cè),覆蓋了Java核心技術(shù)、JVM、Java并發(fā)、SSM、微服務(wù)、數(shù)據(jù)庫(kù)、數(shù)據(jù)結(jié)構(gòu)等等。
          獲取方式:關(guān)注公眾號(hào)并回復(fù) java 領(lǐng)取,更多內(nèi)容陸續(xù)奉上。
          明天見(??ω??)
          瀏覽 34
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  91精品国产欧美一区二区百度云 | 操操操操操操操逼 | 欧美三级片网址 | 国产三级网站在线观看 | 欧美亚洲福利 |