From 0da011039bbcec934d04fd64859ea84ad050e0f5 Mon Sep 17 00:00:00 2001 From: sua kang Date: Wed, 14 Feb 2024 22:42:27 +0900 Subject: [PATCH 1/8] =?UTF-8?q?bugfix:=20user=20=EC=9E=85=EC=9E=A5?= =?UTF-8?q?=EC=8B=9C=20=EB=A9=94=EC=8B=9C=EC=A7=80=EB=A7=8C=20=EB=82=A0?= =?UTF-8?q?=EC=95=84=EA=B0=80=EA=B3=A0=20db=EB=B0=98=EC=98=81=20=EC=95=88?= =?UTF-8?q?=EB=90=98=EB=8A=94=20=EB=B2=84=EA=B7=B8=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/kafka/Producers.java | 3 +- .../controller/ChatController.java | 2 +- .../repository/RoomRepository.java | 3 + .../repository/RoomRepositoryCustom.java | 3 + .../repository/RoomRepositoryImpl.java | 8 +++ .../service/ChatMessageService.java | 68 +++++++++++-------- .../util/constant/MessageType.java | 2 +- 7 files changed, 55 insertions(+), 34 deletions(-) diff --git a/src/backend/chatting-server/src/main/java/chattingserver/config/kafka/Producers.java b/src/backend/chatting-server/src/main/java/chattingserver/config/kafka/Producers.java index fc19a579..eb98410c 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/config/kafka/Producers.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/config/kafka/Producers.java @@ -38,7 +38,7 @@ public class Producers { private final RoomService roomService; public void sendMessage(ChatMessageDto chatMessageDto) { - if (chatMessageDto.getMessageType() == MessageType.ENTRANCE) { + if (chatMessageDto.getMessageType() == MessageType.NOTE) { log.info("producers.sendMessage.if MessageType == ENTRANCE"); RoomResponseDto roomResponseDto = roomService.getRoomInfo(chatMessageDto.getRoomId()); List receivers = roomResponseDto.getUsers().stream().map(UserListResponseDto::getUid).collect(Collectors.toList()); @@ -48,7 +48,6 @@ public void sendMessage(ChatMessageDto chatMessageDto) { .roomResponseDto(roomResponseDto) .build()); } else { - log.info("producers.sendMessage.if MessageType != ENTRANCE"); CompletableFuture> completableFuture = chatKafkaTemplate.send(topicChatName, chatMessageDto); completableFuture.whenComplete((result, ex) -> { diff --git a/src/backend/chatting-server/src/main/java/chattingserver/controller/ChatController.java b/src/backend/chatting-server/src/main/java/chattingserver/controller/ChatController.java index 7a6121f2..7306a41a 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/controller/ChatController.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/controller/ChatController.java @@ -90,7 +90,7 @@ public ResponseEntity allMessagesAtRoom(@PathVariable String r return new ResponseEntity<>(apiMessage, HttpStatus.OK); } - @Operation(summary = "채팅 메시지 Pagination", description = "내림차순으로 해당 채팅방 메시지 Pagination, 사이즈 N = 12 고정") + @Operation(summary = "채팅 메시지 Pagination", description = "내림차순으로 해당 채팅방 메시지 Pagination, 사이즈 N = 20 고정") @GetMapping("/history") public ResponseEntity chatMessagePagination( @RequestParam String roomId, diff --git a/src/backend/chatting-server/src/main/java/chattingserver/repository/RoomRepository.java b/src/backend/chatting-server/src/main/java/chattingserver/repository/RoomRepository.java index ef6fa013..884de581 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/repository/RoomRepository.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/repository/RoomRepository.java @@ -1,6 +1,7 @@ package chattingserver.repository; import chattingserver.domain.room.Room; +import chattingserver.domain.room.User; import org.springframework.data.domain.Sort; import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.data.mongodb.repository.Query; @@ -24,4 +25,6 @@ public interface RoomRepository extends MongoRepository, RoomRepos @Override boolean existsById(String s); + + } \ No newline at end of file diff --git a/src/backend/chatting-server/src/main/java/chattingserver/repository/RoomRepositoryCustom.java b/src/backend/chatting-server/src/main/java/chattingserver/repository/RoomRepositoryCustom.java index d63a123c..00007873 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/repository/RoomRepositoryCustom.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/repository/RoomRepositoryCustom.java @@ -1,10 +1,13 @@ package chattingserver.repository; +import chattingserver.domain.room.User; import chattingserver.dto.request.ReadMessageUpdateRequestDto; import com.mongodb.client.result.UpdateResult; public interface RoomRepositoryCustom { void exitRoom(String roomId, Long uid); + UpdateResult addUserToRoom(String roomId, User user); + UpdateResult updateLastReadMsgId(ReadMessageUpdateRequestDto requestDto); } diff --git a/src/backend/chatting-server/src/main/java/chattingserver/repository/RoomRepositoryImpl.java b/src/backend/chatting-server/src/main/java/chattingserver/repository/RoomRepositoryImpl.java index 63135945..0b086331 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/repository/RoomRepositoryImpl.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/repository/RoomRepositoryImpl.java @@ -1,6 +1,7 @@ package chattingserver.repository; import chattingserver.domain.room.Room; +import chattingserver.domain.room.User; import chattingserver.dto.request.ReadMessageUpdateRequestDto; import com.mongodb.client.result.UpdateResult; import org.springframework.data.mongodb.core.MongoTemplate; @@ -37,4 +38,11 @@ public UpdateResult updateLastReadMsgId(ReadMessageUpdateRequestDto requestDto) return mongoTemplate.updateFirst(query, update, Room.class); } + + @Override + public UpdateResult addUserToRoom(String roomId, User user) { + Query query = new Query(Criteria.where("_id").is(roomId)); + Update update = new Update().addToSet("users", user); + return mongoTemplate.updateFirst(query, update, Room.class); + } } \ No newline at end of file diff --git a/src/backend/chatting-server/src/main/java/chattingserver/service/ChatMessageService.java b/src/backend/chatting-server/src/main/java/chattingserver/service/ChatMessageService.java index 5d749507..a2fa95bf 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/service/ChatMessageService.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/service/ChatMessageService.java @@ -14,7 +14,9 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.data.domain.Page; +import org.springframework.data.mongodb.MongoTransactionManager; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.WebSocketSession; @@ -35,6 +37,7 @@ public class ChatMessageService { private final RoomRepository roomRepository; private final EntityToResponseDtoConverter entityToResponseDtoConverter; + private static final int SIZE = 20; public void sendMessage(WebSocketSession session, T message) { @@ -87,39 +90,44 @@ public void deleteChat(String id) { chatMessageRepository.deleteById(id); } + @Transactional public ChatMessageDto join(ChatMessageDto chatMessageDto) { + try { + Optional optionalRoom = roomRepository.findById(chatMessageDto.getRoomId()); + if (optionalRoom.isEmpty()) { + log.info("해당하는 방이 없습니다. roomId={}", chatMessageDto.getRoomId()); + // TODO null처리 + return null; + } - Optional optionalRoom = roomRepository.findById(chatMessageDto.getRoomId()); - if (optionalRoom.isEmpty()) { - log.info("해당하는 방이 없습니다. roomId={}", chatMessageDto.getRoomId()); - // TODO null처리 - return null; + Room room = optionalRoom.get(); + + ChatMessage message = chatMessageRepository.save(ChatMessage.builder() + .messageType(MessageType.ENTRANCE) + .roomId(chatMessageDto.getRoomId()) + .senderId(chatMessageDto.getSenderId()) + .nickName(chatMessageDto.getNickName()) + .content(chatMessageDto.getNickName() + "님이 입장하셨습니다.") + .senderProfileImage(chatMessageDto.getSenderProfileImage()) + .createdAt(LocalDateTime.now()) + .build()); + + User joinedUser = User.builder() + .uid(chatMessageDto.getSenderId()) + .nickName(chatMessageDto.getNickName()) + .profileImage(chatMessageDto.getSenderProfileImage()) + .enteredAt(LocalDateTime.now()) + .lastReadMessageId(message.getId()) + .build(); + + roomRepository.addUserToRoom(room.getId(), joinedUser); + + return entityToResponseDtoConverter.convertMessage(message); + } catch (Exception e) { + log.error("트랜잭션 오류: {}", e.getMessage()); + // TODO 트랜잭션 롤백 또는 예외 처리 로직 추가 + throw new RuntimeException("트랜잭션 오류 발생"); } - - Room room = optionalRoom.get(); - - ChatMessage message = chatMessageRepository.save(ChatMessage.builder() - .messageType(MessageType.ENTRANCE) - .roomId(chatMessageDto.getRoomId()) - .senderId(chatMessageDto.getSenderId()) - .nickName(chatMessageDto.getNickName()) - .content(chatMessageDto.getNickName() + "님이 입장하셨습니다.") - .senderProfileImage(chatMessageDto.getSenderProfileImage()) - .createdAt(LocalDateTime.now()) - .build()); - - User joinedUser = User.builder() - .uid(chatMessageDto.getSenderId()) - .nickName(chatMessageDto.getNickName()) - .profileImage(chatMessageDto.getSenderProfileImage()) - .enteredAt(LocalDateTime.now()) - .lastReadMessageId(message.getId()) - .build(); - - room.getUsers().add(joinedUser); - - return entityToResponseDtoConverter.convertMessage(message); - } public ChatMessageDto permanentLeaving(String roomId, UserEntranceRequestDto userDto) { diff --git a/src/backend/chatting-server/src/main/java/chattingserver/util/constant/MessageType.java b/src/backend/chatting-server/src/main/java/chattingserver/util/constant/MessageType.java index 907e16c8..5404c4d3 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/util/constant/MessageType.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/util/constant/MessageType.java @@ -1,5 +1,5 @@ package chattingserver.util.constant; public enum MessageType{ - ENTRANCE, MSG + ENTRANCE, NOTE, MSG } From 9cde7d311f2773b57c19be6aaa8507c223fd1ab4 Mon Sep 17 00:00:00 2001 From: sua kang Date: Wed, 14 Feb 2024 23:00:43 +0900 Subject: [PATCH 2/8] =?UTF-8?q?bugfix:=20=EB=B0=A9=20=EC=9E=A0=EC=8B=9C=20?= =?UTF-8?q?=EB=82=98=EA=B0=80=EA=B8=B0=20=EC=8B=9C=20lastreadmessage=20?= =?UTF-8?q?=EC=97=85=EB=8D=B0=EC=9D=B4=ED=8A=B8=20=EC=95=88=EB=90=98?= =?UTF-8?q?=EB=8A=94=20=EB=B2=84=EA=B7=B8=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/request/ReadMessageUpdateRequestDto.java | 2 +- .../chattingserver/repository/ChatMessageRepositoryImpl.java | 4 +++- .../java/chattingserver/repository/RoomRepositoryImpl.java | 3 +-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/backend/chatting-server/src/main/java/chattingserver/dto/request/ReadMessageUpdateRequestDto.java b/src/backend/chatting-server/src/main/java/chattingserver/dto/request/ReadMessageUpdateRequestDto.java index 1d4e313c..a19b5187 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/dto/request/ReadMessageUpdateRequestDto.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/dto/request/ReadMessageUpdateRequestDto.java @@ -14,7 +14,7 @@ public class ReadMessageUpdateRequestDto { @NotBlank private String roomId; @NotBlank - private String uid; + private Long uid; @NotBlank private String messageId; } \ No newline at end of file diff --git a/src/backend/chatting-server/src/main/java/chattingserver/repository/ChatMessageRepositoryImpl.java b/src/backend/chatting-server/src/main/java/chattingserver/repository/ChatMessageRepositoryImpl.java index 3bb5b713..1d9a11e8 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/repository/ChatMessageRepositoryImpl.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/repository/ChatMessageRepositoryImpl.java @@ -77,9 +77,11 @@ public ChatMessage getLastMessage(String roomId) { @Override public List findPreviousMessages(String roomId, String readMsgId, int limit) { + ObjectId readMsgObjectId = new ObjectId(readMsgId); Query query = new Query(); - query.addCriteria(Criteria.where("roomId").is(roomId).and("_id").lt(readMsgId)); // 이전 메시지만 가져오도록 쿼리 설정 + query.addCriteria(Criteria.where("roomId").is(roomId).and("_id").lt(readMsgObjectId)); query.limit(20); // 최대 20개의 메시지 가져오도록 제한 설정 return mongoTemplate.find(query, ChatMessage.class); } + //TODO error처리 이전 메시지 못가져오고 빈배열 반환되는중 lt가 문제임 } diff --git a/src/backend/chatting-server/src/main/java/chattingserver/repository/RoomRepositoryImpl.java b/src/backend/chatting-server/src/main/java/chattingserver/repository/RoomRepositoryImpl.java index 0b086331..c8acc799 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/repository/RoomRepositoryImpl.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/repository/RoomRepositoryImpl.java @@ -30,12 +30,11 @@ public void exitRoom(String roomId, Long uid) { public UpdateResult updateLastReadMsgId(ReadMessageUpdateRequestDto requestDto) { Query query = Query.query( - Criteria.where("roomId").is(requestDto.getRoomId()) + Criteria.where("_id").is(requestDto.getRoomId()) .andOperator(Criteria.where("users").elemMatch(Criteria.where("uid").is(requestDto.getUid()))) ); Update update = new Update().set("users.$.lastReadMessageId", requestDto.getMessageId()); - return mongoTemplate.updateFirst(query, update, Room.class); } From 7e665c05ddfc71aa79228f4aff0f8626755a49cb Mon Sep 17 00:00:00 2001 From: sua kang Date: Thu, 15 Feb 2024 13:23:00 +0900 Subject: [PATCH 3/8] =?UTF-8?q?style:=20TODO=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chattingserver/repository/ChatMessageRepositoryImpl.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/backend/chatting-server/src/main/java/chattingserver/repository/ChatMessageRepositoryImpl.java b/src/backend/chatting-server/src/main/java/chattingserver/repository/ChatMessageRepositoryImpl.java index 1d9a11e8..4aa39c9a 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/repository/ChatMessageRepositoryImpl.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/repository/ChatMessageRepositoryImpl.java @@ -80,8 +80,7 @@ public List findPreviousMessages(String roomId, String readMsgId, i ObjectId readMsgObjectId = new ObjectId(readMsgId); Query query = new Query(); query.addCriteria(Criteria.where("roomId").is(roomId).and("_id").lt(readMsgObjectId)); - query.limit(20); // 최대 20개의 메시지 가져오도록 제한 설정 + query.limit(20); return mongoTemplate.find(query, ChatMessage.class); } - //TODO error처리 이전 메시지 못가져오고 빈배열 반환되는중 lt가 문제임 } From 88337c045db5554dd8785d38e1c772ebbebb451c Mon Sep 17 00:00:00 2001 From: sua kang Date: Thu, 15 Feb 2024 13:54:07 +0900 Subject: [PATCH 4/8] =?UTF-8?q?feat:=20SearchServer=EC=97=90=20=EC=B1=84?= =?UTF-8?q?=ED=8C=85=EB=B0=A9=20=EC=A0=95=EB=B3=B4=20=EC=83=89=EC=9D=B8=20?= =?UTF-8?q?=EC=9A=94=EC=B2=AD=ED=95=98=EB=8A=94=20REST=20client=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/chatting-server/build.gradle | 2 ++ .../chattingserver/config/AsyncConfig.java | 29 ++++++++++++++++ .../config/OpenFeignConfig.java | 10 ++++++ .../controller/RoomController.java | 4 +++ .../controller/SearchProxy.java | 12 +++++++ .../request/IndexingRequestMessageDto.java | 18 ++++++++++ .../chattingserver/service/SearchService.java | 33 +++++++++++++++++++ 7 files changed, 108 insertions(+) create mode 100644 src/backend/chatting-server/src/main/java/chattingserver/config/AsyncConfig.java create mode 100644 src/backend/chatting-server/src/main/java/chattingserver/config/OpenFeignConfig.java create mode 100644 src/backend/chatting-server/src/main/java/chattingserver/controller/SearchProxy.java create mode 100644 src/backend/chatting-server/src/main/java/chattingserver/dto/request/IndexingRequestMessageDto.java create mode 100644 src/backend/chatting-server/src/main/java/chattingserver/service/SearchService.java diff --git a/src/backend/chatting-server/build.gradle b/src/backend/chatting-server/build.gradle index 48bc0db9..f5702159 100644 --- a/src/backend/chatting-server/build.gradle +++ b/src/backend/chatting-server/build.gradle @@ -35,6 +35,8 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-mongodb' + implementation 'org.springframework.cloud:spring-cloud-starter-openfeign:4.0.3' + annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor' } diff --git a/src/backend/chatting-server/src/main/java/chattingserver/config/AsyncConfig.java b/src/backend/chatting-server/src/main/java/chattingserver/config/AsyncConfig.java new file mode 100644 index 00000000..af102a78 --- /dev/null +++ b/src/backend/chatting-server/src/main/java/chattingserver/config/AsyncConfig.java @@ -0,0 +1,29 @@ +package chattingserver.config; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; + +import java.util.concurrent.Executor; + +@Slf4j +@EnableAsync +@Configuration +public class AsyncConfig { + + @Bean(name = "search") + public Executor pushAsyncExecutor() { + ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); + int processors = Runtime.getRuntime().availableProcessors(); + log.info("processors count {}", processors); + executor.setThreadNamePrefix("SearchAsync- "); + executor.setCorePoolSize(processors); + executor.setMaxPoolSize(processors * 2); + executor.setQueueCapacity(50); + executor.setKeepAliveSeconds(60); + executor.initialize(); + return executor; + } +} diff --git a/src/backend/chatting-server/src/main/java/chattingserver/config/OpenFeignConfig.java b/src/backend/chatting-server/src/main/java/chattingserver/config/OpenFeignConfig.java new file mode 100644 index 00000000..893bcbab --- /dev/null +++ b/src/backend/chatting-server/src/main/java/chattingserver/config/OpenFeignConfig.java @@ -0,0 +1,10 @@ +package chattingserver.config; + +import org.springframework.cloud.openfeign.EnableFeignClients; +import org.springframework.context.annotation.Configuration; + +@Configuration +@EnableFeignClients(basePackages = {"chattingserver.controller"}) +public class OpenFeignConfig { + +} diff --git a/src/backend/chatting-server/src/main/java/chattingserver/controller/RoomController.java b/src/backend/chatting-server/src/main/java/chattingserver/controller/RoomController.java index 59d60616..f3836e9b 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/controller/RoomController.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/controller/RoomController.java @@ -12,6 +12,7 @@ import chattingserver.dto.response.UserListResponseDto; import chattingserver.service.ChatMessageService; import chattingserver.service.RoomService; +import chattingserver.service.SearchService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Schema; @@ -37,6 +38,7 @@ public class RoomController { private final RoomService roomService; private final ChatMessageService chatMessageService; + private final SearchService searchService; private final Producers producers; @Operation(summary = "채팅방 생성 API", description = "신규 채팅방 생성", responses = { @@ -56,6 +58,8 @@ public ResponseEntity groupCreation(@Valid @RequestBody RoomCr apiMessage.setMessage(CommonAPIMessage.ResultEnum.success); apiMessage.setData(roomResponseDto); + searchService.sendIndexingRequestToSearchServer(roomResponseDto); + return new ResponseEntity<>(apiMessage, HttpStatus.CREATED); } diff --git a/src/backend/chatting-server/src/main/java/chattingserver/controller/SearchProxy.java b/src/backend/chatting-server/src/main/java/chattingserver/controller/SearchProxy.java new file mode 100644 index 00000000..3849c2a9 --- /dev/null +++ b/src/backend/chatting-server/src/main/java/chattingserver/controller/SearchProxy.java @@ -0,0 +1,12 @@ +package chattingserver.controller; + +import chattingserver.dto.request.IndexingRequestMessageDto; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; + +@FeignClient(name = "search-service", url = "localhost:20000") +public interface SearchProxy { + @PostMapping("/search/index/send") + void sendIndexingRequest(@RequestBody IndexingRequestMessageDto dto); +} diff --git a/src/backend/chatting-server/src/main/java/chattingserver/dto/request/IndexingRequestMessageDto.java b/src/backend/chatting-server/src/main/java/chattingserver/dto/request/IndexingRequestMessageDto.java new file mode 100644 index 00000000..f4972453 --- /dev/null +++ b/src/backend/chatting-server/src/main/java/chattingserver/dto/request/IndexingRequestMessageDto.java @@ -0,0 +1,18 @@ +package chattingserver.dto.request; + +import chattingserver.domain.room.Playlist; +import chattingserver.dto.response.UserListResponseDto; +import lombok.Builder; +import lombok.Data; + +import java.time.LocalDateTime; +import java.util.List; + +@Data +@Builder +public class IndexingRequestMessageDto { + private String roomId; + private String roomName; + private String playlistId; + private String thumbnailImage; +} \ No newline at end of file diff --git a/src/backend/chatting-server/src/main/java/chattingserver/service/SearchService.java b/src/backend/chatting-server/src/main/java/chattingserver/service/SearchService.java new file mode 100644 index 00000000..6a3beddd --- /dev/null +++ b/src/backend/chatting-server/src/main/java/chattingserver/service/SearchService.java @@ -0,0 +1,33 @@ +package chattingserver.service; + +import chattingserver.controller.SearchProxy; +import chattingserver.dto.request.IndexingRequestMessageDto; +import chattingserver.dto.response.RoomResponseDto; +import feign.FeignException; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Service; + +@Slf4j +@Service +@Async("search") +@RequiredArgsConstructor +public class SearchService { + private final SearchProxy searchProxy; + + public void sendIndexingRequestToSearchServer(RoomResponseDto roomResponseDto) { + + try { + searchProxy.sendIndexingRequest(IndexingRequestMessageDto.builder() + .roomId(roomResponseDto.getId()) + .roomName(roomResponseDto.getRoomName()) + .playlistId(roomResponseDto.getPlaylist().getId()) + .thumbnailImage(roomResponseDto.getThumbnailImage()) + .build()); + } catch (FeignException e) { + log.error("searchProxy.sendIndexingRequest, 검색 서버 연결 불가"); + } + log.info("검색 서버에 인덱싱 요청 성공"); + } +} From 101bb3f242febd861b85e8c6f49c4d27bac822e5 Mon Sep 17 00:00:00 2001 From: sua kang Date: Thu, 15 Feb 2024 14:43:53 +0900 Subject: [PATCH 5/8] =?UTF-8?q?bugfix:=20spring=20cloud=20netflix=20-=20op?= =?UTF-8?q?enfeign=20=EB=B2=84=EC=A0=84=20=EB=B9=8C=EB=93=9C=20=EB=B2=84?= =?UTF-8?q?=EA=B7=B8=20fix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/chatting-server/build.gradle | 12 +++++++++++- src/backend/chatting-server/docker-compose.yml | 11 +++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/backend/chatting-server/build.gradle b/src/backend/chatting-server/build.gradle index f5702159..37d089ab 100644 --- a/src/backend/chatting-server/build.gradle +++ b/src/backend/chatting-server/build.gradle @@ -35,11 +35,21 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-mongodb' - implementation 'org.springframework.cloud:spring-cloud-starter-openfeign:4.0.3' + implementation 'org.springframework.cloud:spring-cloud-starter-openfeign:4.1.0' annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor' } +ext { + set('springCloudVersion', "2023.0.0") +} + +dependencyManagement { + imports { + mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" + } +} + tasks.named('test') { useJUnitPlatform() } diff --git a/src/backend/chatting-server/docker-compose.yml b/src/backend/chatting-server/docker-compose.yml index efb8a561..05318f6c 100644 --- a/src/backend/chatting-server/docker-compose.yml +++ b/src/backend/chatting-server/docker-compose.yml @@ -36,6 +36,17 @@ services: networks: - lalala-network + sonarqube: + image: sonarqube:latest + ports: + - 18200:9000 + networks: + - lalala-network + environment: + - SONAR_JDBC_URL=jdbc:postgresql://postgres:5432/sonar + - SONAR_JDBC_USERNAME=sonar + - SONAR_JDBC_PASSWORD=sonar + volumes: chatting-mongodbdata: From 24ddb448019cada3f26ff76572596f3b6549724a Mon Sep 17 00:00:00 2001 From: sua kang Date: Thu, 15 Feb 2024 16:27:59 +0900 Subject: [PATCH 6/8] =?UTF-8?q?refactor:=20=EC=9E=85=ED=87=B4=EC=9E=A5=20?= =?UTF-8?q?=EB=A9=94=EC=8B=9C=EC=A7=80,=20=EC=9D=BC=EB=B0=98=20=EB=A9=94?= =?UTF-8?q?=EC=8B=9C=EC=A7=80=20=ED=86=A0=ED=94=BD=20=EA=B5=AC=EB=B6=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/chatting-server/docker-compose.yml | 11 ----------- .../java/chattingserver/config/kafka/Consumers.java | 7 +++---- .../java/chattingserver/config/kafka/Producers.java | 4 ++-- 3 files changed, 5 insertions(+), 17 deletions(-) diff --git a/src/backend/chatting-server/docker-compose.yml b/src/backend/chatting-server/docker-compose.yml index 05318f6c..efb8a561 100644 --- a/src/backend/chatting-server/docker-compose.yml +++ b/src/backend/chatting-server/docker-compose.yml @@ -36,17 +36,6 @@ services: networks: - lalala-network - sonarqube: - image: sonarqube:latest - ports: - - 18200:9000 - networks: - - lalala-network - environment: - - SONAR_JDBC_URL=jdbc:postgresql://postgres:5432/sonar - - SONAR_JDBC_USERNAME=sonar - - SONAR_JDBC_PASSWORD=sonar - volumes: chatting-mongodbdata: diff --git a/src/backend/chatting-server/src/main/java/chattingserver/config/kafka/Consumers.java b/src/backend/chatting-server/src/main/java/chattingserver/config/kafka/Consumers.java index 03971aca..0300265f 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/config/kafka/Consumers.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/config/kafka/Consumers.java @@ -21,10 +21,9 @@ public void listenChat(ChatMessageDto chatMessageDto) { } @KafkaListener(groupId = "${spring.kafka.consumer.room-consumer.group-id}", topics = "${kafka.topic.room-name}", containerFactory = "kafkaListenerContainerFactory") - public void listenGroupCreation(RoomMessageDto roomMessageDto) { + public void listenRoomMessage(RoomMessageDto roomMessageDto) { RoomResponseDto roomResponseDto = roomMessageDto.getRoomResponseDto(); - for (Long userId : roomMessageDto.getReceivers()) { - template.convertAndSend("/chat/topic/new-room/" + userId, roomResponseDto); - } + template.convertAndSend("/chat/topic/room/" + roomMessageDto.getRoomResponseDto().getId(), roomResponseDto); + } } \ No newline at end of file diff --git a/src/backend/chatting-server/src/main/java/chattingserver/config/kafka/Producers.java b/src/backend/chatting-server/src/main/java/chattingserver/config/kafka/Producers.java index eb98410c..4b451455 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/config/kafka/Producers.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/config/kafka/Producers.java @@ -38,7 +38,7 @@ public class Producers { private final RoomService roomService; public void sendMessage(ChatMessageDto chatMessageDto) { - if (chatMessageDto.getMessageType() == MessageType.NOTE) { + if (chatMessageDto.getMessageType() == MessageType.ENTRANCE) { log.info("producers.sendMessage.if MessageType == ENTRANCE"); RoomResponseDto roomResponseDto = roomService.getRoomInfo(chatMessageDto.getRoomId()); List receivers = roomResponseDto.getUsers().stream().map(UserListResponseDto::getUid).collect(Collectors.toList()); @@ -47,7 +47,7 @@ public void sendMessage(ChatMessageDto chatMessageDto) { .receivers(receivers) .roomResponseDto(roomResponseDto) .build()); - } else { + } else if (chatMessageDto.getMessageType() == MessageType.MSG){ CompletableFuture> completableFuture = chatKafkaTemplate.send(topicChatName, chatMessageDto); completableFuture.whenComplete((result, ex) -> { From 16753de84947fef7e0484869ed2862588e4cd698 Mon Sep 17 00:00:00 2001 From: sua kang Date: Thu, 15 Feb 2024 17:02:00 +0900 Subject: [PATCH 7/8] =?UTF-8?q?feat:=20=EC=B9=B4=ED=94=84=EC=B9=B4?= =?UTF-8?q?=EB=A1=9C=20=EC=B1=84=ED=8C=85=EB=B0=A9=20=EC=83=9D=EC=84=B1?= =?UTF-8?q?=EC=8B=9C=20=EA=B2=80=EC=83=89=20=EC=84=9C=EB=B2=84=EB=A1=9C=20?= =?UTF-8?q?=EC=9D=B4=EB=B2=A4=ED=8A=B8=20=EB=B0=9C=ED=96=89=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/kafka/Consumers.java | 10 +++---- .../config/kafka/KafkaProducerConfig.java | 7 +++-- .../config/kafka/Producers.java | 26 +++++++++---------- .../controller/RoomController.java | 9 ++++--- .../request/IndexingRequestMessageDto.java | 12 ++++----- .../util/constant/MessageType.java | 2 +- 6 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/backend/chatting-server/src/main/java/chattingserver/config/kafka/Consumers.java b/src/backend/chatting-server/src/main/java/chattingserver/config/kafka/Consumers.java index 0300265f..b964907f 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/config/kafka/Consumers.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/config/kafka/Consumers.java @@ -1,8 +1,7 @@ package chattingserver.config.kafka; import chattingserver.dto.ChatMessageDto; -import chattingserver.dto.RoomMessageDto; -import chattingserver.dto.response.RoomResponseDto; +import chattingserver.dto.request.IndexingRequestMessageDto; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.kafka.annotation.KafkaListener; @@ -20,10 +19,11 @@ public void listenChat(ChatMessageDto chatMessageDto) { template.convertAndSend("/chat/topic/room/" + chatMessageDto.getRoomId(), chatMessageDto); } + // TODO search indexing test용 추후 삭제 예정 @KafkaListener(groupId = "${spring.kafka.consumer.room-consumer.group-id}", topics = "${kafka.topic.room-name}", containerFactory = "kafkaListenerContainerFactory") - public void listenRoomMessage(RoomMessageDto roomMessageDto) { - RoomResponseDto roomResponseDto = roomMessageDto.getRoomResponseDto(); - template.convertAndSend("/chat/topic/room/" + roomMessageDto.getRoomResponseDto().getId(), roomResponseDto); + public void listenRoomCreation(IndexingRequestMessageDto indexingRequestMessageDto) { + + template.convertAndSend("/chat/topic/search/room/index", indexingRequestMessageDto); } } \ No newline at end of file diff --git a/src/backend/chatting-server/src/main/java/chattingserver/config/kafka/KafkaProducerConfig.java b/src/backend/chatting-server/src/main/java/chattingserver/config/kafka/KafkaProducerConfig.java index 5edcd8c1..2e29ab36 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/config/kafka/KafkaProducerConfig.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/config/kafka/KafkaProducerConfig.java @@ -1,8 +1,7 @@ package chattingserver.config.kafka; import chattingserver.dto.ChatMessageDto; -import chattingserver.dto.RoomMessageDto; -import chattingserver.dto.response.ChatMessageResponseDto; +import chattingserver.dto.request.IndexingRequestMessageDto; import org.apache.kafka.clients.producer.ProducerConfig; import org.apache.kafka.common.serialization.StringSerializer; import org.springframework.beans.factory.annotation.Value; @@ -51,7 +50,7 @@ public KafkaTemplate chatKafkaTemplate() { } @Bean - public ProducerFactory roomProducerFactory() { + public ProducerFactory roomProducerFactory() { return new DefaultKafkaProducerFactory<>(roomProducerConfigurations()); } @@ -65,7 +64,7 @@ public Map roomProducerConfigurations() { } @Bean - public KafkaTemplate roomKafkaTemplate() { + public KafkaTemplate roomKafkaTemplate() { return new KafkaTemplate<>(roomProducerFactory()); } } diff --git a/src/backend/chatting-server/src/main/java/chattingserver/config/kafka/Producers.java b/src/backend/chatting-server/src/main/java/chattingserver/config/kafka/Producers.java index 4b451455..bd2f824d 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/config/kafka/Producers.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/config/kafka/Producers.java @@ -1,9 +1,8 @@ package chattingserver.config.kafka; -import chattingserver.domain.room.User; import chattingserver.dto.ChatMessageDto; import chattingserver.dto.RoomMessageDto; -import chattingserver.dto.response.ChatMessageResponseDto; +import chattingserver.dto.request.IndexingRequestMessageDto; import chattingserver.dto.response.RoomResponseDto; import chattingserver.dto.response.UserListResponseDto; import chattingserver.service.ChatMessageService; @@ -32,22 +31,23 @@ public class Producers { @Value("${kafka.topic.room-name}") private String topicRoomName; - private final KafkaTemplate roomKafkaTemplate; + private final KafkaTemplate roomKafkaTemplate; private final ChatMessageService chatMessageService; private final RoomService roomService; public void sendMessage(ChatMessageDto chatMessageDto) { - if (chatMessageDto.getMessageType() == MessageType.ENTRANCE) { - log.info("producers.sendMessage.if MessageType == ENTRANCE"); + if (chatMessageDto.getMessageType() == MessageType.CREATION) { RoomResponseDto roomResponseDto = roomService.getRoomInfo(chatMessageDto.getRoomId()); List receivers = roomResponseDto.getUsers().stream().map(UserListResponseDto::getUid).collect(Collectors.toList()); receivers.remove(chatMessageDto.getSenderId()); - sendRoomMessage(RoomMessageDto.builder() - .receivers(receivers) - .roomResponseDto(roomResponseDto) + sendRoomMessage(IndexingRequestMessageDto.builder() + .roomId(roomResponseDto.getId()) + .roomName(roomResponseDto.getRoomName()) + .playlistId(roomResponseDto.getPlaylist().getId()) + .thumbnailImage(roomResponseDto.getThumbnailImage()) .build()); - } else if (chatMessageDto.getMessageType() == MessageType.MSG){ + } else { CompletableFuture> completableFuture = chatKafkaTemplate.send(topicChatName, chatMessageDto); completableFuture.whenComplete((result, ex) -> { @@ -62,14 +62,14 @@ public void sendMessage(ChatMessageDto chatMessageDto) { } } - public void sendRoomMessage(RoomMessageDto roomMessageDto) { - CompletableFuture> completableFuture = roomKafkaTemplate.send(topicRoomName, roomMessageDto); + public void sendRoomMessage(IndexingRequestMessageDto roomMessageDto) { + CompletableFuture> completableFuture = roomKafkaTemplate.send(topicRoomName, roomMessageDto); completableFuture.whenComplete((result, ex) -> { if (ex == null) { - log.info("메시지 전송 성공=[" + roomMessageDto.getRoomResponseDto().getId() + "] with offset=[" + result.getRecordMetadata().offset() + "]"); + log.info("메시지 전송 성공=[" + roomMessageDto.getRoomId() + "] with offset=[" + result.getRecordMetadata().offset() + "]"); log.info("roomMessageDto={}", roomMessageDto.toString()); } else { - log.info("메시지 전송 불가=[" + roomMessageDto.getRoomResponseDto().getId() + "] 원인 : " + ex.getMessage()); + log.info("메시지 전송 불가=[" + roomMessageDto.getRoomId() + "] 원인 : " + ex.getMessage()); } }); } diff --git a/src/backend/chatting-server/src/main/java/chattingserver/controller/RoomController.java b/src/backend/chatting-server/src/main/java/chattingserver/controller/RoomController.java index f3836e9b..36d91843 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/controller/RoomController.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/controller/RoomController.java @@ -3,6 +3,7 @@ import chattingserver.config.kafka.Producers; import chattingserver.domain.room.User; import chattingserver.dto.RoomMessageDto; +import chattingserver.dto.request.IndexingRequestMessageDto; import chattingserver.dto.request.ReadMessageUpdateRequestDto; import chattingserver.dto.request.RoomCreateRequestDto; import chattingserver.dto.request.UserEntranceRequestDto; @@ -49,9 +50,11 @@ public ResponseEntity groupCreation(@Valid @RequestBody RoomCr RoomResponseDto roomResponseDto = roomService.create(roomCreateRequestDto); - producers.sendRoomMessage(RoomMessageDto.builder() - .receivers(roomResponseDto.getUsers().stream().map(UserListResponseDto::getUid).collect(Collectors.toList())) - .roomResponseDto(roomResponseDto) + producers.sendRoomMessage(IndexingRequestMessageDto.builder() + .roomId(roomResponseDto.getId()) + .roomName(roomResponseDto.getRoomName()) + .playlistId(roomResponseDto.getPlaylist().getId()) + .thumbnailImage(roomResponseDto.getThumbnailImage()) .build()); CommonAPIMessage apiMessage = new CommonAPIMessage(); diff --git a/src/backend/chatting-server/src/main/java/chattingserver/dto/request/IndexingRequestMessageDto.java b/src/backend/chatting-server/src/main/java/chattingserver/dto/request/IndexingRequestMessageDto.java index f4972453..5b386da8 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/dto/request/IndexingRequestMessageDto.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/dto/request/IndexingRequestMessageDto.java @@ -1,14 +1,12 @@ package chattingserver.dto.request; -import chattingserver.domain.room.Playlist; -import chattingserver.dto.response.UserListResponseDto; -import lombok.Builder; -import lombok.Data; +import lombok.*; -import java.time.LocalDateTime; -import java.util.List; -@Data +@ToString +@AllArgsConstructor +@NoArgsConstructor +@Getter @Builder public class IndexingRequestMessageDto { private String roomId; diff --git a/src/backend/chatting-server/src/main/java/chattingserver/util/constant/MessageType.java b/src/backend/chatting-server/src/main/java/chattingserver/util/constant/MessageType.java index 5404c4d3..622149e2 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/util/constant/MessageType.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/util/constant/MessageType.java @@ -1,5 +1,5 @@ package chattingserver.util.constant; public enum MessageType{ - ENTRANCE, NOTE, MSG + ENTRANCE, CREATION, MSG } From 822405e5612b8d246a0e041452d1930988dc7b4d Mon Sep 17 00:00:00 2001 From: sua kang Date: Thu, 15 Feb 2024 17:35:30 +0900 Subject: [PATCH 8/8] =?UTF-8?q?style:=20api=20=ED=98=95=EC=8B=9D=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/chattingserver/config/util/SwaggerConfig.java | 2 +- .../main/java/chattingserver/controller/ChatController.java | 2 +- .../main/java/chattingserver/controller/RoomController.java | 6 +----- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/backend/chatting-server/src/main/java/chattingserver/config/util/SwaggerConfig.java b/src/backend/chatting-server/src/main/java/chattingserver/config/util/SwaggerConfig.java index d8abc5c2..11f77c88 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/config/util/SwaggerConfig.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/config/util/SwaggerConfig.java @@ -17,7 +17,7 @@ public class SwaggerConfig { @Bean public GroupedOpenApi chatOpenApi() { - String[] paths = {"/api/**", "/chat/**"}; + String[] paths = {"/v1/**", "/**/chat"}; return GroupedOpenApi.builder() .group("CHATTING-SERVER API V1") diff --git a/src/backend/chatting-server/src/main/java/chattingserver/controller/ChatController.java b/src/backend/chatting-server/src/main/java/chattingserver/controller/ChatController.java index 7306a41a..760c7603 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/controller/ChatController.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/controller/ChatController.java @@ -27,7 +27,7 @@ @Tag(name = "chat", description = "채팅 API") @RequiredArgsConstructor @RestController -@RequestMapping("/chat") +@RequestMapping("v1/api/chat") @Slf4j public class ChatController { diff --git a/src/backend/chatting-server/src/main/java/chattingserver/controller/RoomController.java b/src/backend/chatting-server/src/main/java/chattingserver/controller/RoomController.java index 36d91843..e8fae3bf 100644 --- a/src/backend/chatting-server/src/main/java/chattingserver/controller/RoomController.java +++ b/src/backend/chatting-server/src/main/java/chattingserver/controller/RoomController.java @@ -1,8 +1,6 @@ package chattingserver.controller; import chattingserver.config.kafka.Producers; -import chattingserver.domain.room.User; -import chattingserver.dto.RoomMessageDto; import chattingserver.dto.request.IndexingRequestMessageDto; import chattingserver.dto.request.ReadMessageUpdateRequestDto; import chattingserver.dto.request.RoomCreateRequestDto; @@ -10,7 +8,6 @@ import chattingserver.dto.response.CommonAPIMessage; import chattingserver.dto.response.JoinedRoomResponseDto; import chattingserver.dto.response.RoomResponseDto; -import chattingserver.dto.response.UserListResponseDto; import chattingserver.service.ChatMessageService; import chattingserver.service.RoomService; import chattingserver.service.SearchService; @@ -28,12 +25,11 @@ import java.util.HashMap; import java.util.List; -import java.util.stream.Collectors; @Tag(name = "room", description = "채팅방 API") @Slf4j @RestController -@RequestMapping("/api/v1/rooms") +@RequestMapping("/v1/api/rooms") @RequiredArgsConstructor public class RoomController {