diff --git a/dl_admin/ruoyi-admin/src/main/resources/application-druid.yml b/dl_admin/ruoyi-admin/src/main/resources/application-druid.yml index 01673a8..0d5d331 100644 --- a/dl_admin/ruoyi-admin/src/main/resources/application-druid.yml +++ b/dl_admin/ruoyi-admin/src/main/resources/application-druid.yml @@ -6,7 +6,7 @@ spring: druid: # # 主库数据源-成达服务器-测试库 master: - url: jdbc:mysql://8.220.74.244:3306/dl_test_site?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 + url: jdbc:mysql://127.0.0.1:3306/dl_test_site?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 username: dl_test_site password: Aa123456. # 主库数据源-成达服务器-生产库 diff --git a/dl_admin/ruoyi-admin/src/main/resources/application.yml b/dl_admin/ruoyi-admin/src/main/resources/application.yml index cbd191b..44f11e5 100644 --- a/dl_admin/ruoyi-admin/src/main/resources/application.yml +++ b/dl_admin/ruoyi-admin/src/main/resources/application.yml @@ -16,7 +16,7 @@ ruoyi: # 开发环境配置 server: # 服务器的HTTP端口,默认为8080 - port: 8099 + port: 8098 servlet: # 应用的访问路径 context-path: / diff --git a/dl_admin/ruoyi-common/src/main/java/com/ruoyi/common/utils/GoogleRankUtil.java b/dl_admin/ruoyi-common/src/main/java/com/ruoyi/common/utils/GoogleRankUtil.java index 7f0cc89..0980225 100644 --- a/dl_admin/ruoyi-common/src/main/java/com/ruoyi/common/utils/GoogleRankUtil.java +++ b/dl_admin/ruoyi-common/src/main/java/com/ruoyi/common/utils/GoogleRankUtil.java @@ -58,9 +58,11 @@ public class GoogleRankUtil { private static volatile int BASE_BACKOFF_MS = 10000; private static volatile boolean KEEP_BROWSER_OPEN = false; private static volatile String GOOGLE_REGION = "GH"; // Google地理位置(如:US, CN, ZA等) + private static volatile String CHROMEDRIVER_PATH = null; // ChromeDriver路径(优先级最高) // 线程本地变量 private static final ThreadLocal TL_DRIVER = new ThreadLocal<>(); + private static final ThreadLocal TL_USER_DATA_DIR = new ThreadLocal<>(); // 记录用户数据目录 private static final AtomicLong LAST_NAV_AT = new AtomicLong(0L); // ==================== 配置方法 ==================== @@ -95,8 +97,22 @@ public class GoogleRankUtil { return GOOGLE_REGION; } + public static void setChromedriverPath(String path) { + CHROMEDRIVER_PATH = (path == null || path.trim().isEmpty()) ? null : path.trim(); + if (CHROMEDRIVER_PATH != null) { + System.setProperty("webdriver.chrome.driver", CHROMEDRIVER_PATH); + log.info("手动设置ChromeDriver路径: {}", CHROMEDRIVER_PATH); + } + } + + public static String getChromedriverPath() { + return CHROMEDRIVER_PATH; + } + public static void shutdownDriver() { WebDriver d = TL_DRIVER.get(); + String userDataDir = TL_USER_DATA_DIR.get(); + if (d != null) { try { d.quit(); @@ -104,6 +120,39 @@ public class GoogleRankUtil { } TL_DRIVER.remove(); } + + // 清理临时用户数据目录 + if (userDataDir != null && !userDataDir.isEmpty()) { + try { + java.io.File dir = new java.io.File(userDataDir); + if (dir.exists() && dir.isDirectory()) { + deleteDirectory(dir); + log.debug("已清理临时用户数据目录: {}", userDataDir); + } + } catch (Exception e) { + log.warn("清理临时用户数据目录失败: {}", e.getMessage()); + } + TL_USER_DATA_DIR.remove(); + } + } + + /** + * 递归删除目录 + */ + private static void deleteDirectory(java.io.File directory) { + if (directory.exists()) { + java.io.File[] files = directory.listFiles(); + if (files != null) { + for (java.io.File file : files) { + if (file.isDirectory()) { + deleteDirectory(file); + } else { + file.delete(); + } + } + } + directory.delete(); + } } // ==================== 核心方法 ==================== @@ -523,6 +572,8 @@ public class GoogleRankUtil { d.quit(); } catch (Exception ignore) { } + // 【关键】清理旧的临时目录 + cleanupOldUserDataDir(); TL_DRIVER.remove(); } d = createWebDriver(); @@ -531,21 +582,181 @@ public class GoogleRankUtil { return d; } + /** + * 清理旧的用户数据目录 + */ + private static void cleanupOldUserDataDir() { + String oldDir = TL_USER_DATA_DIR.get(); + if (oldDir != null && !oldDir.isEmpty()) { + try { + // 强制等待一下,确保 Chrome 进程完全退出 + Thread.sleep(1000); + + java.io.File dir = new java.io.File(oldDir); + if (dir.exists() && dir.isDirectory()) { + deleteDirectory(dir); + log.debug("已清理旧的用户数据目录: {}", oldDir); + } + } catch (Exception e) { + log.warn("清理旧的用户数据目录失败: {}", e.getMessage()); + } + TL_USER_DATA_DIR.remove(); + } + } + + /** + * 自动搜索ChromeDriver可执行文件 + * 在常见位置查找:项目目录、系统PATH、常见安装位置 + */ + private static String findChromeDriver() { + String osName = System.getProperty("os.name").toLowerCase(); + boolean isWindows = osName.contains("win"); + boolean isMac = osName.contains("mac"); + + String executableName = isWindows ? "chromedriver.exe" : "chromedriver"; + + // 搜索路径列表(按优先级) + List searchPaths = new ArrayList<>(); + + // 1. 项目根目录及其子目录 + String userDir = System.getProperty("user.dir"); + searchPaths.add(userDir); + searchPaths.add(userDir + "/drivers"); + searchPaths.add(userDir + "/driver"); + searchPaths.add(userDir + "/chromedriver"); + searchPaths.add(userDir + "/../drivers"); + searchPaths.add(userDir + "/../driver"); + + // 2. 用户目录 + String userHome = System.getProperty("user.home"); + searchPaths.add(userHome + "/drivers"); + searchPaths.add(userHome + "/driver"); + + // 3. Windows 常见位置 + if (isWindows) { + searchPaths.add("C:/chromedriver"); + searchPaths.add("C:/chromedriver-win64"); + searchPaths.add("D:/chromedriver"); + searchPaths.add("D:/chromedriver-win64"); + searchPaths.add("C:/Program Files/chromedriver"); + searchPaths.add("C:/Program Files (x86)/chromedriver"); + searchPaths.add(userHome + "/Downloads"); + searchPaths.add(userHome + "/Downloads/chromedriver-win64"); + } + + // 4. Mac 常见位置 + if (isMac) { + searchPaths.add("/usr/local/bin"); + searchPaths.add("/usr/bin"); + searchPaths.add("/opt/homebrew/bin"); + searchPaths.add("/opt/local/bin"); + searchPaths.add(userHome + "/Downloads"); + } + + // 5. Linux 常见位置 + if (!isWindows && !isMac) { + searchPaths.add("/usr/local/bin"); + searchPaths.add("/usr/bin"); + searchPaths.add("/bin"); + searchPaths.add("/opt/chromedriver"); + searchPaths.add("/opt/google/chrome"); + searchPaths.add(userHome + "/.local/bin"); + searchPaths.add(userHome + "/bin"); + searchPaths.add("/snap/bin"); + } + + // 6. 从 PATH 环境变量中查找 + String pathEnv = System.getenv("PATH"); + if (pathEnv != null) { + String[] paths = pathEnv.split(isWindows ? ";" : ":"); + for (String path : paths) { + searchPaths.add(path.trim()); + } + } + + // 开始搜索 + log.debug("开始在 {} 个位置搜索 ChromeDriver...", searchPaths.size()); + for (String searchPath : searchPaths) { + java.io.File file = new java.io.File(searchPath, executableName); + if (file.exists() && file.isFile()) { + if (file.canExecute()) { + log.info("✅ 自动发现ChromeDriver: {}", file.getAbsolutePath()); + return file.getAbsolutePath(); + } else { + log.warn("⚠️ 找到ChromeDriver但无执行权限: {}", file.getAbsolutePath()); + // 尝试添加执行权限 + if (file.setExecutable(true)) { + log.info("✅ 已自动添加执行权限: {}", file.getAbsolutePath()); + return file.getAbsolutePath(); + } + } + } + // 同时尝试没有扩展名的情况(某些系统) + if (isWindows) { + file = new java.io.File(searchPath, "chromedriver"); + if (file.exists() && file.isFile()) { + log.info("✅ 自动发现ChromeDriver: {}", file.getAbsolutePath()); + return file.getAbsolutePath(); + } + } + } + + log.warn("❌ 未能自动找到ChromeDriver,已搜索 {} 个位置", searchPaths.size()); + log.warn("💡 建议:"); + log.warn(" 1. 手动下载 ChromeDriver: https://googlechromelabs.github.io/chrome-for-testing/"); + log.warn(" 2. 放到以下任意位置: /usr/local/bin, /usr/bin, 或项目drivers目录"); + log.warn(" 3. 或使用代码配置: GoogleRankUtil.setChromedriverPath(\"/path/to/chromedriver\")"); + return null; + } + /** * 创建WebDriver实例 */ private static WebDriver createWebDriver() { - try { - WebDriverManager.chromedriver().setup(); - log.info("WebDriverManager 已自动配置 chromedriver"); - } catch (Throwable t) { - log.warn("WebDriverManager 配置失败,尝试使用手动路径: {}", t.getMessage()); - String sysProp = System.getProperty("webdriver.chrome.driver"); - String envProp = System.getenv("CHROMEDRIVER_PATH"); - if (sysProp == null || sysProp.isEmpty()) { - String path = (envProp != null && !envProp.isEmpty()) ? envProp : "/usr/local/bin/chromedriver"; - System.setProperty("webdriver.chrome.driver", path); - log.info("使用ChromeDriver路径: {}", path); + // 优先级1:静态配置的路径 + if (CHROMEDRIVER_PATH != null && !CHROMEDRIVER_PATH.isEmpty()) { + System.setProperty("webdriver.chrome.driver", CHROMEDRIVER_PATH); + log.info("使用手动配置的ChromeDriver路径: {}", CHROMEDRIVER_PATH); + } else { + // 优先级2:尝试使用 WebDriverManager 自动配置 + try { + WebDriverManager.chromedriver().setup(); + log.info("WebDriverManager 已自动配置 chromedriver"); + } catch (Throwable t) { + log.warn("WebDriverManager 配置失败,尝试其他方法: {}", t.getMessage()); + + // 优先级3:环境变量 + String sysProp = System.getProperty("webdriver.chrome.driver"); + String envProp = System.getenv("CHROMEDRIVER_PATH"); + + if (sysProp == null || sysProp.isEmpty()) { + String foundPath = null; + + // 优先级4:环境变量指定的路径 + if (envProp != null && !envProp.isEmpty()) { + foundPath = envProp; + log.info("使用环境变量CHROMEDRIVER_PATH: {}", foundPath); + } else { + // 优先级5:自动搜索常见位置 + foundPath = findChromeDriver(); + } + + // 优先级6:操作系统默认路径(最后的兜底) + if (foundPath == null) { + String osName = System.getProperty("os.name").toLowerCase(); + if (osName.contains("win")) { + foundPath = "D:/chromedriver-win64/chromedriver.exe"; + } else if (osName.contains("mac")) { + foundPath = "/usr/local/bin/chromedriver"; + } else { + foundPath = "/usr/local/bin/chromedriver"; + } + log.warn("使用操作系统默认路径: {} (可能不存在,建议手动配置)", foundPath); + } + + System.setProperty("webdriver.chrome.driver", foundPath); + log.info("最终使用ChromeDriver路径: {}", foundPath); + } } } @@ -578,6 +789,16 @@ public class GoogleRankUtil { options.addArguments("--no-sandbox"); options.addArguments("--disable-dev-shm-usage"); + // 【关键修复】服务器环境:不指定user-data-dir,让Chrome自动管理临时配置 + // 这样每次都是完全独立的临时环境,避免"already in use"冲突! + log.debug("使用Chrome默认临时配置(不指定user-data-dir)"); + + // 【额外修复】添加更多服务器友好的参数 + options.addArguments("--disable-gpu"); // 禁用GPU加速 + options.addArguments("--disable-software-rasterizer"); + options.addArguments("--disable-setuid-sandbox"); + options.addArguments("--remote-debugging-port=0"); // 禁用远程调试,避免端口冲突 + // 是否保留浏览器窗口 options.setExperimentalOption("detach", KEEP_BROWSER_OPEN); @@ -608,9 +829,9 @@ public class GoogleRankUtil { * @author vinjor-M * @date 11:17 2025/10/24 * @param searchText TODO - * @param site TODO + * @param site TODO * @return int - **/ + **/ public static int getGoogleRankMain(String searchText, String site) { int rank = -1; // 配置参数 @@ -661,11 +882,12 @@ public class GoogleRankUtil { GoogleRankUtil.setKeepBrowserOpen(false); // 结束后关闭 // 【新增】设置Google地理位置 - // 常见代码:US=美国, CN=中国, ZA=南非, EG=埃及, NG=尼日利亚,GH=加纳, - GoogleRankUtil.setGoogleRegion("GH"); // 设置为加纳 + // 常见代码:US=美国, CN=中国, ZA=南非, EG=埃及, NG=尼日利亚 + GoogleRankUtil.setGoogleRegion("GH"); // 设置为南非 + // 测试关键词 - List keywords = Arrays.asList("cd truck"); + List keywords = Arrays.asList("cd duck"); String site = "www.cdtrucktralier.com"; System.out.println("========== GoogleRankUtil 测试 =========="); diff --git a/dl_vue/.env.development b/dl_vue/.env.development index 04d843d..c8f65bc 100644 --- a/dl_vue/.env.development +++ b/dl_vue/.env.development @@ -5,7 +5,7 @@ VUE_APP_TITLE = 成事达管理平台 ENV = 'development' # 成事达管理平台/生产环境 -VUE_APP_BASE_API = 'http://127.0.0.1:8099' +VUE_APP_BASE_API = 'http://8.220.74.244:8098' # 路由懒加载 VUE_CLI_BABEL_TRANSPILE_MODULES = true