라벨이 HTTP인 게시물 표시

서버 전송 이벤트(Server-Sent Events, SSE) 및 웹소켓

이미지
서버 전송 이벤트(Server-Sent Events, SSE) 및 웹소켓 1. 서버 전송 이벤트(SSE)란? 정의 : SSE는 클라이언트와 서버 간에 한 방향 통신을 제공하는 웹 표준입니다. 기능 : 서버가 클라이언트에게 새로운 데이터가 있을 때마다 푸시할 수 있습니다. 2. 웹소켓이란? 정의 : 웹소켓은 클라이언트와 서버 간의 양방향 통신을 제공합니다. 적용 : 실시간 상호작용이 필요한 애플리케이션에 적합합니다. 3. Spring Boot에서 SSE 구현 Spring WebFlux 사용 : 비동기 처리와 백프레셔 관리에 유용합니다. Spring MVC 사용 : SseEmitter 를 사용하여 SSE 구현 가능합니다. 4. WebFlux 구현 예시 Notification.java : public class Notification { private final int id; private final String content; public Notification(int id, String content) { this.id = id; this.content = content; } // getters ... } SseController.java : import org.springframework.http.codec.ServerSentEvent; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import reactor.core.publisher.Flux; import java.time.Duration; import java.util.stream.Stream; @RestController public class SseController { private List<Notifica...

WebClient 사용 예제 코드

HTTP 통신을 하기 위한 라이브러리 입니다. 리액티브 타입의 송/수신을 하여 Non-Blocking 통신을 지원합니다. 필요할 때 편하게 보기 위해 예제 위주로 기록 합니다. WebClinet 기본 설정 적용하여 Bean 으로 등록하는 방법 @Configuration public class WebClientConfig { @Bean public WebClient.Builder webClientBuilder () { return WebClient.builder() .baseUrl( "https://sample.io" ) .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); } } GET 요청을 보내는 예시 WebClient webClient = WebClient.create( "https://sample.io" ); Mono<String> result = webClient.get() .uri( "/info" ) .retrieve() .bodyToMono(String.class); result.subscribe(System.out::println); POST 요청을 보내는 예시 WebClient webClient = WebClient.create( "https://sample.io" ); Mono<String> result = webClient.post() .uri( "/info" ) ...