Pārlūkot izejas kodu

营销系统-IP修改

wangcl 1 nedēļu atpakaļ
vecāks
revīzija
9d9fb86134

+ 1 - 1
pig-common/pig-common-bom/pom.xml

@@ -38,7 +38,7 @@
         <common.io.version>2.18.0</common.io.version>
         <spring.checkstyle.plugin>0.0.43</spring.checkstyle.plugin>
         <flatten-maven-plugin.version>1.6.0</flatten-maven-plugin.version>
-		<ip2region.version>2.6.6</ip2region.version>
+		<ip2region.version>2.7.0</ip2region.version>
     </properties>
 
     <!-- 定义全局jar版本,模块使用需要再次引入但不用写版本号-->

+ 19 - 51
pig-common/pig-common-core/src/main/java/com/pig4cloud/pig/common/core/util/ip/IPUtils.java

@@ -14,9 +14,6 @@ import java.net.InetAddress;
 import java.net.URL;
 import java.net.UnknownHostException;
 import java.nio.charset.StandardCharsets;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
@@ -44,59 +41,30 @@ public class IPUtils {
 	private static Searcher searcher;
 
 	static {
-		initIpSearcher();
+		try {
+			initIpSearcher();
+		} catch (IOException e) {
+			throw new RuntimeException(e);
+		}
 	}
 
 	/**
 	 * 初始化IP地址索引
 	 */
-	private static void initIpSearcher() {
-		try {
-			// 仅使用相对路径查找(工作目录、jar同目录、常见容器目录)
-			java.util.List<Path> candidates = new java.util.ArrayList<>();
-			try {
-				Path cwd = Paths.get("").toAbsolutePath();
-				candidates.add(cwd.resolve("ip/ip2region.xdb"));
-				candidates.add(cwd.resolve("ip2region.xdb"));
-				// 以运行jar所在目录为基准
-				URL loc = IPUtils.class.getProtectionDomain().getCodeSource().getLocation();
-				if (loc != null) {
-					Path base;
-					try {
-						Path jarPath = Paths.get(loc.toURI());
-						base = Files.isRegularFile(jarPath) ? jarPath.getParent() : jarPath;
-					} catch (Exception e) {
-						base = null;
-					}
-					if (base != null) {
-						candidates.add(base.resolve("ip/ip2region.xdb"));
-						candidates.add(base.resolve("../ip/ip2region.xdb"));
-						candidates.add(base.resolve("ip2region.xdb"));
-					}
-				}
-				// 常见容器工作目录
-				candidates.add(Paths.get("/pig-marketing/ip/ip2region.xdb"));
-			} catch (Exception ignore) {}
-
-			for (Path cand : candidates) {
-				try {
-					if (cand != null && Files.exists(cand)) {
-						Long sz = null; try { sz = Files.size(cand); } catch (Exception ignore) {}
-						log.info("ip2region尝试相对路径: path={}, size={}", cand, sz);
-						searcher = Searcher.newWithFileOnly(cand.toString());
-						log.info("ip2region初始化成功 (relative path: {})", cand);
-						return;
-					}
-				} catch (Exception tryNext) {
-					log.warn("ip2region相对路径初始化失败: {}", cand, tryNext);
-				}
-			}
-
-			log.error("ip2region初始化失败:未在相对路径找到ip2region.xdb,请将文件放在 ./ip/ip2region.xdb 或与jar同目录的ip目录中");
-			searcher = null;
-		} catch (Exception e) {
-			log.error("ip2region初始化失败", e);
-			searcher = null;
+	private static void initIpSearcher() throws IOException {
+		// use jar resource file  file
+
+		try{
+			 ClassLoader classLoader = IPUtils.class.getClassLoader();
+			 if (classLoader.getResourceAsStream("ip/ip2region.xdb") == null) {
+				 log.error("初始化IP地址索引失败");
+			 }
+			 log.info("初始化IP地址索引成功 (classpath)");
+			URL url = classLoader.getResource("ip/ip2region.xdb");
+			searcher = Searcher.newWithFileOnly(url.getPath());
+		} catch (IOException e) {
+			         log.error("初始化IP地址索引失败", e);
+					 throw  e;
 		}
 	}