现象:Ip2Region查询时报错java.lang.ArrayIndexOutOfBoundsException
原因:maven resources 拷贝文件是默认会做 filter,会导致数据文件发生变化,导致文件不能被读
解决过程:
1、本地跑单测是没有问题的,本地跑应用也没有问题,但是线上不行报数组越界,且searcher的contentBuf属性大小不一样;
首先怀疑,是路径问题,没有加载到(这里其实有误区,如果没加载到contentBuf应该是空,但实际是有值的),因为测试环境和生产环境都是容器,是不是环境导致了路径上会有什么变化;各种查询加载方式试了一圈都不行;
本地正常的searcher
线上不正常的searcher
获取代码如下
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地址失败, 无法创建搜索器:{}", 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.如果没有转发的ip,则取当前通信的请求端的ip(兼容k8s集群获取ip)
if (StringUtils.isEmpty(ipAddress) || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
// 如果是127.0.0.1,则取本地真实ip
if (localIp.equals(ipAddress)) {
// 根据网卡取本机配置的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));
}
/**
* 根据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);
}
/**
* 根据ip获取 国家或区域
*
* @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);
}
/**
* 根据ip2region解析ip地址
*
* @param ip ip地址
* @return 解析后的ip地址信息
* 美国|0|加利福尼亚|费利蒙|Hurricane-Electric
* 中国|0|江西省|赣州市|移动
* 国家|区域|省|市|运营商
*/
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 "未知";
}
}
继续思考尝试,发现文件是找到了且存在了的,searcher也是正常初始化了的,说明启动时ip2region.db是找到了的,那就是不是路径问题;那没办法了一点头绪没有了,只能借助神秘力量了,索性有相同踩坑人给我们留下了解决方案(感谢大佬,不然真的要吃粑粑了https://blog.csdn.net/u012190388/article/details/129391855)
这谁能想到啊,maven给我过滤掉了,嘿按照博主的方法加上提交运行,完美解决
过滤的配置代码片段
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>xdb</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>