refactor: RestTemplateConfig 설정 추가
This commit is contained in:
parent
da74a5756a
commit
e0874a1bde
10
pom.xml
10
pom.xml
@ -91,7 +91,15 @@
|
||||
<artifactId>json-simple</artifactId>
|
||||
<version>1.1.1</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>4.5.13</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@ -0,0 +1,65 @@
|
||||
package com.itn.mjonApi.cmn.config;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* packageName : com.itn.mjonApi.cmn.config
|
||||
* fileName : RestTemplateConfig
|
||||
* author : hylee
|
||||
* date : 2023-05-08
|
||||
* description :
|
||||
* ===========================================================
|
||||
* DATE AUTHOR NOTE
|
||||
* -----------------------------------------------------------
|
||||
* 2023-05-08 hylee 최초 생성
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
public class RestTemplateConfig {
|
||||
/*
|
||||
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
|
||||
|
||||
|
||||
|
||||
*//*return new RestTemplateBuilder()
|
||||
// 로깅 인터셉터에서 Stream을 소비하므로 BufferingClientHttpRequestFactory 을 꼭 써야한다.
|
||||
.requestFactory(() -> new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()))
|
||||
// 타임아웃 설정
|
||||
.setConnectTimeout(Duration.ofMillis(5000)) // connection-timeout
|
||||
.setReadTimeout(Duration.ofMillis(5000)) // read-timeout
|
||||
//메시지 컨버터 추가
|
||||
.additionalMessageConverters(new StringHttpMessageConverter(Charset.forName("UTF-8")))
|
||||
// 로깅 인터셉터 설정
|
||||
.additionalInterceptors(new RestTemplateLoggingInterceptor())
|
||||
.build();*//*
|
||||
|
||||
|
||||
// Apache HttpComponents
|
||||
HttpClient httpClient = HttpClientBuilder.create()
|
||||
.setMaxConnTotal(50)//최대 커넥션 수
|
||||
.setMaxConnPerRoute(20)
|
||||
.build(); //각 호스트(IP와 Port 의 조합)당 커넥션 풀에 생성가능한 커넥션 수
|
||||
|
||||
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
|
||||
factory.setHttpClient(httpClient);
|
||||
|
||||
RestTemplate restTemplate = restTemplateBuilder
|
||||
// .requestFactory(() -> factory)
|
||||
.setReadTimeout(Duration.ofSeconds(5)) // read timeout
|
||||
.setConnectTimeout(Duration.ofSeconds(5)) // connection timeout
|
||||
.additionalMessageConverters(new StringHttpMessageConverter(StandardCharsets.UTF_8)) //메시지 컨버터 추가
|
||||
.additionalInterceptors(new RestTemplateLoggingInterceptor())
|
||||
.build();
|
||||
|
||||
// 로깅 DEBUG 레벨이 활성화된 경우에만 BufferingClientHttpRequest 사용
|
||||
if (log.isDebugEnabled()) {
|
||||
ClientHttpRequestFactory clientHttpRequestFactory = new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory());
|
||||
restTemplate.setRequestFactory(clientHttpRequestFactory);
|
||||
return restTemplate;
|
||||
}
|
||||
return restTemplate;
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
@ -0,0 +1,54 @@
|
||||
package com.itn.mjonApi.cmn.interceptor;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpRequest;
|
||||
import org.springframework.http.client.ClientHttpRequestExecution;
|
||||
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* packageName : com.itn.mjonApi.cmn.interceptor
|
||||
* fileName : RestTemplateLoggingInterceptor
|
||||
* author : hylee
|
||||
* date : 2023-05-08
|
||||
* description :
|
||||
* ===========================================================
|
||||
* DATE AUTHOR NOTE
|
||||
* -----------------------------------------------------------
|
||||
* 2023-05-08 hylee 최초 생성
|
||||
*/
|
||||
@Slf4j
|
||||
public class RestTemplateLoggingInterceptor implements ClientHttpRequestInterceptor {
|
||||
|
||||
@Override
|
||||
public ClientHttpResponse intercept(HttpRequest req, byte[] body, ClientHttpRequestExecution ex) throws IOException {
|
||||
final String sessionNumber = makeSessionNumber();
|
||||
printRequest(sessionNumber, req, body);
|
||||
ClientHttpResponse response = ex.execute(req, body);
|
||||
printResponse(sessionNumber, response);
|
||||
return response;
|
||||
}
|
||||
|
||||
private String makeSessionNumber() {
|
||||
return Integer.toString((int) (Math.random() * 1000000));
|
||||
}
|
||||
|
||||
private void printRequest(final String sessionNumber, final HttpRequest req, final byte[] body) {
|
||||
log.info("[{}] \n URI: {}\n, Method: {}\n, Headers:{}\n, Body:{} \n",
|
||||
sessionNumber, req.getURI(), req.getMethod(), req.getHeaders(), new String(body, StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private void printResponse(final String sessionNumber, final ClientHttpResponse res) throws IOException {
|
||||
String body = new BufferedReader(new InputStreamReader(res.getBody(), StandardCharsets.UTF_8)).lines()
|
||||
.collect(Collectors.joining("\n"));
|
||||
|
||||
log.info("[{}] \n Status: {}\n, Headers:{}\n, Body:{}\n ",
|
||||
sessionNumber, res.getStatusCode(), res.getHeaders(), body);
|
||||
}
|
||||
}
|
||||
@ -1,7 +1,10 @@
|
||||
package com.itn.mjonApi.mjon.api.web;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import com.itn.mjonApi.cmn.idgen.mapper.domain.SendMsgVO;
|
||||
import com.itn.mjonApi.cmn.msg.PlainResponse;
|
||||
import com.itn.mjonApi.cmn.msg.RestResponse;
|
||||
import com.itn.mjonApi.mjon.api.service.AccessKeyService;
|
||||
import com.itn.mjonApi.mjon.api.service.AccessTokenService;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@ -10,11 +13,7 @@ import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.itn.mjonApi.cmn.idgen.mapper.domain.SendMsgVO;
|
||||
import com.itn.mjonApi.cmn.msg.PlainResponse;
|
||||
import com.itn.mjonApi.cmn.msg.RestResponse;
|
||||
import com.itn.mjonApi.mjon.api.service.AccessKeyService;
|
||||
import com.itn.mjonApi.mjon.api.service.AccessTokenService;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* @author User
|
||||
@ -48,7 +47,7 @@ public class AccessKeyRestController {
|
||||
*/
|
||||
@GetMapping("/api/accessKey/SendMsg")
|
||||
public ResponseEntity<RestResponse> apiaccessKeysendMsg(
|
||||
@ModelAttribute SendMsgVO sendMsgVO
|
||||
SendMsgVO sendMsgVO
|
||||
){
|
||||
//step2.api 처리
|
||||
//단문 문자 발송 처리
|
||||
|
||||
Loading…
Reference in New Issue
Block a user