Skip to content

Commit

Permalink
♻️ refactor: ChromeDriver 실행 방법 변경
Browse files Browse the repository at this point in the history
ChromeDriver를 컨테이너 환경에서 직접 다운받는 방법으로 변경하였습니다.
  • Loading branch information
seoshinehyo committed Feb 10, 2025
1 parent f94a34d commit f7ba671
Showing 1 changed file with 9 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import com.example.mody.global.common.exception.RestApiException;
import com.example.mody.global.common.exception.code.status.CrawlerErrorStatus;
//import io.github.bonigarcia.wdm.WebDriverManager;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.net.URLEncoder;
Expand All @@ -22,6 +22,9 @@
@RequiredArgsConstructor
public class CrawlerService {

@Value("${webdriver.driver.chrome}") // yml에서 크롬 드라이버 경로 주입
private String chromeDriverPath;

public String getRandomImageUrl(String keyword) {

WebDriver driver = getWebDriver();
Expand Down Expand Up @@ -67,32 +70,17 @@ public String getRandomImageUrl(String keyword) {
}
}

private static WebDriver getWebDriver() {
private WebDriver getWebDriver() {
System.setProperty("webdriver.chrome.driver", chromeDriverPath);

ChromeOptions options = new ChromeOptions();
options.addArguments("--headless"); // 백그라운드 실행 (UI 렌더링 생략)
options.addArguments("--disable-gpu"); // GPU 사용 X
options.addArguments("--no-sandbox"); // 샌드박스 모드 비활성화(Docker 환경에서 크롬 드라이버 실행에 필요)
// options.addArguments("--disable-dev-shm-usage"); // /dev/shm 사용 비활성화(Docker 환경에서 크롬 크래시 문제 해결)
options.addArguments("--disable-dev-shm-usage"); // /dev/shm 사용 비활성화(Docker 환경에서 크롬 크래시 문제 해결)
options.addArguments("--ignore-ssl-errors=yes");
options.addArguments("--ignore-certificate-errors"); // SSL 차단 대비
// options.addArguments("--remote-allow-origins=*"); // CORS 대비

WebDriver driver = new ChromeDriver(options);
return driver;
return new ChromeDriver(options);
}

// // WebDriver 재사용으로 속도 개선
// private static final ThreadLocal<WebDriver> threadLocalDriver = ThreadLocal.withInitial(() -> {
//// WebDriverManager.chromedriver().setup();
// ChromeOptions options = new ChromeOptions();
// options.addArguments("--headless"); // 백그라운드 실행 (UI 렌더링 생략)
// options.addArguments("--disable-gpu"); // GPU 사용 X
// options.addArguments("--window-size=1920,1080"); // 브라우저 창 크기 설정
// options.addArguments("--no-sandbox"); // 샌드박스 모드 비활성화(Docker 환경에서 크롬 드라이버 실행에 필요)
// options.addArguments("--disable-dev-shm-usage"); // /dev/shm 사용 비활성화(Docker 환경에서 크롬 크래시 문제 해결)
// options.addArguments("--ignore-ssl-errors=yes");
// options.addArguments("--ignore-certificate-errors"); // SSL 차단 대비
//// options.addArguments("--remote-allow-origins=*"); // CORS 대비
// return new ChromeDriver(options);
// });
}

0 comments on commit f7ba671

Please sign in to comment.