๐ ์ค๋ ๋ฐฐ์ด ๋ด์ฉ!
- Spring WebFlux
- Spring WebFlux ์ ํ๋ฆฌ์ผ์ด์ vs Spring MVC ์ ํ๋ฆฌ์ผ์ด์
- Spring MVC ์ ํ๋ฆฌ์ผ์ด์ โ Spring WebFlux ์ ํ๋ฆฌ์ผ์ด์ ๋ณํ
โ๏ธ Spring WebFlux
The reactive-stack web framework, Spring WebFlux, was added later in version 5.0.
( from Spring ๊ณต์ ์ฌ์ดํธ )
- Spring 5๋ถํฐ ์ง์ํ๋ ๋ฆฌ์กํฐ๋ธ ์น ์ ํ๋ฆฌ์ผ์ด์ ์ ์ํ ์น ํ๋ ์์ํฌ
- ์ด๋ฅผ ์ฌ์ฉํด์ ๋ฆฌ์กํฐ๋ธํ ์น ์ ํ๋ฆฌ์ผ์ด์ ๊ตฌํ ๊ฐ๋ฅ
- ๋ฆฌ์กํฐ๋ธ ์คํธ๋ฆผ์ฆ(Reactive Streams)์ ์ฌ์์ธ ์ธํฐํ์ด์ค๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๋์
โ ๋ฆฌ์กํฐ๋ธ ์คํธ๋ฆผ์ฆ๋ฅผ ๊ตฌํํ ๊ตฌํ์ฒด๋ผ๋ฉด ๋๋ถ๋ถ Reactor ๋์ ์ฌ์ฉ ๊ฐ๋ฅ
( Reactor ์ด์ธ์ ๋ฆฌ์กํฐ๋ธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ReactiveAdapter
์ReactiveAdapterRegistry
๋ฅผ ํตํด ์ฌ์ฉ ๊ฐ๋ฅ )
[ ReactiveAdapter ์ฐธ๊ณ ]
[ ReactiveAdapterRegistry ์ฐธ๊ณ ]
โ๏ธ WebFlux
- ์ฝ๊ฒ ๋งํด ์น ์ ํ๋ฆฌ์ผ์ด์ ์์ ์ฌ์ฉํ ์ ์๋ Flux + Web
โ๏ธ Spring WebFlux ์ ํ๋ฆฌ์ผ์ด์ vs Spring MVC ์ ํ๋ฆฌ์ผ์ด์
โ ๊ธฐ์ ์คํ ๋น๊ต
( ์ฌ๊ธฐ์ Reactive Stack์ด Spring WebFlux / Servlet Stack์ด Spring MVC์ ํด๋น )
โ
โ Spring WebFlux
๐ Non-Blocking ํต์ ๋ฐฉ์
โ Spring MVC
๐ Blocking ํต์ ๋ฐฉ์
โก
โ Spring WebFlux
๐ Reactive Adapter๋ฅผ ์ฌ์ฉํ์ฌ Reactor ๋ฟ๋ง ์๋๋ผ RxJava ๋ฑ ๋ค๋ฅธ ๋ฆฌ์กํฐ๋ธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ ์ ์๋ ์ ์ฐํจ ์ ๊ณต
โ Spring MVC
๐ Servlet API ์คํ์ ์์กด์
โข
( Spring WebFlux
/ Spring MVC
๋ชจ๋ ๋ณด์์ ์ํด Spring Security ์ฌ์ฉ )
โ Spring WebFlux
๐ WebFilter๋ฅผ ์ฌ์ฉํด ๋ฆฌ์กํฐ๋ธ ํน์ฑ์ ๋ง๊ฒ ๋ณด์ ์ ์ฉ
โ Spring MVC
๐ ์๋ธ๋ฆฟ ํํฐ ๋ฐฉ์์ผ๋ก ๋ณด์ ์ ์ฉ
โฃ
โ Reactive Stack
๐ ์น ๊ณ์ธต(ํ๋ฆฌ์ ํ
์ด์
๊ณ์ธต, API ๊ณ์ธต)์์๋ Spring WebFlux ์ฌ์ฉ
โ Servlet Stack
๐ Spring MVC ์ฌ์ฉ
โค
โ Spring WebFlux
๐ ์์ ํ Non-Blocking ํต์ ์ ์ํด ๋ฆฌ์กํฐ๋ธ ์คํ์ ๋ฐ์ดํฐ ์ก์ธ์ค ๊ณ์ธต๊น์ง ํ์ฅ
โ Spring MVC
๐ Repository ์ฌ์ฉ
โ๏ธ R2DBC (Reactive Relation Database Connectivity)
โ ๊ด๊ณํ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ Non-Blocking ํต์ ์ ์ ์ฉํ๊ธฐ ์ํ ํ์ค ์ฌ์(Specification)
โ MySQL, Oracle ๋ฑ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ฒค๋์์๋ R2DBC ์ฌ์์ ๋ง๋ ๋๋ผ์ด๋ฒ๋ฅผ ๊ตฌํํ์ฌ ๊ณต๊ธ
โ ๋ฒค ๋ค์ด์ด๊ทธ๋จ์ ํตํ ๋น๊ต
[๋ฒค ๋ค์ด์ด๊ทธ๋จ์ ํตํ ๋น๊ต ์ฐธ๊ณ ]
( โฌ๏ธ ์ ๋ฒค๋ค์ด์ด๊ทธ๋จ์ ํตํด ๊ฐ๊ฐ์ ์ฐจ์ด์ ๊ณผ ๊ณตํต์ ์ ์ ์ ์์ )
- ๊ณตํต์
@Controleller
(or@RestController
) ๊ฐ์ ์ ๋ํ ์ด์ ์ ์ฌ์ฉ- RestTemplate์ ๋์ฒดํ ์ ์๋ WebClient์ ์ฌ์ฉ
[WebClient ์ฐธ๊ณ 1], [WebClient ์ฐธ๊ณ 2] - Tomcat, Jetty ๋ฑ์ ์๋ธ๋ฆฟ ์ปจํ ์ด๋๋ ๋ ๋ฐฉ์ ๋ชจ๋ ์ง์
โ ๋์ ๋ฐฉ์์ ์ ํ ๋ค๋ฅด์ง๋ง ํ๋ ์์ํฌ๋ฅผ ์ฌ์ฉํ๋ ์ฌ์ฉ์ ์ ์ฅ์ ๊ณ ๋ คํ์ฌ
์ต์ํ์ ๋ณ๊ฒฝ ํฌ์ธํธ๋ง ์ ์งํ๋ฉด์ ์ผ๊ด๋ ์ ๊ทผ ๋ฐฉ์์ผ๋ก ๋ ๊ธฐ์ ์ ์ฌ์ฉํ ์ ์๋๋ก ๋น์ทํ๊ฒ ๊ตฌ์ฑํจ
โ
โ Spring์์ ์ถ๊ตฌํ๋ ์ผ๊ด๋ ์๋น์ค ์ถ์ํ(PSA)๊ฐ ๊ฐ๋ ์ ์ผ๋ก ๋ฐ์๋์ด ์๋ค๊ณ ๋ณผ ์ ์์
โ ์ํ ์ฝ๋๋ฅผ ํตํ ๋น๊ต
( projects - be-template-webflux ๋ด์ ๋น๊ต ์ฝ๋ ์์ )
โ main ํด๋์ค(ํด๋ผ์ด์ธํธ) โ ๋ด๋ถ ์๋ฒ (8080) โ ์ธ๋ถ ์๋ฒ (7070) ์ ์์๋ก ๋์
โ ๊ฒฐ์ ์ ์ธ ์ฐจ์ด์
1. โ๏ธ Spring MVC
โ Thread๊ฐ ์ฐจ๋จ๋๋ Blocking I/O ๋ฐฉ์
โ SpringMvcMainSampleApplication ํด๋์ค์ ์คํ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด,
5๊ฐ์ ์์ฒญ๋ง๋ค ์ฌ๋ฌ๊ฐ์ ์ฐ๋ ๋๊ฐ ์์ฐจ์ ์ผ๋ก ๋์ํ์ฌ ์ด 25์ด ๊ฑธ๋ฆผ
โก Thread ๋ชจ๋ธ
โ request ๋น ๋งค๋ฒ thread ํ๋๊ฐ ๋งคํ๋์ด ์ฒ๋ฆฌํ๋ ๋ฐฉ์
( A thread per request )
โข ํธ๋ค๋ฌ ๋ฉ์๋์ ๋ฆฌํดํ์
์ ResponseEntity
์ฌ์ฉ
Ex. ResponseEntity<CoffeeResponseDto>
2. โ๏ธ Spring WebFlux
โ Thread๊ฐ ์ฐจ๋จ๋์ง ์๋ Non-Blocking I/O ๋ฐฉ์
โ SpringWebFluxMainSampleApplication ํด๋์ค์ ์คํ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด,
ํ๋์ ์ฐ๋ ๋์์ ์ฌ๋ฌ๊ฐ์ ์์ฒญ์ ํ๋ฒ์ ๋ฐ์ ๊ฑฐ์ ๋์์ ๋์ํ์ฌ ์ด 6์ด ๊ฑธ๋ฆผ
โก Thread ๋ชจ๋ธ
โ CPU ์ฝ์ด ๊ฐ์๋งํผ์ ์ ์ ์ฐ๋ ๋๋ก ๋๋์ ํธ๋ํฝ์ ์ฒ๋ฆฌํ๋ ์ด๋ฒคํธ ๋ฃจํ(Event loop) ๋ฐฉ์
โ๏ธ ์ด๋ฒคํธ ๋ฃจํ (Event loop)
- ๋ธ๋ผ์ฐ์ ๋ฉ์ธ ์ค๋ ๋ ๋์ ํ์ด๋ฐ์ ๊ด๋ฆฌํ๋ ๊ด๋ฆฌ์
- ์์ ๊ฐ ์ ํ ์๋๋ฅผ ๋น ๋ฅด๊ฒ ํ์ฌ, ํ ๋ฒ์ ํ๋์ ์์ ์ฉ ์ํํ์ง๋ง ๋ง์น ๋์์ ์ํํ๋ ๊ฒ์ฒ๋ผ ๋์์ํด
[์ฐธ๊ณ ]
โข ํธ๋ค๋ฌ ๋ฉ์๋์ ๋ฆฌํดํ์
์ Mono
or Flux
์ฌ์ฉ
Ex. Mono<CoffeeResponseDto>
๐ก Spring WebFlux์์๋ ResponseEntity๊ฐ ์ฌ์ฉ๋๊ธฐ๋ ํ์ง๋ง,
ResponseEntity<Mono<CoffeeResponseDto>> ์ ๊ฐ์ ํํ๋ก ์ฌ์ฉ !
โ๏ธ Spring MVC ์ ํ๋ฆฌ์ผ์ด์ โ Spring WebFlux ์ ํ๋ฆฌ์ผ์ด์ ๋ณํ
- ๋ฐ์ดํฐ ์์ธ์ค ๊ณ์ธต์ Non-Blocking I/O๋ฅผ ์ง์ํ๋ R2DBC ์ฌ์ฉ
[ R2dbcEntityTemplate ์ฐธ๊ณ 1 ], [ R2dbcEntityTemplate ์ฐธ๊ณ 2 ] - ์ฐ๊ด ๊ด๊ณ ๋งคํ ์ง์ X
( ํ ์ด๋ธ ๋ด์ ์ธ๋ํค๋ฅผ ๊ทธ๋ฅ ์จ์ฃผ๋ฉด ๋จ ) - ๋ชจ๋ ๋ก์ง์ด Mono or Flux Operator ์ฒด์ธ ๋ด์ ์์ฑ
( Operator๋ก ์์ํด์ ~ Operator๋ก ๋๋๋ค )
โ Spring WebFlux ๊ธฐ๋ฐ ํด๋์ค๋ Mono or Flux๋ก ๋ํํ ๊ฐ์ ๋ฆฌํด
โ ๏ธ ์ฃผ์ํ ์
- Spring ๋ฆฌ์กํฐ๋ธ ์คํ์ ๊ฒฝ์ฐ, H2 ์น ์ฝ์์ ์ ์์ ์ผ๋ก ์ง์ X
- Spring Data R2DBC๋ Auto DDL ๊ธฐ๋ฅ์ ์ ๊ณตํ์ง ์์
โ ์ง์ SQL ์คํฌ๋ฆฝํธ ์ค์ ์ถ๊ฐ ํด์ผํจ
๐ ์ค์ต
- projects - be-template-webflux
- git - be-homework-webflux
๐ก Spring ๋ฆฌ์กํฐ๋ธ ์คํ์ ๊ฒฝ์ฐ, H2 ์น ์ฝ์์ ์ ์์ ์ผ๋ก ์ง์ X ๋ผ๊ณ ํ์๋๋ฐ
build.gradle
ํ์ผ์ ์๋์ ๊ฐ์ด h2 ์์กด์ฑ ์ถ๊ฐํด์ฃผ๊ณ ,implementation 'com.h2database:h2' // ์ถ๊ฐ
( ์ฟผ๋ฆฌ๋ฌธ์ r2dbc ๋ก ๋๊ฐ )
application.yml
ํ์ผ์ ์๋์ ๊ฐ์ด ์ถ๊ฐ ํด์ฃผ๊ณ ,
r2dbc:
url: r2dbc:h2:mem:///test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
username: sa
password:
- ์๋์ ๊ฐ์ด ๋ง๋ค๋ฉด 8078 ํฌํธ๋ก h2 console์ ์ง์ ๊ฐ๋ฅํ๋ !!
@Slf4j
@Component
//@Profile("local")
public class H2Console {
private Server webServer;
โ
@EventListener(ContextRefreshedEvent.class)
public void start() throws java.sql.SQLException {
log.info("starting h2 console at port 8078");
this.webServer = org.h2.tools.Server.createWebServer("-webPort", "8078", "-tcpAllowOthers").start();
}
โ
@EventListener(ContextClosedEvent.class)
public void stop() {
log.info("stopping h2 console at port 8078");
this.webServer.stop();
}
๐ ๋๋์
๊ทธ๋๋ ์ดํ๊ฐ ๊ฐ๋
๋ง ๊ณต๋ถํ๋ ๊ฒ ๋ณด๋ค ์ง์ ์ฝ๋๋ฅผ ์น๊ณ Spring MVC์์ Spring WebFlux ๋ก ๋ณ๊ฒฝํ์ฌ ์ ์ฉํ๋ค๋ณด๋ ๋ ์ดํด๊ฐ ์ ๋๋ ๊ฒ ๊ฐ๋ค !
๊ทธ๋๋ ๋ฐฉ์์ ์ด๋ ต์ง ์์๋ฐ ์ ค ์ด๋ ค์ด ๊ฒ ์๋ค๋ฉด ๋๋ฌด ๋ง์ Operator๋ค๊ณผ ๊ทธ๋ค์ ์ฌ์ฉ๋ฒ ,,
๋์ค์ ์ฐจ์ฐจ ์ตํ๊ฐ์ผ๊ฒ ๋ค.
( ๋ณธ ๊ฒ์๋ฌผ์ 2022/12/02์ ์์ฑํ ๊ธ์ ์ฎ๊ธด ๊ธ์ ๋๋ค. ์๋ฌธ์ ์๊ธฐ์ ์์! )
'โข CodeStates BootCamp > Section 4' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐ [Section4] 11. [Cloud] ๋ฐฐํฌ ์ปจํ ์ด๋ Docker (0) | 2023.04.13 |
---|---|
๐ [Section4] 10. [Cloud] ์ด์ ํ๊ฒฝ ๊ตฌ์ฑ (0) | 2023.04.13 |
๐ [Section4] 8. [Spring WebFlux] Project Reactor (2) | 2023.04.13 |
๐ [Section4] 7. [Spring WebFlux] ๋ฆฌ์กํฐ๋ธ ํ๋ก๊ทธ๋๋ฐ (0) | 2023.04.13 |
๐ [Section4] 6. [Spring Security] OAuth2 2 (0) | 2023.04.13 |