Spring WebFlex

달달한단밤·2024년 11월 28일

Spring WebFlux

목록 보기
1/2

Spring WebFlex란?

  • Reactive Programming을 지원하는 Spring Framework의 모듈
  • Non-Blocking I/O 작업을 수행
  • 비동기 / 반응형 프로그래밍
  • Reactor 라이브러리를 기반으로 구현되어 있다.

Reactiv Programming

  • 변화의 전파와 데이터 흐름과 관련된 선언적 프로그래밍
  • 이벤트를 비동기적으로 처리하기 때문에 이벤트가 발생하는 즉시 처리 할 수 있다.
  • 데이터 스트림을 다룰 수 있어 이벤트가 발생할 때마다 이벤트를 처리하는 콜백 함수를 등록한다.

Non-Blocking I/O

  • 호출 직후 프로그램으로 부터 제어가 돌아옴으로서 시스템 호출 종료를 기다리지 않고 다음 처리로 넘어갈 수 있다.
  • CPU를 다른 프로세스에서 사용함으로서 I/O 대기시간을 줄이거나 활용 할 수 있다.

Reactor

  • 비동기 데이터 처리를 위해 Mono와 Flux를 제공하는 라이브러리
  • Publisher-Subscriber 패턴을 사용
  • Mono : 0개 또는 1개의 데이터를 비동기적으로 처리
  • Flux : 0개 이상의 데이터를 스트림으로 비동기 처리
  • 여러 스트림을 하나의 결과로 모아줄 때 Mono를 쓰고, 각각의 Mono를 합쳐서 여러 개의 값을 처리하는 Flux로 표현한다.

아키텍쳐

Spring boot : 3.3.5

Java : 21

lib : spring reactive web, r2dbc

db : mysql

DB

DROP DATABASE webflux;

Create DATABASE webflux;

use webflux;

create table member(
                       id varchar(100) NOT NULL UNIQUE PRIMARY KEY,
                       pass varchar(100) NOT NULL ,
                       name varchar(100) NOT NULL ,
                       create_at DATETIME NOT NULL ,
                       update_at DATETIME NOT NULL
);

create table board(
                      id bigint NOT NULL UNIQUE PRIMARY KEY auto_increment ,
                      title varchar(100) NOT NULL ,
                      content varchar(100) NOT NULL ,
                      create_at DATETIME NOT NULL ,
                      update_at DATETIME NOT NULL
);

create table member_board(
                             id bigint NOT NULL UNIQUE PRIMARY KEY auto_increment,
                             member_id varchar(100) NOT NULL ,
                             board_id bigint NOT NULL ,
                             FOREIGN KEY (member_id) REFERENCES member (id),
                             FOREIGN KEY (board_id) REFERENCES board (id)
);

Spring Boot 프로젝트 생성

Dependencies

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'io.projectreactor:reactor-test'
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

    // r2dbc
    implementation "org.springframework.boot:spring-boot-starter-data-r2dbc"
    implementation 'io.asyncer:r2dbc-mysql:1.2.0'
    testRuntimeOnly 'com.h2database:h2'
    testImplementation 'io.r2dbc:r2dbc-h2'
}

Application.yml

spring :
  application:
    name: webfluxtest
  r2dbc:
    url: r2dbc:pool:mysql://localhost:3306/webflux
    username: root
    password: wb6265

logging:
  level:
    org.springframework.r2dbc.core: debug

https://github.com/jsy6265/webfluxtest

0개의 댓글