Skip to content

Commit

Permalink
[MERGE] api 반복요청 수정 - #362 (#363)
Browse files Browse the repository at this point in the history
  • Loading branch information
rlarlgnszx authored Sep 26, 2024
2 parents ae2ee68 + ae38201 commit 781ca87
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
21 changes: 15 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ plugins {
id 'jacoco'
}

ext {
springCloudVersion = "2023.0.3"
}

group = 'org.sopt'
version = '0.0.1-SNAPSHOT'

Expand All @@ -28,10 +32,10 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-security'

// OpenFeign Client
implementation 'io.github.openfeign:feign-core:12.4'
implementation 'io.github.openfeign:feign-okhttp:12.4'
implementation 'io.github.openfeign:feign-jackson:12.4'
implementation 'io.github.openfeign:feign-slf4j:12.4'
implementation "io.github.openfeign:feign-okhttp"
implementation "io.github.openfeign:feign-jackson"
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'


// swagger
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.6.0'
Expand All @@ -54,7 +58,6 @@ dependencies {
//postgres
runtimeOnly 'org.postgresql:postgresql'


// mapper
implementation 'org.mapstruct:mapstruct:1.5.3.Final'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.3.Final'
Expand Down Expand Up @@ -82,7 +85,7 @@ dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'

// retry
implementation 'org.springframework.retry:spring-retry:1.3.0'
implementation 'org.springframework.retry:spring-retry:2.0.6'

// test
testImplementation 'org.springframework.boot:spring-boot-starter-test'
Expand All @@ -104,4 +107,10 @@ tasks.named('test') {

test {
exclude '**/*RepositoryTest.*'
}

dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:$springCloudVersion"
}
}
10 changes: 3 additions & 7 deletions src/main/java/org/sopt/app/common/config/ClientConfig.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package org.sopt.app.common.config;



import feign.Feign;
import feign.Logger;
import feign.Retryer;
import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.jackson.JacksonDecoder;
Expand All @@ -13,18 +11,20 @@
import feign.slf4j.Slf4jLogger;
import org.sopt.app.application.playground.PlaygroundClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.retry.annotation.EnableRetry;

@Configuration
@EnableFeignClients
public class ClientConfig {
@Value("${makers.playground.server}")
private String playgroundEndPoint;

@Bean
public PlaygroundClient playgroundClient() {
return Feign.builder()
.retryer(retryer())
.client(okHttpClient())
.encoder(encoder())
.decoder(decoder())
Expand All @@ -33,10 +33,6 @@ public PlaygroundClient playgroundClient() {
.target(PlaygroundClient.class, playgroundEndPoint);
}

@Bean
public Retryer retryer() {
return Retryer.NEVER_RETRY;
}
@Bean
public Encoder encoder() {
return new JacksonEncoder();
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/org/sopt/app/common/config/RestTemplateConfig.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package org.sopt.app.common.config;

import java.time.Duration;
import java.util.Map;
import lombok.val;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.retry.backoff.FixedBackOffPolicy;
import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;

@Configuration
@EnableRetry
class RestTemplateConfig {

@Bean
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
return restTemplateBuilder
Expand All @@ -25,9 +30,14 @@ public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
public ClientHttpRequestInterceptor clientHttpRequestInterceptor() {
return (request, body, execution) -> {
val retryTemplate = new RetryTemplate();
val retryPolicy = new SimpleRetryPolicy();
retryPolicy.setMaxAttempts(2);
val retryPolicy = new SimpleRetryPolicy(2, Map.of(
HttpServerErrorException.class, true,
HttpClientErrorException.class, false
));
retryTemplate.setRetryPolicy(retryPolicy);
val backOffPolicy = new FixedBackOffPolicy();
backOffPolicy.setBackOffPeriod(2000);
retryTemplate.setBackOffPolicy(backOffPolicy);
try {
return retryTemplate.execute(context -> execution.execute(request, body));
} catch (Throwable throwable) {
Expand Down

0 comments on commit 781ca87

Please sign in to comment.