2023. 5. 13. 09:56ㆍ이력서 스터디
RestClient
- Spring에서 제공하는 Http 통신에 사용할 수 있는 템플릿이다.
- RestTemplate과 WebClient 2가지 종류가 있다.
RestTemplate: Blocking I/O 기반의 Synchronous API
WebClient: Non-Blocking I/O 기반의 Asynchronous API
RestTemplate
- Spring이 제공하는 동기식 Http 클라이언트
- Restful서비스를 호출하는데 사용되며, Http 메서드에 대한 템플릿 메서드를 제공한다.
- 예를들어 Get, Post, Delete 등의 메서드를 지원한다.
- Spring 5.0이후로는 RestTemplate이 deprecated 되었으며, 대신 WebClient를 사용하는 것이 권장된다.
- 프로젝트에 spring-web 모듈이 있다면 RestTemplateBuilder를 빈으로 등록
RestTemplate 예제
@RestController
public class Controller{
@GetMapping("/sample")
public String sample() throws InterruptedException{
Thread.sleep(3000L);
return "sample";
}
@GetMapping("/sample2")
public String sample2() throws InterruptedException{
Thread.sleep(5000L);
return "sample2";
}
}
RestTemplateBuilder를 주입받아 RestTemplate 생성
- getForObject로 get 요청 보내기
- blocking i/o: 호출된 함수가 자신의 작업을 모두 마칠 때까지 호출한 함수에게 제어권을 넘겨주지 않고 대기하게 만듦
- sync: 호출된 함수의 작업 완료 여부를 신경쓰기 때문에 작업을 마칠때까지 기다린다.
@Component
public class RestRunner implements ApplicaitionRunner{
@Autowired
public RestTemplateBuilder restTemplateBuilder;
@Override
public void run(ApplicationArguments args) throws Exception{
RestTemplate restTemplate = restTemplateBuilder.build();
StopWatch stopWatch = new StopWatch();
stopWatch.start();
String sampleResult = restTemplate.getForObject("<http://localhost:8080/sample>", String.class);
String sample2Result = restTemplate.getForObject("<http://localhost:8080/sample2>", String.class);
stopWatch.stop();
Systemp.out.println(stopWatch.prettyPrint());
}
}
- 5초 후 sample 출력, 8초 후 sample2 출력
- sync-blocking : /sample GET요청이 끝날 때까지 기다리고, /sample2 GET요청이 끝날 때까지 기다림
- 2개의 요청이 모두 끝나려면 약 8초가 걸린다.
WebClient
- Spring 5.0에서 도입된 비동기, non-blocking, reactive 웹 클라이언트이다.
- ProjectReactor를 기반으로 하며, 비동기 처리와 백프레셔(BackPressure)를 지원한다.
- RestTemplate보다 더 효율적인 리소스 사용이 가능하며, 대량의 요청을 처리하는데 적합하다.
- RestTemplate이 deprecated된 이후, WebClient가 Restful 서비스를 호출하는 주요 도구가 되었다.
WebClient webclient = WebClient.create();
Mono<String> result = webClient.get()
.uri(url)
.retrieve()
.bodyToMono(String.class);
- Non-Blocking I/O 기반의 Asynchronous API
- WebClientAutoConfiguration
- 프로젝트에 Spring-webFlux 모듈이 있다면 webClient.Builder를 빈으로 등록
WebClient.Builder를 주입받아 WebClient 생성
- get().uri().bodyToMono(String.class)로 GET요청 보내기
- Stram API를 사용하기 때문에 subscirbe()로 결과를 반환해야 함
- non-blocking I/O: 호출된 함수가 바로 결과를 반환하여, 호출한 함수에 제어권을 넘겨준다.
- async: 호출된 함수의 작업 완료 여부를 신경쓰지 않기 때문에, 작업 완료 시 호출된 함수는 전달받은 콜백을 실행한다.
@Component
public class RestRunner implements ApplicationRunner{
@Autowired
WebClient.Builder webClientBuilder;
@Override
public void run(ApplicationArgument args) throws Exception {
WebClient webClient = webClientBuilder.builde();
StopWatch stopWatch = new StopWatch();
stopWatch.start();
Mono<String> sampleResult = webClient.get().uri("<http://localhost:8080/sample>")
.retrieve()
.bodyToMono(String.class);
sampleResult.subscribe(result -> {
System.out.println(result);
if(stopWatch.isRunning()){
stopWatch.start();
});
Mono<String> sample2Result = webClient.get().uri("<http://localhost:8080/sample2>")
.retrieve()
.bodyToMono(String.class);
worldResult.subscribe(result -> {
System.out.println(result);
if(stopWatch.isRunnig()){
stopWatch.stop();
}
System.out.println(stopWatch.prettyPrint());
stopWatch.satrt();
});
}
}
- 3초 후 sample 출력, 5초 후 sample2 출력
- async-nonblocking : /sample GET요청과 /sample2 GET요청이 병렬적으로 수행된다.
- 2개의 요청이 모두 끝나려면 약 5초가 걸린다.