-
Notifications
You must be signed in to change notification settings - Fork 16
[이지현] Sprint12 #173
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 #173
Conversation
prod: validate, dev: update
하드 코딩된 dev 프로필의 데이터소스 정보 수정 gitignore에 .env 추가
설정 프로필 prod로 변경 compose 설정 파일에 volume 추가 schema 파일 위치 루트 폴더로 변경
JpaClockConfig 클래스 추가 각 Instant 타임 정보를 사용하는 Request 클래스의 JsonFormat의 timezone을 Asia/Seoul로 변경 Dockerfile, docker-compose에 타임존 설정 추가 application 디폴트 설정 파일에 타임존 설정 추가 AWS S3 관련 의존성 추가
AWSS3Test 서비스 클래스 생성 (S3 테스트용) - 업로드, 다운로드, 프리사인드 URL 생성 기능 설정 파일(application.yaml)에 AWS S3 관련 환경변수 설정 값 추가 docker-compose 설정 파일에 AWS S3 환경 변수 추가 BasicBinaryContentService에 S3를 사용해 파일을 생성,저장하는 로직 추가 BinaryContentController에 AWS S3용 create, donwolad API 메서드 추가 AWS S3 의존성 추가, Spring Boot 버전 변경(3.4.0 -> 3.5.5)
S3BinaryContentStorage 구현, S3Config 로직 내장(S3Client, S3Presigner 생성 로직) S3Config, AWSS3Test 제거
AWSS3Test 클래스 재생성, 외부 연동 테스트 코드 추가
application-prod 설정 파일의 ddl-auto를 validate로 변경 shcema.sql 오타 수정
.env 환경 변수 파일과 값이 겹치는 것에 의한 배포 오류 해결을 위함
8081:8081 -> 80:8081로 변경
Windows 환경에서 CRLF 개행으로 인한 docker buildx 실패 오류 해결
.gitattributes로 CRLF를 기본 LF로 변경 Dockerfile의 dos2unix 설치, 줄바꿈 변환 파트 제거
환경변수가 인식되지 않는 오류 수정
.gitignore에 CI/CD 워크플로우 폴더(.github) 대상 제외
테스트 코드에 환경 변수 주입용도 코드 추가
빌드 단계에 env로 추가
AWS 자격 증명 설정 추가로 로직 변경
다시 env 주입 로직으로 변경하고 gradle에서 test 환경에서 주입받도록 하는 코드 추가
GitHub Actions - Secrets에서 주입받도록 변경 AwsProperties 클래스 삭제 (로컬 env 주입 로직 삭제) @value를 통해 CI로 주입받은 환경변수 값을 application.yml로부터 직접 주입 CI 주입 방식 변경, 빌드 전에 주입
설정 파일을 통한 주입에서 System.getenve()로 변경
- 첨부파일이 없는 메시지는 WebSocket으로 실시간 통신
- 채널 정보 변경 시 이벤트 발송 로직의 추가가 안됨
- 채널 정보 변경 시 이벤트 발송 로직의 추가가 안됨
- WebSocket을 이관했으나 정상 동작하지 않는 오류 발견됨 (수정 필요)
- BinaryContentRequiredEventListener -> BinaryContentRequiredTopicListener - WebSocketRequiredEventListener -> WebSocketRequiredTopicListener
| 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 | ||
| @Slf4j | ||
| public class WebSocketMessageController { | ||
|
|
||
| private final MessageService messageService; | ||
|
|
||
| @MessageMapping("/messages") // /pub/messages | ||
| public void sendMessage(@Payload @Valid MessageCreateRequest request) { | ||
| log.info("WebSocket 메시지 생성 요청: request={}", request); | ||
| messageService.create(request, List.of()); | ||
| } |
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 WebSocketRequiredEventListener { | ||
|
|
||
| // private final SimpMessagingTemplate messagingTemplate; | ||
| // | ||
| // @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) | ||
| // public void handleMessage(MessageCreatedEventForWebSocket event) { | ||
| // log.info("WebSocket 메시지 생성 이벤트 수신: {}", event); | ||
| // | ||
| // MessageDto messageDto = event.getData(); | ||
| // String destination = String.format("/sub/channels.%s.messages", messageDto.channelId()); | ||
| // | ||
| // messagingTemplate.convertAndSend(destination, messageDto); | ||
| // log.debug("WebSocket 메시지 전송 완료: messageDto={}, destination={}", messageDto, destination); | ||
| // } |
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.
메세지 수신 처리
| @Repository | ||
| public class SseMessageInMemoryRepository implements SseMessageRepository { | ||
|
|
||
| private final ConcurrentLinkedDeque<UUID> eventIdQueue = new ConcurrentLinkedDeque<>(); | ||
| private final Map<UUID, SseMessage> messages = new ConcurrentHashMap<>(); | ||
| private static final int MAX_CACHE_SIZE = 500; | ||
|
|
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.
이벤트 순서 보장 및 동시성 제어
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) // 30분 간격 | ||
| public void cleanUp() { | ||
| Map<UUID, List<SseEmitter>> emitters = sseEmitterRepository.findAll(); | ||
| log.info("SSE 클린업 작업 시작: sessionCount={}", emitters.size()); | ||
|
|
||
| emitters.forEach((receiverId, emitterList) -> { | ||
| for (SseEmitter emitter : emitterList) { | ||
| if (!ping(emitter)) { | ||
| log.info("SSE 세션 만료로 제거: receiverId={}", receiverId); | ||
| sseEmitterRepository.remove(receiverId, emitter); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| log.info("SSE 클린업 완료: activeSessionCount={}", sseEmitterRepository.findAll().size()); | ||
| } |
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 { | ||
|
|
||
| @Override | ||
| public void configureMessageBroker(MessageBrokerRegistry config) { | ||
| config.enableSimpleBroker("/sub"); // 구독용 | ||
| config.setApplicationDestinationPrefixes("/pub"); // 발행용 | ||
| } | ||
|
|
||
| @Override | ||
| public void registerStompEndpoints(StompEndpointRegistry registry) { | ||
| registry.addEndpoint("/ws") | ||
| .setAllowedOriginPatterns("*") // CORS 허용 | ||
| .withSockJS(); | ||
| } | ||
| } |
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 인증 수행
기본 요구사항
웹소켓 구현하기
웹소켓 환경 구성
spring-boot-starter-websocket의존성을 추가하세요.implementation 'org.springframework.boot:spring-boot-starter-websocket'SimpleBroker를 사용하세요./pub으로 설정하세요./ws로 설정하고,SockJS연결을 지원해야 합니다.메시지 송신
/pub/messages엔드포인트에 메시지를 전송할 수 있어야 합니다.@MessageMapping을 활용하세요.MessageCreateRequest를 그대로 활용합니다.POST /api/messages)를 그대로 활용합니다.메시지 수신
/sub/channels.{channelId}.messages를 구독해 메시지를 수신합니다.MessageCreatedEvent를 통해 새로운 메시지 생성 이벤트를 확인하세요.SimpMessagingTemplate를 통해 적절한 엔드포인트로 메시지를 전송하세요.SSE 구현하기
SSE 환경을 구성하세요.
GET /api/sseconnect: SseEmitter 객체를 생성합니다.send,broadcast: SseEmitter 객체를 통해 이벤트를 전송합니다.cleanUp: 주기적으로 ping을 보내서 만료된SseEmitter객체를 삭제합니다.ping: 최초 연결 또는 만료 여부를 확인하기 위한 용도로 더미 이벤트를 보냅니다.SseEmitter객체를 메모리에서 저장하는 컴포넌트를 구현하세요.ConcurrentMap: 스레드 세이프한 자료구조를 사용합니다.List<SseEmitter>: 사용자 당 N개의 연결을 허용할 수 있도록 합니다. (예: 다중 탭)이벤트 유실 복원을 위해 SSE 메시지를 저장하는 컴포넌트를 구현하세요.
LastEventId를 전송해 이벤트 유실 복원이 가능하도록 해야 합니다.기존에 클라이언트에서 폴링 방식으로 주기적으로 요청하던 데이터를 SSE를 이용해 서버에서 실시간으로 전달하는 방식으로 리팩토링하세요.
새로운 알림 이벤트 전송
새 알림이 생성되었을 때 클라이언트에 이벤트를 전송하세요.
클라이언트는 이 이벤트를 수신하면 알림 목록에 알림을 추가합니다.
이벤트 명세
파일 업로드 상태 변경 이벤트 전송
파일 업로드 상태가 변경될 때 이벤트를 발송하세요.
클라이언트는 해당 상태를 수신하면 파일 상태 UI를 다시 렌더링합니다.
이벤트 명세
채널 갱신 이벤트 전송
채널 정보가 변경될 때, 이벤트를 발송하세요.
클라이언트는 해당 이벤트를 수신하면 채널 UI를 다시 렌더링합니다.
이벤트 명세
사용자 갱신 이벤트 전송
사용자 정보 또는 로그인 상태가 변경될 때, 이벤트를 발송하세요.
클라이언트는 해당 이벤트를 수신하면 사용자 UI를 다시 렌더링합니다.
이벤트 명세
배포 아키텍처 구성하기
Reverse Proxy/api/*,/ws/*요청은 Backend 컨테이너로 프록시 처리합니다.3000번포트를 통해 접근할 수 있어야 합니다.BackendReverse Proxy를 통해/api/*,/ws/*요청이 이 서버로 전달됩니다.DB,Memory DB,Message BrokerBackend컨테이너가 접근 가능한 다음의 인프라 컨테이너들을 구성하세요심화 요구사항
웹소켓 인증/인가 처리하기
인증 처리
CONNECT프레임의 헤더에 다음과 같이Authorization토큰을 포함합니다.ChannelInterceptor를 구현하여 연결 시 토큰을 검증하고, 인증된 사용자 정보를SecurityContext에 설정해야 합니다.| 참고 문서: https://docs.spring.io/spring-framework/reference/web/websocket/stomp/authentication-token-based.html
CONNECT프레임일 때 엑세스 토큰을 검증하는JwtAuthenticationChannelInterceptor구현체를 정의하세요.검증 로직은 이전에 구현한
JwtAuthenticationFilter를 참고하세요.인증이 완료되면
SecurityContext에 인증정보를 저장하는 대신accessor객체에 저장하세요.SecurityContextChannelInterceptor를 등록하여 이후 메시지 처리 흐름에서도 인증 정보를 활용할 수 있도록 구성하세요.인가 처리
AuthorizationChannelInterceptor를 사용해 메시지 권한 검사를 수행합니다.AuthorizationChannelInterceptor를 활용하기 위해의존성을 추가하세요.implementation 'org.springframework.security:spring-security-messaging'MessageMatcherDelegatingAuthorizationManager를 활용해 인가 정책을 정의하고, 채널에 추가하세요.분산 환경 배포 아키텍처 구성하기
다음의 다이어그램에 부합하는 배포 아키텍처를 Docker Compose를 통해 구현하세요.
Backend-*deploy.replicas설정을 활용하세요.Reverse Proxyupstream블록을 수정해 다음의 로드밸런싱 전략을 적용해Backend로 트래픽을 분산시켜보세요.기본값$upstream_addr변수를 활용해 실제 요청을 처리하는 서버의 IP를 헤더에 추가하고 브라우저 개발자 도구를 활용해 비교해보세요.분산환경에 따른
InMemoryJwtRegistry의 한계점을 식별하고 Redis를 활용해 리팩토링하세요.어떤 한계가 있는지 식별하고 PR에 남겨주세요.
RedisJwtRegistry구현체를 활용하세요.분산환경에 따른 웹소켓과 SSE의 한계점을 식별하고 Kafka를 활용해 리팩토링하세요.
어떤 한계가 있는지 식별하고 PR에 남겨주세요.
일반적인 카프카 이벤트와 다르게 각 서버 인스턴스마다 이벤트를 받을 수 있어야 합니다. 따라서
컨슈머 group id를 적절히 설정하세요.주요 변경사항
스크린샷
멘토에게