現(xiàn)象:Ip2Region查詢時報錯java.lang.ArrayIndexOutOfBoundsException
原因:maven resources 拷貝文件是默認會做 filter,會導致數(shù)據(jù)文件發(fā)生變化,導致文件不能被讀
解決過程:
1、本地跑單測是沒有問題的,本地跑應用也沒有問題,但是線上不行報數(shù)組越界,且searcher的contentBuf屬性大小不一樣;
首先懷疑,是路徑問題,沒有加載到(這里其實有誤區(qū),如果沒加載到contentBuf應該是空,但實際是有值的),因為測試環(huán)境和生產環(huán)境都是容器,是不是環(huán)境導致了路徑上會有什么變化;各種查詢加載方式試了一圈都不行;
本地正常的searcher

企業(yè)微信截圖_17285526328025.png
線上不正常的searcher

企業(yè)微信截圖_17285526786871.png
獲取代碼如下
package com.ZOCO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.Pair;
import org.lionsoul.ip2region.xdb.Searcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* @ClassName: Ip2RegionUtil
* @Description:
* @Author: ZOCO
* @date: 2024-09-18 11:26
* @Version: 1.0.0
**/
@Slf4j
@Component
public class Ip2RegionUtil {
// private static final String IP2REGION_DB_PATH = "/Users/zoco/warehouse/code/store/xsyx-store-operation-service/operation-common-util/src/main/resources/ip2region/ip2region.xdb";
private static final String IP2REGION_DB_CLASSPATH = "ip2region/ip2region.xdb";
// private static void test() throws Exception {
// String dbOath = String.valueOf(Object.class.getResource(IP2REGION_DB_PATH));
// byte[] bytes = Searcher.loadContentFromFile(IP2REGION_DB_PATH);
// Searcher searcher = Searcher.newWithBuffer(bytes);
// String search = searcher.search(testIP);
// System.out.println(search);
// }
private static String testIP = "10.253.0.101";
public static void main(String[] args) throws Exception {
// test();
System.out.println(getProvince(testIP));
System.out.println(getCountry(testIP));
System.out.println(getIp2region(testIP));
// System.out.println(getIPAndProvince(null));
}
private static final Logger logger = LoggerFactory.getLogger(Ip2RegionUtil.class);
private final static String localIp = "127.0.0.1";
private final static String SPLIT_REG = "\\|";
private final static String UNKNOWN = "未知";
private static Searcher searcher;
/**
在服務啟動時加載 ip2region.db 到內存中
解決打包jar后找不到 ip2region.db 的問題
*/
static {
try {
// InputStream ris = Ip2RegionUtil.class.getResourceAsStream(IP2REGION_DB_CLASSPATH);
ClassPathResource resource = new ClassPathResource(IP2REGION_DB_CLASSPATH);
if (!resource.exists()) {
logger.error("ip2region加載失敗,找不到文件,path:{}!?。?!", IP2REGION_DB_CLASSPATH);
}
InputStream ris = resource.getInputStream();
byte[] dbBinStr = FileCopyUtils.copyToByteArray(ris);
searcher = Searcher.newWithBuffer(dbBinStr);
//注意:不能使用文件類型,打成jar包后,會找不到文件
logger.info("ip2region緩存成功,path:{}?。。?!", IP2REGION_DB_CLASSPATH);
} catch (IOException e) {
logger.error("Ip2Region解析ip地址失敗, 無法創(chuàng)建搜索器:{}", e);
throw new RuntimeException(e);
}
}
/**
* 獲取用戶真實IP地址,不使用request.getRemoteAddr();的原因是有可能用戶使用了代理軟件方式避免真實IP地址,
* 參考文章: http://developer.51cto.com/art/201111/305181.htm
* 可是,如果通過了多級反向代理的話,X-Forwarded-For的值并不止一個,而是一串IP值,究竟哪個才是真正的用戶端的真實IP呢?
* 答案是取X-Forwarded-For中第一個非unknown的有效IP字符串。
* 如:X-Forwarded-For:192.168.1.110, 192.168.1.120, 192.168.1.130, 192.168.1.100
* 用戶真實IP為: 192.168.1.110
*
* @param request
* @return
*/
public static String getIp(HttpServletRequest request) {
log.info("獲取請求的真實IP,request:{}", request);
String ipAddress;
try {
// 以下兩個獲取在k8s中,將真實的客戶端IP,放到了x-Original-Forwarded-For。而將WAF的回源地址放到了 x-Forwarded-For了。
ipAddress = HttpUtils.getIp(request.getHeader("X-Original-Forwarded-For"));
if (ipAddress == null || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = HttpUtils.getIp(request.getHeader("X-Forwarded-For"));
}
//獲取nginx等代理的ip
if (ipAddress == null || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = HttpUtils.getIp(request.getHeader("x-forwarded-for"));
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("WL-Proxy-Client-IP");
}
if (ipAddress == null || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("HTTP_CLIENT_IP");
}
if (ipAddress == null || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("HTTP_X_FORWARDED_FOR");
}
// 2.如果沒有轉發(fā)的ip,則取當前通信的請求端的ip(兼容k8s集群獲取ip)
if (StringUtils.isEmpty(ipAddress) || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
// 如果是127.0.0.1,則取本地真實ip
if (localIp.equals(ipAddress)) {
// 根據(jù)網卡取本機配置的IP
InetAddress inet;
try {
inet = InetAddress.getLocalHost();
ipAddress = inet.getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
logger.error("ip2region獲取IP地址失敗", e);
}
}
}
// 對于通過多個代理的情況,第一個IP為客戶端真實IP,多個IP按照','分割
if (ipAddress != null && ipAddress.length() > 15) {// = 15
if (ipAddress.indexOf(",") > 0) {
ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
}
}
} catch (Exception e) {
logger.error("ip2region解析請求IP失敗", e);
ipAddress = "";
}
return (StringUtils.isEmpty(ipAddress) || "0:0:0:0:0:0:0:1".equalsIgnoreCase(ipAddress)) ? localIp : ipAddress;
}
/**
* 獲取 IP和省信息
*
* @param request
* @return IP, 省份
*/
public static Pair<String, String> getProvince(HttpServletRequest request) {
return getProvince(getIp(request));
}
/**
* 根據(jù)ip獲取 省信息
*
* @param ipAddress
* @return IP, 省份
*/
public static Pair<String, String> getProvince(String ipAddress) {
if (StringUtils.isEmpty(ipAddress)) {
return Pair.of(ipAddress, UNKNOWN);
}
String province = null;
try {
log.info("Ip2Region獲取歸屬地,IpAddress:{}", ipAddress);
String search = searcher.search(ipAddress);
if (!StringUtils.isEmpty(search)) {
province = search.split(SPLIT_REG)[2];
}
} catch (Exception e) {
logger.error("Ip2Region搜索:{}失敗:{}", ipAddress, e);
}
return Pair.of(ipAddress, (province == null || province.equals("0")) ? UNKNOWN : province);
}
/**
* 根據(jù)ip獲取 國家或區(qū)域
*
* @param ipAddress
* @return IP, 國家
*/
public static Pair<String, String> getCountry(String ipAddress) {
if (StringUtils.isEmpty(ipAddress)) {
return Pair.of(ipAddress, UNKNOWN);
}
String cityInfo = null;
try {
String search = searcher.search(ipAddress);
if (!StringUtils.isEmpty(search)) {
cityInfo = search.split(SPLIT_REG)[0].equals("0") ? search.split(SPLIT_REG)[1] : search.split(SPLIT_REG)[0];
}
} catch (Exception e) {
logger.error("Ip2Region搜索:{}失敗:{}", ipAddress, e);
}
return Pair.of(ipAddress, (cityInfo == null || cityInfo.equals("0")) ? UNKNOWN : cityInfo);
}
/**
* 根據(jù)ip2region解析ip地址
*
* @param ip ip地址
* @return 解析后的ip地址信息
* 美國|0|加利福尼亞|費利蒙|Hurricane-Electric
* 中國|0|江西省|贛州市|移動
* 國家|區(qū)域|?。校\營商
*/
public static String getIp2region(String ip) {
if (searcher == null) {
logger.error("Ip2Region Error:DbSearcher is null");
return null;
}
try {
String ipInfo = searcher.search(ip);
if (!StringUtils.isEmpty(ipInfo)) {
ipInfo = ipInfo.replace(" | 0", "");
ipInfo = ipInfo.replace("0 |", "");
}
return ipInfo;
} catch (Exception e) {
e.printStackTrace();
}
return UNKNOWN;
}
/**
* 獲取IP地址
*
* @return 本地IP地址
*/
public static String getHostIp() {
try {
return InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
}
return localIp;
}
/**
* 獲取主機名
*
* @return 本地主機名
*/
public static String getHostName() {
try {
return InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
}
return "未知";
}
}
繼續(xù)思考嘗試,發(fā)現(xiàn)文件是找到了且存在了的,searcher也是正常初始化了的,說明啟動時ip2region.db是找到了的,那就是不是路徑問題;那沒辦法了一點頭緒沒有了,只能借助神秘力量了,索性有相同踩坑人給我們留下了解決方案(感謝大佬,不然真的要吃粑粑了https://blog.csdn.net/u012190388/article/details/129391855)
這誰能想到啊,maven給我過濾掉了,嘿按照博主的方法加上提交運行,完美解決

企業(yè)微信截圖_1728611770852.png
過濾的配置代碼片段
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>xdb</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>