Skip to content

Commit 35bc0b5

Browse files
committed
feat: restTemplate 재시도 로직 추가
1 parent 1980af7 commit 35bc0b5

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ dependencies {
6363
// caffeine
6464
implementation 'com.github.ben-manes.caffeine:caffeine'
6565

66+
implementation 'org.springframework.retry:spring-retry:2.0.12'
6667
}
6768

6869
tasks.named('test') {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.oronaminc.join.global.config;
2+
3+
import java.time.Duration;
4+
5+
import org.springframework.boot.web.client.RestTemplateBuilder;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.http.client.ClientHttpRequestInterceptor;
9+
import org.springframework.retry.policy.SimpleRetryPolicy;
10+
import org.springframework.retry.support.RetryTemplate;
11+
import org.springframework.web.client.RestTemplate;
12+
13+
@Configuration
14+
class RestTemplateConfig {
15+
16+
@Bean
17+
public RestTemplate restTemplate() {
18+
return new RestTemplateBuilder()
19+
.connectTimeout(Duration.ofSeconds(5))
20+
.readTimeout(Duration.ofSeconds(5))
21+
.additionalInterceptors(clientHttpRequestInterceptor())
22+
.build();
23+
}
24+
25+
// 3번 재시도
26+
public ClientHttpRequestInterceptor clientHttpRequestInterceptor() {
27+
return (request, body, execution) -> {
28+
RetryTemplate retryTemplate = new RetryTemplate();
29+
retryTemplate.setRetryPolicy(new SimpleRetryPolicy(3));
30+
try {
31+
return retryTemplate.execute(context -> execution.execute(request, body));
32+
} catch (Throwable throwable) {
33+
throw new RuntimeException(throwable);
34+
}
35+
};
36+
}
37+
38+
}

0 commit comments

Comments
 (0)