반응형
public class MsaErrorDecoder implements ErrorDecoder {
@Override
public Exception decode(String methodKey, Response response) {
..
}
}
Feign 흐름
Feign Proxy
→ RequestTemplate 생성
→ Client.execute() ← 여기서 네트워크 I/O
→ Response 생성
→ 상태코드 분기
├─ 2xx → Decoder
└─ 4xx/5xx → ErrorDecoder
// Client.execute() 실패 시
1. IOException 발생(대부분)
→ RetryableException 으로 래핑 가능
- Feign이 RetryableException 으로 감쌈
- Retryer 있으면 재시도
- 최종적으로 FeignException 전파
2. RuntimeException(url 세팅, null header 등에서 에러 발생)
→ 그대로 전파 (retry 대상 아님)
| 예외 유형 | 의미 | 대응 |
| IOException / RetryableException | 외부 시스템 문제 | retry / fallback |
| FeignException (4xx/5xx) | 상대 시스템 응답 | ErrorDecoder |
| RuntimeException | 설정/버그 | 즉시 실패 |
ErrorDecoder 가 작동하는 조건
1. feign Client가 정상적으로 생성되어 있을 것
- 해당 클라이언트에 ErrorDecoder가 등록되어 있어야 함
- 1. 빈으로 등록(모든 feign에 공통 적용; 전역 적용)
@Bean
public ErrorDecoder errorDecoder() {
return new CustomErrorDecoder();
}
//or
@Component
public class CustomErrorDecoder implements ErrorDecoder {
}
- 2. 특정 클라이언트에 적용 시 @FeignClient 또는 Feign.builder() 사용
@FeignClient(contextId = "item-product", name = "item", url = "localhost:12007", configuration = MsaFeignConfig.class)
- 전역 + 일부 오버라이드 구조
- Feign은 클라이언트별 설정이 전역 설정보다 우선됩니다.
2. HTTP 상태 코드가 에러 범위일 것
200 ~ 299 → Decoder (정상 응답)
400 ~ 599 → ErrorDecoder
- 404, 409, 500, 503 등은 모두 대상
- 3xx는 기본 설정에서는 에러 아님
3. HTTP 응답을 실제로 받을 때
- 서버로부터 HTTP Response 자체는 도착
- status line + headers(+ body)가 존재
호출되지 않는 경우
- connect timeout
- read timeout
- DNS 실패
- connection refused
- 네트워크 단절
위 경우는 전부 I/O 단계 예외라서 decode()까지 오지 않습니다.
즉, Client.execute() 에서 에러가 발생할 경우 decoder/error decoder 타지 않음
1. Connect Timeout
- 서버에 TCP 연결 자체 실패
- ConnectTimeoutException
- SocketTimeoutException
2. Read Timeout
- 연결은 됐지만 응답 바디 대기 중 시간 초과
- SocketTimeoutException
3. DNS / Network 문제
- UnknownHostException
- NoRouteToHostException
- Connection refused
4. TLS / Handshake 실패
- SSL handshake error
728x90
반응형
'개발 > spring' 카테고리의 다른 글
| [jpa] paging 0부터 시작 -> 1부터 시작으로 변경 (4) | 2025.07.17 |
|---|---|
| 트랜젝션 안에 API 호출이 있고 실패나면? 다른 트랜젝션이 있고 실패나면? (0) | 2025.07.07 |
| @ControllerAdvice (0) | 2025.06.11 |
| [page] Serializing PageImpl instances as-is is not supported (0) | 2025.05.21 |
| open feign에서 401일 때 바디가 안 나올 경우 (0) | 2025.03.11 |