Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Improve/rest template] RestTemplate Singleton 으로 변경 #127

Merged
merged 2 commits into from
Jul 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ dependencies {
implementation 'org.projectlombok:lombok:1.18.26'
developmentOnly 'org.springframework.boot:spring-boot-devtools'

// http client
implementation 'org.apache.httpcomponents:httpclient'
implementation 'com.google.code.gson:gson:2.10.1'

// metadata config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
@Service
@RequiredArgsConstructor
public class RestTemplateService {
private final RestTemplate restTemplate;

public String postRequestToSlack(String uri, String sender, Object data) {
SlackPostRequest request = new SlackPostRequest(sender, data, ":love_letter:");
Expand All @@ -18,12 +19,12 @@ public String postRequestToSlack(String uri, String sender, Object data) {
}

public String getToUri(String uri) {
return new RestTemplate().getForObject(uri, String.class);
return restTemplate.getForObject(uri, String.class);
}

private String sendPost(String uri, SlackPostRequest request) {
return String.valueOf(
new RestTemplate().exchange(uri, HttpMethod.POST, request.toEntity(), String.class).toString()
restTemplate.exchange(uri, HttpMethod.POST, request.toEntity(), String.class)
);
}
}
21 changes: 20 additions & 1 deletion src/main/java/com/service/core/config/web/WebConfig.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.service.core.config.web;

import com.service.core.auth.presentation.AuthMemberArgumentResolver;

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
Expand Down Expand Up @@ -39,4 +44,18 @@ public UrlBasedCorsConfigurationSource corsConfigurationSource() {
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
resolvers.add(authMemberArgumentResolver);
}
}

@Bean
public RestTemplate buildRestTemplate(){
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setConnectTimeout(5000); // 연결시간초과, ms
factory.setReadTimeout(5000); // 읽기시간초과, ms
HttpClient httpClient = HttpClientBuilder.create()
.setMaxConnTotal(100) // 최대 오픈되는 커넥션 수
.setMaxConnPerRoute(5) // IP,포트 1쌍에 대해 수행 할 연결 수
.build();
factory.setHttpClient(httpClient);

return new RestTemplate(factory);
}
}
Loading