-
Notifications
You must be signed in to change notification settings - Fork 16
[김민수] Sprint12 #165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 김민수
Are you sure you want to change the base?
[김민수] Sprint12 #165
Conversation
| import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; | ||
|
|
||
| @Configuration | ||
| @EnableWebSocketMessageBroker |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
STOMP 사용 활성화 처리
| @Controller | ||
| @RequiredArgsConstructor | ||
| public class MessageWebSocketController { | ||
| private final SimpMessagingTemplate simpMessagingTemplate; | ||
| private final MessageService messageService; | ||
| private final ApplicationEventPublisher applicationEventPublisher; | ||
|
|
||
| @MessageMapping("/messages") | ||
| public void sendMessage(@Payload MessageCreateRequest messageCreateRequest) { | ||
| Message message = messageService.createMessage(messageCreateRequest, null); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
메세지 송신 처리
| @Component | ||
| @RequiredArgsConstructor | ||
| public class WebSocketRequiredEventListener { | ||
| private final SimpMessagingTemplate messagingTemplate; | ||
|
|
||
| @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) | ||
| public void handleMessage(MessageCreatedEvent event) { | ||
| messagingTemplate.convertAndSend("/sub/channels." + event.message().getChannelId() + ".messages", event.message()); | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
메세지 수신 처리
| eventPublisher.publishEvent(new MessageCreatedEvent(message)); | ||
| return message; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이벤트 발행
| private final ConcurrentLinkedDeque<UUID> eventIdQueue = new ConcurrentLinkedDeque<>(); | ||
| private final Map<UUID, SseMessage> Messages = new ConcurrentHashMap<>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이벤트 순서 보장 및 동시성 보장
| @Component | ||
| public class SseMessageListener { | ||
|
|
||
| private final SseService sseService; | ||
|
|
||
| public SseMessageListener(SseService sseService) { | ||
| this.sseService = sseService; | ||
| } | ||
|
|
||
| @EventListener | ||
| public void handleMessage(SseMessageEvent event) { | ||
| sseService.broadcast(event.name(), event.data()); | ||
| } | ||
|
|
||
| @EventListener | ||
| public void handle(SseNotificationEvent event) { | ||
| sseService.send(event.receiverIds(), event.name(), event.data()); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SSE 이벤트 소비
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
docker-compose 설정 완료 (nginx 제외)
| @Scheduled(fixedDelay = 1000 * 60 * 30) | ||
| @Override | ||
| public void cleanUp() { | ||
| Collection<UUID> receiverIds = sseEmitterRepository.getReceiverIds(); | ||
| for (UUID receiverId : receiverIds) { | ||
| List<SseEmitter> emitters = sseEmitterRepository.get(receiverId); | ||
| for (SseEmitter emitter : emitters) { | ||
| if (!ping(emitter)) { | ||
| sseEmitterRepository.remove(receiverId, emitter); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private boolean ping(SseEmitter emitter) { | ||
| try { | ||
| emitter.send(SseEmitter.event().name("ping").data("keep-alive")); | ||
| return true; | ||
| } catch (IOException | IllegalStateException e) { | ||
| return false; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
주기적으로 응답 없는 연결 제외
| public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { | ||
| private final JwtAuthenticationChannelInterceptor jwtAuthenticationChannelInterceptor; | ||
|
|
||
| public WebSocketConfig(JwtAuthenticationChannelInterceptor jwtAuthenticationChannelInterceptor) { | ||
| this.jwtAuthenticationChannelInterceptor = jwtAuthenticationChannelInterceptor; | ||
| } | ||
|
|
||
| @Override | ||
| public void registerStompEndpoints(StompEndpointRegistry registry) { | ||
| registry.addEndpoint("/ws") | ||
| .setAllowedOriginPatterns("*") | ||
| .withSockJS(); | ||
| } | ||
|
|
||
| @Override | ||
| public void configureMessageBroker(MessageBrokerRegistry registry) { | ||
| registry.setApplicationDestinationPrefixes("/pub"); | ||
| registry.enableSimpleBroker("/sub"); | ||
| } | ||
|
|
||
| @Override | ||
| public void configureClientInboundChannel(ChannelRegistration registration) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WebSocketMessageBrokerConfigurer 통해 HTTP 요청 간 JWT 인증 수행
| - target: 8081 | ||
| depends_on: [ postgres, redis, broker] | ||
| deploy: | ||
| replicas: 3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
분산 환경 3대 구축
요구사항
기본
웹소켓 구현하기
spring-boot-starter-websocket의존성을 추가하세요.SimpleBroker를 사용하세요./sub으로 설정하세요./pub으로 설정하세요./ws로 설정하고,SockJS연결을 지원해야 합니다./pub/messages엔드포인트에 메시지를 전송할 수 있어야 합니다.@MessageMapping을 활용하세요.MessageCreateRequest를 그대로 활용합니다.POST /api/messages)를 그대로 활용합니다./sub/channels.{channelId}.messages를 구독해 메시지를 수신합니다.MessageCreatedEvent를 통해 새로운 메시지 생성 이벤트를 확인하세요.SimpMessagingTemplate를 통해 적절한 엔드포인트로 메시지를 전송하세요.SSE 구현하기
GET /api/sseconnect: SseEmitter 객체를 생성합니다.send,broadcast: SseEmitter 객체를 통해 이벤트를 전송합니다.cleanUp: 주기적으로 ping을 보내서 만료된SseEmitter객체를 삭제합니다.ping: 최초 연결 또는 만료 여부를 확인하기 위한 용도로 더미 이벤트를 보냅니다.SseEmitter객체를 메모리에서 저장하는 컴포넌트를 구현하세요.ConcurrentMap: 스레드 세이프한 자료구조를 사용합니다.List<SseEmitter>: 사용자 당 N개의 연결을 허용할 수 있도록 합니다. (예: 다중 탭)LastEventId를 전송해 이벤트 유실 복원이 가능하도록 해야 합니다.notifications.createdNotificationDtobinaryContents.updatedBinaryContentDtochannels.createdorupdatedordeletedChannelDtousers.createdorupdatedordeletedUserDto배포 아키텍처 구성하기
심화
웹소켓 인증/인가 처리하기
CONNECT프레임의 헤더에 다음과 같이Authorization토큰을 포함합니다.ChannelInterceptor를 구현하여 연결 시 토큰을 검증하고, 인증된 사용자 정보를SecurityContext에 설정해야 합니다.CONNECT프레임일 때 엑세스 토큰을 검증하는JwtAuthenticationChannelInterceptor구현체를 정의하세요.JwtAuthenticationFilter를 참고하세요.SecurityContext에 인증정보를 저장하는 대신accessor객체에 저장하세요.SecurityContextChannelInterceptor를 등록하여 이후 메시지 처리 흐름에서도 인증 정보를 활용할 수 있도록 구성하세요.AuthorizationChannelInterceptor를 사용해 메시지 권한 검사를 수행합니다.AuthorizationChannelInterceptor를 활용하기 위해의존성을 추가하세요.MessageMatcherDelegatingAuthorizationManager를 활용해 인가 정책을 정의하고, 채널에 추가하세요.분산 환경 배포 아키텍처 구성하기
zkdz2mts7-image.png
Backend-*deploy.replicas설정을 활용하세요.Reverse Proxyupstream블록을 수정해 다음의 로드밸런싱 전략을 적용해 Backend로 트래픽을 분산시켜보세요.기본값$upstream_addr변수를 활용해 실제 요청을 처리하는 서버의 IP를 헤더에 추가하고 브라우저 개발자 도구를 활용해 비교해보세요.분산환경에 따른
InMemoryJwtRegistry의 한계점을 식별하고 Redis를 활용해 리팩토링하세요.RedisJwtRegistry구현체를 활용하세요.분산환경에 따른 웹소켓과 SSE의 한계점을 식별하고 Kafka를 활용해 리팩토링하세요.
컨슈머 group id를 적절히 설정하세요.주요 변경사항
docker compose를 사용하면서 비즈니스 로직과 Frontend, Nginx 폴더를 분리해서 3개의 폴더로 올렸습니다
심화 요구사항 구현 결과 3개의 분산 환경을 구현하는데는 성공하였지만 일관성 문제가 있어 403 Forbidden 오류가 발생하는 상황입니다.
멘토에게