Projects/HubEleven

[동시성 처리] Trouble Shooting : 멀티스레드 재고 감소 통합 테스트 코드 Eureka Server Connection refused

annovation 2026. 2. 16. 23:32

진행 환경

💡진행 환경

  • Java 17
  • Spring Boot 3.5.7
  • MySQL 8.0.44
  • Gradle

문제 상황

💡 문제 상황

  • 재고 감소 시 동시에 100개의 요청을 정확히 차감하는지 테스트 하는 코드에서 오류 발생

💡 문제 증상

org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8761/eureka/apps/": Connect to http://localhost:8761 failed: Connection refused
  • Eureka 서버(8761 포트)에 연결하려고 했는데 해당 포트에 서버가 떠 있지 않아서 거절당한 것

원인 분석

💡 원인 1 : 테스트 실행 중 Config Client가 활성화되어 Eureka를 통해 Config Server를 찾으려 함

  • @SpringBootTest 가 전체 ApplicationContext를 다 올리고 있다.

@SpringBootTest 동작 흐름

  • 테스트 클래스에서 @SpringBootTest 발견
  • Spring Test Framework가 테스트 컨텍스트 생성
  • SpringApplication.run 을 통해 ApplicationContext 생성
  • Spring Boot auto-configuration + Test 환경 설정 적용
  • 테스트 실행

✅ ApplicationContext 개념 및 역할

 

1) ApplicationContext 란? (공식 문서)

  • Spring Container
  • Bean 정의, 의존성 주입, 라이프사이클 관리 제공
  • 환경, 프로퍼티, 메시지, 이벤트 처리 포함
It extends the BeanFactory interface, so it provides all the functionality of BeanFactory and more.

 

2) Context 란?

  • Context = 환경 + 설정 + Bean들의 집합
  • 현재 애플리케이션이 동작하는 설정 환경 전체
  • 즉, 현재 실행 중인 Spring 컨테이너의 전체 상태
A Spring ApplicationContext is essentially a container of beans.

해결 방안 검토

💡 해결 방안 1 : 기존 Config Server 트러블슈팅 해결 방안을 적용

https://annovation.tistory.com/512

 

[동시성 처리] Trouble Shooting : 테스트 코드 Config Client 오류

문제 상황💡 문제 상황Stock 테스트 코드에서 yml 설정으로 config server 를 false 설정 했음에도 관련 오류로 인해 테스트 실행이 안되는 상황 발생💡 문제 코드 - StockServiceTest.java@ActiveProfiles("test")@Sp

annovation.tistory.com


해결 방법

💡 해결 방법 : 해당 트러블슈팅으로 인해 기존에 작성해둔 application-test.yml 을 적용한다.

@ActiveProfiles("test")
@SpringBootTest
public class StockCurrencyTest {

...(생략)...

}

 

✅ @ActiveProfiles("test") 역할 (공식 문서)

  • 테스트에서 사용할 프로필을 지정
  • 지정된 프로필만 적용해서 context를 로딩
  • 테스트 환경 분리/커스터마이징에 사용

참고 자료

1) Spring Docs : ActiveProfiles

https://docs.spring.io/spring-framework/reference/testing/annotations/integration-spring/annotation-activeprofiles.html

 

@ActiveProfiles :: Spring Framework

@ActiveProfiles is an annotation that can be applied to a test class to declare which bean definition profiles should be active when loading an ApplicationContext for an integration test.

docs.spring.io