Spring WebFlux
리액티브한 웹 애플리케이션
- Spring WebFlux의 경우 Non-Blocking 통신을 지원
- Spring WebFlux의 경우 Reactive Adapter를 사용해서 Reactor 뿐만 아니라 RxJava 등의 다른 리액티브 라이브러리를 사용할 수 있는 유연함을 제공
- WebFilter를 사용해 리액티브 특성에 맞게 인증과 권한 등의 보안을 적용
Spring WebFlux의 Non-Blocking 처리 방식
@Slf4j
@RestController
@RequestMapping("/v")
public class SpringWebFluxMainCoffeeController {
String uri = "http://localhost:5050/v11/coffees/1";
@ResponseStatus(HttpStatus.OK)
@GetMapping("/{coffee-id}")
public Mono<CoffeeResponseDto> getCoffee(@PathVariable("coffee-id") long coffeeId) throws InterruptedException {
log.info("# call Spring WebFlux Main Controller: {}", LocalDateTime.now());
return WebClient.create() //WebClient라는 Rest Client 사용 -> Non-Blocking 방식의 Rest Client
.get()
.uri(uri)
.retrieve()
.bodyToMono(ResponseDto.class);
}
}
Spring WebFlux 기반 메인 애플리케이션을 호출하는 클라이언트 샘플 코드
@Slf4j
@SpringBootApplication
public class SpringWebFluxMainSampleApplication {
public static void main(String[] args) {
System.setProperty("reactor.netty.ioWorkerCount", "1");
SpringApplication.run(SpringWebFluxMainSampleApplication.class, args);
}
@Bean
public CommandLineRunner run() {
return (String... args) -> {
log.info("# 요청 시작 시간: {}", LocalTime.now());
// (1)
for (int i = 1; i <= 5; i++) {
this.getCoffee()
.subscribe(
response -> {
log.info("{}: coffee name: {}", LocalTime.now(), response.getKorName());
}
);
}
};
}
private Mono<CoffeeResponseDto> getCoffee() {
String uri = "http://localhost:6060/v11/coffees/1";
return WebClient.create()
.get()
.uri(uri)
.retrieve()
.bodyToMono(CoffeeResponseDto.class);
}
}