Skip to content

Commit

Permalink
refactor: RestClient 타임 아웃 설정 (#736)
Browse files Browse the repository at this point in the history
  • Loading branch information
sangwonsheep authored Dec 11, 2024
1 parent 591eabc commit 164246d
Showing 1 changed file with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package techpick.api.config;

import java.time.Duration;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.ClientHttpRequestFactories;
import org.springframework.boot.web.client.ClientHttpRequestFactorySettings;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.support.RestClientAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
Expand All @@ -12,6 +17,8 @@
/**
* 외부 서버와 통신하는 것을 Http Interface 방식으로 사용하기 위한 설정. <br>
* - https://docs.spring.io/spring-framework/reference/integration/rest-clients.html#rest-http-interface
* 타임 아웃 설정
* - https://www.baeldung.com/spring-rest-timeout
*/
@Configuration
public class HttpApiConfiguration {
Expand All @@ -20,10 +27,26 @@ public class HttpApiConfiguration {
private String rankingServerUrl;

@Bean
public RankingApi rankingApi() {
var restClient = RestClient.create(rankingServerUrl);
public RankingApi rankingApi(RestClient restClient) {
var adapter = RestClientAdapter.create(restClient);
var proxy = HttpServiceProxyFactory.builderFor(adapter).build();
return proxy.createClient(RankingApi.class);
}

@Bean
public RestClient restClient() {
return RestClient
.builder()
.baseUrl(rankingServerUrl)
.requestFactory(clientHttpRequestFactory())
.build();
}

@Bean
public ClientHttpRequestFactory clientHttpRequestFactory() {
ClientHttpRequestFactorySettings settings = ClientHttpRequestFactorySettings.DEFAULTS
.withConnectTimeout(Duration.ofMillis(500))
.withReadTimeout(Duration.ofMillis(500));
return ClientHttpRequestFactories.get(settings);
}
}

0 comments on commit 164246d

Please sign in to comment.