From 5da7b093ab57b6c8cc61ad4bcc13a40d7bf303d5 Mon Sep 17 00:00:00 2001 From: Minky Date: Mon, 2 Dec 2024 17:00:13 +0900 Subject: [PATCH] =?UTF-8?q?[FEAT]=20=EC=A1=B0=ED=9A=8C=EC=88=98=20?= =?UTF-8?q?=EA=B8=B0=EB=B0=98=20=EB=9E=AD=ED=82=B9=20=EB=AA=A8=EB=93=88=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84=20(#649)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/build.gradle | 1 - .../java/techpick/TechPickApiApplication.java | 2 +- .../src/main/resources/application.yaml | 2 + .../src/main/resources/logback-spring.xml | 3 + .../techpick/TechPickBatchApplication.java | 8 +- .../core/rabbitmq/EventMessageAspect.java | 7 +- .../core/rabbitmq/RabbitmqConfig.java | 4 + .../src/main/resources/application-core.yaml | 24 ++-- backend/techpick-ranking/build.gradle | 2 + .../techpick/TechPickRankingApplication.java | 3 +- .../java/techpick/ranking/LinkViewCount.java | 33 +++++ .../ranking/LinkViewCountRepository.java | 26 ++++ .../ranking/LinkViewRankingService.java | 64 +++++++++ .../techpick/ranking/RankingController.java | 47 +++++++ .../application/ProducerController.java | 46 ------- .../ranking/config/RabbitmqConfig.java | 122 ------------------ .../ranking/domain/ProducerService.java | 64 --------- .../ranking/domain/messageComponent.java | 27 ---- .../java/techpick/ranking/dto/MessageDto.java | 22 ---- .../java/techpick/ranking/entity/Message.java | 26 ---- .../techpick/ranking/entity/PickPick.java | 36 ------ .../ranking/repository/MessageRepository.java | 11 -- .../repository/PickPickRepository.java | 10 -- .../java/techpick/ranking/test/EmitLog.java | 29 ----- .../java/techpick/ranking/test/NewTask.java | 30 ----- .../techpick/ranking/test/ReceiveLogs.java | 32 ----- .../main/java/techpick/ranking/test/Recv.java | 31 ----- .../main/java/techpick/ranking/test/Send.java | 24 ---- .../java/techpick/ranking/test/Worker.java | 50 ------- .../src/main/resources/application.yaml | 15 +-- .../src/main/resources/logback-spring.xml | 4 + 31 files changed, 217 insertions(+), 588 deletions(-) create mode 100644 backend/techpick-ranking/src/main/java/techpick/ranking/LinkViewCount.java create mode 100644 backend/techpick-ranking/src/main/java/techpick/ranking/LinkViewCountRepository.java create mode 100644 backend/techpick-ranking/src/main/java/techpick/ranking/LinkViewRankingService.java create mode 100644 backend/techpick-ranking/src/main/java/techpick/ranking/RankingController.java delete mode 100644 backend/techpick-ranking/src/main/java/techpick/ranking/application/ProducerController.java delete mode 100644 backend/techpick-ranking/src/main/java/techpick/ranking/config/RabbitmqConfig.java delete mode 100644 backend/techpick-ranking/src/main/java/techpick/ranking/domain/ProducerService.java delete mode 100644 backend/techpick-ranking/src/main/java/techpick/ranking/domain/messageComponent.java delete mode 100644 backend/techpick-ranking/src/main/java/techpick/ranking/dto/MessageDto.java delete mode 100644 backend/techpick-ranking/src/main/java/techpick/ranking/entity/Message.java delete mode 100644 backend/techpick-ranking/src/main/java/techpick/ranking/entity/PickPick.java delete mode 100644 backend/techpick-ranking/src/main/java/techpick/ranking/repository/MessageRepository.java delete mode 100644 backend/techpick-ranking/src/main/java/techpick/ranking/repository/PickPickRepository.java delete mode 100644 backend/techpick-ranking/src/main/java/techpick/ranking/test/EmitLog.java delete mode 100644 backend/techpick-ranking/src/main/java/techpick/ranking/test/NewTask.java delete mode 100644 backend/techpick-ranking/src/main/java/techpick/ranking/test/ReceiveLogs.java delete mode 100644 backend/techpick-ranking/src/main/java/techpick/ranking/test/Recv.java delete mode 100644 backend/techpick-ranking/src/main/java/techpick/ranking/test/Send.java delete mode 100644 backend/techpick-ranking/src/main/java/techpick/ranking/test/Worker.java diff --git a/backend/build.gradle b/backend/build.gradle index 1e034d75f..a1e45523c 100644 --- a/backend/build.gradle +++ b/backend/build.gradle @@ -48,7 +48,6 @@ subprojects { // RabbitMQ implementation 'org.springframework.boot:spring-boot-starter-amqp' implementation 'org.springframework.amqp:spring-amqp:3.2.0' - implementation 'com.rabbitmq:amqp-client:5.23.0' // lombok annotation compileOnly 'org.projectlombok:lombok' diff --git a/backend/techpick-api/src/main/java/techpick/TechPickApiApplication.java b/backend/techpick-api/src/main/java/techpick/TechPickApiApplication.java index 18f6ca904..5b7b0ae2d 100644 --- a/backend/techpick-api/src/main/java/techpick/TechPickApiApplication.java +++ b/backend/techpick-api/src/main/java/techpick/TechPickApiApplication.java @@ -4,7 +4,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication( - scanBasePackages = {"techpick.api", "techpick.core", "techpick.security"} + // scanBasePackages = {"techpick.api", "techpick.core", "techpick.security"} ) public class TechPickApiApplication { public static void main(String[] args) { diff --git a/backend/techpick-api/src/main/resources/application.yaml b/backend/techpick-api/src/main/resources/application.yaml index a7a86c5fc..f77b629cb 100644 --- a/backend/techpick-api/src/main/resources/application.yaml +++ b/backend/techpick-api/src/main/resources/application.yaml @@ -2,6 +2,8 @@ spring: profiles: include: - core + application: + name: techpick-api springdoc: swagger-ui: diff --git a/backend/techpick-api/src/main/resources/logback-spring.xml b/backend/techpick-api/src/main/resources/logback-spring.xml index ead09f86f..d8ad79db7 100644 --- a/backend/techpick-api/src/main/resources/logback-spring.xml +++ b/backend/techpick-api/src/main/resources/logback-spring.xml @@ -70,6 +70,9 @@ + + + diff --git a/backend/techpick-batch/src/main/java/techpick/TechPickBatchApplication.java b/backend/techpick-batch/src/main/java/techpick/TechPickBatchApplication.java index f7c2a3b32..ab770fbb2 100644 --- a/backend/techpick-batch/src/main/java/techpick/TechPickBatchApplication.java +++ b/backend/techpick-batch/src/main/java/techpick/TechPickBatchApplication.java @@ -4,10 +4,10 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication( - scanBasePackages = {"techpick.batch", "techpick.core"} + // scanBasePackages = {"techpick.batch", "techpick.core"} ) public class TechPickBatchApplication { - public static void main(String[] args) { - SpringApplication.run(TechPickBatchApplication.class, args); - } + public static void main(String[] args) { + SpringApplication.run(TechPickBatchApplication.class, args); + } } \ No newline at end of file diff --git a/backend/techpick-core/src/main/java/techpick/core/rabbitmq/EventMessageAspect.java b/backend/techpick-core/src/main/java/techpick/core/rabbitmq/EventMessageAspect.java index 509448fe2..99bccf7dd 100644 --- a/backend/techpick-core/src/main/java/techpick/core/rabbitmq/EventMessageAspect.java +++ b/backend/techpick-core/src/main/java/techpick/core/rabbitmq/EventMessageAspect.java @@ -9,6 +9,7 @@ import org.springframework.stereotype.Component; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import techpick.core.annotation.TechpickAnnotation; /** @@ -16,6 +17,7 @@ * 컨트롤러의 반환 값을 이용해서 이벤트를 생성하고 전송하는 AOP * sendEvent 어노테이션의 type parameter를 함께 설정합니다. */ +@Slf4j @Aspect @Component @RequiredArgsConstructor @@ -31,9 +33,8 @@ public void sendEvent( ) { var responseBody = response.getBody(); if (Objects.isNull(responseBody)) { - throw new EventProcessingError( - String.format("%s : ResponseBody가 null이여서 이벤트로 변환할 수 없습니다.", jp.getSignature()) - ); + log.error("{} : ResponseBody가 null이여서 이벤트로 변환할 수 없습니다.", jp.getSignature()); + return; } eventSender.sendEvent(responseBody.toEvent(sendEvent.type()/* 생성/조회/삭제/수정 */)); } diff --git a/backend/techpick-core/src/main/java/techpick/core/rabbitmq/RabbitmqConfig.java b/backend/techpick-core/src/main/java/techpick/core/rabbitmq/RabbitmqConfig.java index a0b374d47..cc7d7e39a 100644 --- a/backend/techpick-core/src/main/java/techpick/core/rabbitmq/RabbitmqConfig.java +++ b/backend/techpick-core/src/main/java/techpick/core/rabbitmq/RabbitmqConfig.java @@ -24,6 +24,9 @@ public static final class QUEUE { public static final String Q1 = "queue.event-q1"; } + @Value("${spring.application.name}") + private String appName; + @Value("${spring.rabbitmq.host}") private String host; @@ -67,6 +70,7 @@ Binding directBinding(DirectExchange directExchange, Queue queue1) { @Bean ConnectionFactory connectionFactory() { CachingConnectionFactory connectionFactory = new CachingConnectionFactory(); + connectionFactory.setConnectionNameStrategy(cn -> appName + "-" + cn); connectionFactory.setHost(host); connectionFactory.setPort(port); connectionFactory.setUsername(username); diff --git a/backend/techpick-core/src/main/resources/application-core.yaml b/backend/techpick-core/src/main/resources/application-core.yaml index b6e0b6640..498236b7f 100644 --- a/backend/techpick-core/src/main/resources/application-core.yaml +++ b/backend/techpick-core/src/main/resources/application-core.yaml @@ -1,6 +1,4 @@ spring: - application: - name: techpick-server output: ansi: enabled: always @@ -14,12 +12,6 @@ spring: dialect: org.hibernate.dialect.MySQL8Dialect open-in-view: false - rabbitmq: - host: ${RABBITMQ_HOST} - port: ${RABBITMQ_PORT} - username: ${RABBITMQ_USERNAME} - password: ${RABBITMQ_PASSWORD} - #logging: # /* Transaction Logging */ # level: # org.springframework.orm.jpa: DEBUG @@ -43,6 +35,11 @@ spring: username: ${DOCKER_MYSQL_USERNAME} password: ${DOCKER_MYSQL_PASSWORD} driver-class-name: com.mysql.cj.jdbc.Driver + rabbitmq: + host: ${LOCAL_RABBITMQ_HOST} + port: ${RABBITMQ_PORT} + username: ${RABBITMQ_USERNAME} + password: ${RABBITMQ_PASSWORD} --- @@ -62,6 +59,11 @@ spring: username: ${DOCKER_MYSQL_USERNAME} password: ${DOCKER_MYSQL_PASSWORD} driver-class-name: com.mysql.cj.jdbc.Driver + rabbitmq: + host: ${RABBITMQ_HOST} + port: ${RABBITMQ_PORT} + username: ${RABBITMQ_USERNAME} + password: ${RABBITMQ_PASSWORD} --- @@ -81,5 +83,9 @@ spring: username: ${DOCKER_MYSQL_USERNAME} password: ${DOCKER_MYSQL_PASSWORD} driver-class-name: com.mysql.cj.jdbc.Driver - + rabbitmq: + host: ${RABBITMQ_HOST} + port: ${RABBITMQ_PORT} + username: ${RABBITMQ_USERNAME} + password: ${RABBITMQ_PASSWORD} --- \ No newline at end of file diff --git a/backend/techpick-ranking/build.gradle b/backend/techpick-ranking/build.gradle index 07378eb57..1cb478590 100644 --- a/backend/techpick-ranking/build.gradle +++ b/backend/techpick-ranking/build.gradle @@ -10,6 +10,8 @@ repositories { } dependencies { + implementation project(":techpick-core") + implementation 'org.springframework.boot:spring-boot-starter-data-mongodb' testImplementation platform('org.junit:junit-bom:5.10.0') diff --git a/backend/techpick-ranking/src/main/java/techpick/TechPickRankingApplication.java b/backend/techpick-ranking/src/main/java/techpick/TechPickRankingApplication.java index dcd44eb18..4d4d0fe4e 100644 --- a/backend/techpick-ranking/src/main/java/techpick/TechPickRankingApplication.java +++ b/backend/techpick-ranking/src/main/java/techpick/TechPickRankingApplication.java @@ -6,7 +6,8 @@ import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; @SpringBootApplication( - exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class} + // scanBasePackages = {"techpick.core"} + // exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class} ) public class TechPickRankingApplication { diff --git a/backend/techpick-ranking/src/main/java/techpick/ranking/LinkViewCount.java b/backend/techpick-ranking/src/main/java/techpick/ranking/LinkViewCount.java new file mode 100644 index 000000000..7a1834fb6 --- /dev/null +++ b/backend/techpick-ranking/src/main/java/techpick/ranking/LinkViewCount.java @@ -0,0 +1,33 @@ +package techpick.ranking; + +import java.time.LocalDate; + +import org.springframework.data.mongodb.core.mapping.Document; + +import jakarta.persistence.Id; +import lombok.Getter; + +@Document(collection = "link_view_count") +@Getter +public class LinkViewCount { + + @Id + private String id; + + private final LocalDate date; + + private final String url; + + private Long count; + + public LinkViewCount(LocalDate date, String url, Long count) { + this.date = date; + this.url = url; + this.count = count; + } + + public LinkViewCount incrementCount() { + ++this.count; + return this; + } +} diff --git a/backend/techpick-ranking/src/main/java/techpick/ranking/LinkViewCountRepository.java b/backend/techpick-ranking/src/main/java/techpick/ranking/LinkViewCountRepository.java new file mode 100644 index 000000000..bab3b40a3 --- /dev/null +++ b/backend/techpick-ranking/src/main/java/techpick/ranking/LinkViewCountRepository.java @@ -0,0 +1,26 @@ +package techpick.ranking; + +import java.time.LocalDate; +import java.util.List; +import java.util.Optional; + +import org.springframework.data.domain.Limit; +import org.springframework.data.domain.Sort; +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface LinkViewCountRepository extends MongoRepository { + + Optional findLinkViewCountByDateAndUrl(LocalDate date, String url); + + List findAllByDateOrderByCountDesc(LocalDate date); + + List findByDate(LocalDate date, Sort sort, Limit limit); + + List findByDateBetweenOrderByCountDesc( + LocalDate startDate, + LocalDate endDate, + Limit limit + ); +} diff --git a/backend/techpick-ranking/src/main/java/techpick/ranking/LinkViewRankingService.java b/backend/techpick-ranking/src/main/java/techpick/ranking/LinkViewRankingService.java new file mode 100644 index 000000000..b2ed17de3 --- /dev/null +++ b/backend/techpick-ranking/src/main/java/techpick/ranking/LinkViewRankingService.java @@ -0,0 +1,64 @@ +package techpick.ranking; + +import java.time.LocalDate; +import java.time.Month; +import java.util.ArrayList; +import java.util.List; + +import org.springframework.amqp.rabbit.annotation.RabbitListener; +import org.springframework.data.domain.Limit; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.stereotype.Service; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import techpick.core.rabbitmq.Event; +import techpick.core.rabbitmq.RabbitmqConfig; + +@Slf4j +@Service +@RequiredArgsConstructor +public class LinkViewRankingService { + + private final LinkViewCountRepository linkViewCountRepository; + + @RabbitListener(queues = {RabbitmqConfig.QUEUE.Q1}) + public void readViewEvent(Event event) { + var date = event.getOccuredOn().toLocalDate(); + var url = event.getUrl(); + var linkViewCount = linkViewCountRepository + .findLinkViewCountByDateAndUrl(date, url) + .orElseGet(() -> new LinkViewCount(date, url, 0L)) + .incrementCount(); + linkViewCountRepository.save(linkViewCount); + } + + // DAY-RANK : GET TOP N (= count) + public List getTopViewsByDay(int maxCount) { + var currentDay = LocalDate.now(); + return linkViewCountRepository.findByDate(currentDay, Sort.by(Sort.Direction.DESC, "count"), + Limit.of(maxCount)); + } + + // WEEK-RANK : GET TOP N (= count) + // 오늘 기준 지난 일주일 + public List getTopViewsByPast7Days(int maxCount) { + var currentDay = LocalDate.now(); + return linkViewCountRepository.findByDateBetweenOrderByCountDesc( + currentDay.minusDays(7), + currentDay.plusDays(1), // TODO: GT를 GTE로 바꾸는 방법이 뭘까? + Limit.of(maxCount) + ); + } + + // MONT:-RANK : GET TOP N (= count) + public List getTopViewByPast30Days(int maxCount) { + var currentMonth = LocalDate.now(); + return linkViewCountRepository.findByDateBetweenOrderByCountDesc( + currentMonth.minusDays(30), + currentMonth.plusDays(1), // TODO: GT를 GTE로 바꾸는 방법이 뭘까? + Limit.of(maxCount) + ); + } +} diff --git a/backend/techpick-ranking/src/main/java/techpick/ranking/RankingController.java b/backend/techpick-ranking/src/main/java/techpick/ranking/RankingController.java new file mode 100644 index 000000000..0dacbe6d4 --- /dev/null +++ b/backend/techpick-ranking/src/main/java/techpick/ranking/RankingController.java @@ -0,0 +1,47 @@ +package techpick.ranking; + +import java.time.LocalDate; +import java.util.List; + +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.RequiredArgsConstructor; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/ranking") +@Tag(name = "Rank API", description = "랭킹 API") +public class RankingController { + + // TODO: 시간 나라별 기준 이런거 검토 필요합니다. + private final LinkViewRankingService linkViewRankingService; + + @GetMapping("/daily") + public ResponseEntity getDailyRanking( + @RequestParam int count + ) { + var result = linkViewRankingService.getTopViewsByDay(count); + return ResponseEntity.ok(result); + } + + @GetMapping("/weekly") + public ResponseEntity getWeeklyRanking( + @RequestParam int count + ) { + var result = linkViewRankingService.getTopViewsByPast7Days(count); + return ResponseEntity.ok(result); + } + + @GetMapping("/monthly") + public ResponseEntity getMonthlyRanking( + @RequestParam int count + ) { + var result = linkViewRankingService.getTopViewByPast30Days(count); + return ResponseEntity.ok(result); + } +} diff --git a/backend/techpick-ranking/src/main/java/techpick/ranking/application/ProducerController.java b/backend/techpick-ranking/src/main/java/techpick/ranking/application/ProducerController.java deleted file mode 100644 index 2c5717f56..000000000 --- a/backend/techpick-ranking/src/main/java/techpick/ranking/application/ProducerController.java +++ /dev/null @@ -1,46 +0,0 @@ -package techpick.ranking.application; - -import java.util.List; - -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import techpick.ranking.domain.ProducerService; -import techpick.ranking.dto.MessageDto; -import techpick.ranking.entity.Message; - -@Slf4j -@RestController -@RequestMapping("/api/producer") -@RequiredArgsConstructor -public class ProducerController { - - private final ProducerService producerService; - - @GetMapping - public ResponseEntity> findAllMessage() { - return ResponseEntity.ok(producerService.findMessage()); - } - - @PostMapping("/send/direct") - public ResponseEntity directSendMessage(@RequestBody MessageDto messageDto) { - String result = ""; - - producerService.directSendMessage(messageDto); - return ResponseEntity.ok(result); - } - - @PostMapping("/send/fanout") - public ResponseEntity fanoutSendMessage(@RequestBody MessageDto messageDto) { - String result = ""; - - producerService.fanoutSendMessage(messageDto); - return ResponseEntity.ok(result); - } -} diff --git a/backend/techpick-ranking/src/main/java/techpick/ranking/config/RabbitmqConfig.java b/backend/techpick-ranking/src/main/java/techpick/ranking/config/RabbitmqConfig.java deleted file mode 100644 index 5fe421311..000000000 --- a/backend/techpick-ranking/src/main/java/techpick/ranking/config/RabbitmqConfig.java +++ /dev/null @@ -1,122 +0,0 @@ -package techpick.ranking.config; - -import org.springframework.amqp.core.Binding; -import org.springframework.amqp.core.BindingBuilder; -import org.springframework.amqp.core.DirectExchange; -import org.springframework.amqp.core.FanoutExchange; -import org.springframework.amqp.core.Queue; -import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; -import org.springframework.amqp.rabbit.connection.ConnectionFactory; -import org.springframework.amqp.rabbit.core.RabbitTemplate; -import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; -import org.springframework.amqp.support.converter.MessageConverter; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration -public class RabbitmqConfig { - - @Value("${spring.rabbitmq.host}") - private String host; - - @Value("${spring.rabbitmq.port}") - private int port; - - @Value("${spring.rabbitmq.username}") - private String username; - - @Value("${spring.rabbitmq.password}") - private String password; - - /** - * 1. Exchange 구성 - */ - @Bean - DirectExchange directExchange() { - return new DirectExchange("exchange.direct"); - } - - @Bean - FanoutExchange fanoutExchange() { - return new FanoutExchange("exchange.fanout"); - } - - /** - * 2. 큐 구성 - */ - @Bean - Queue queue1() { - return new Queue("queue1", false); - } - - @Bean - Queue queue2() { - return new Queue("queue2", false); - } - - @Bean - Queue queue3() { - return new Queue("queue3", false); - } - - /** - * 3. 큐와 DirectExchange를 바인딩 - */ - @Bean - Binding directBinding(DirectExchange directExchange, Queue queue1) { - return BindingBuilder - .bind(queue1) - .to(directExchange) - .with("direct.key"); // 라우팅 키 - } - - @Bean - Binding fanoutBinding1(FanoutExchange fanoutExchange, Queue queue2) { - return BindingBuilder - .bind(queue2) - .to(fanoutExchange); - } - - @Bean - Binding fanoutBinding2(FanoutExchange fanoutExchange, Queue queue3) { - return BindingBuilder - .bind(queue3) - .to(fanoutExchange); - } - - /** - * 4. RabbitMQ 연결을 위한 ConnectionFactory 구성 - * application.yaml의 RabbitMQ 사용자 정보를 가져온 후 - * RabbitMQ 연결에 필요한 ConnectionFactory 구성 - */ - @Bean - ConnectionFactory connectionFactory() { - CachingConnectionFactory connectionFactory = new CachingConnectionFactory(); - connectionFactory.setHost(host); - connectionFactory.setPort(port); - connectionFactory.setUsername(username); - connectionFactory.setPassword(password); - return connectionFactory; - } - - /** - * 5. 메시지를 전송하고 수신하기 위한 JSON 타입으로 메시지를 변경 - * Jackson2JsonMessageConverter를 사용하여 메시지 변환을 수행 - * JSON 형식으로 메시지를 전송하고 수신 - */ - @Bean - MessageConverter messageConverter() { - return new Jackson2JsonMessageConverter(); - } - - /** - * 6. 구성한 ConnectionFactory, MessageConverter를 통해 템플릿 구성 - */ - @Bean - RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory, MessageConverter messageConverter) { - RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory); - rabbitTemplate.setMessageConverter(messageConverter); - return rabbitTemplate; - } -} diff --git a/backend/techpick-ranking/src/main/java/techpick/ranking/domain/ProducerService.java b/backend/techpick-ranking/src/main/java/techpick/ranking/domain/ProducerService.java deleted file mode 100644 index 5fffcce0c..000000000 --- a/backend/techpick-ranking/src/main/java/techpick/ranking/domain/ProducerService.java +++ /dev/null @@ -1,64 +0,0 @@ -package techpick.ranking.domain; - -import java.util.List; - -import org.springframework.amqp.rabbit.core.RabbitTemplate; -import org.springframework.stereotype.Service; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; - -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import techpick.ranking.dto.MessageDto; -import techpick.ranking.entity.Message; -import techpick.ranking.entity.PickPick; -import techpick.ranking.repository.MessageRepository; -import techpick.ranking.repository.PickPickRepository; - -@Slf4j -@Service -@RequiredArgsConstructor -public class ProducerService { - - private final RabbitTemplate rabbitTemplate; - private final MessageRepository messageRepository; - private final PickPickRepository pickPickRepository; - - public List findMessage() { - return messageRepository.findAll(); - } - - public void directSendMessage(MessageDto messageDto) { - try { - ObjectMapper objectMapper = new ObjectMapper(); - String objectToJson = objectMapper.writeValueAsString(messageDto); - - rabbitTemplate.convertAndSend("exchange.direct", "direct.key", objectToJson); - - Message message = messageRepository.save( - Message - .builder().title(messageDto.getTitle()).message(messageDto.getMessage()).build()); - pickPickRepository.save(PickPick - .builder() - .url("https://www.naver.com") - .title("제목") - .parentFolderId(1L) - .tagIdOrderedList(List.of(1L, 2L, 3L)) - .build()); - } catch (JsonProcessingException e) { - System.out.println("파싱 오류 발생"); - } - } - - public void fanoutSendMessage(MessageDto messageDto) { - try { - ObjectMapper objectMapper = new ObjectMapper(); - String objectToJson = objectMapper.writeValueAsString(messageDto); - - rabbitTemplate.convertAndSend("exchange.fanout", "", objectToJson); - } catch (JsonProcessingException e) { - System.out.println("파싱 오류 발생"); - } - } -} diff --git a/backend/techpick-ranking/src/main/java/techpick/ranking/domain/messageComponent.java b/backend/techpick-ranking/src/main/java/techpick/ranking/domain/messageComponent.java deleted file mode 100644 index 4b2c073f3..000000000 --- a/backend/techpick-ranking/src/main/java/techpick/ranking/domain/messageComponent.java +++ /dev/null @@ -1,27 +0,0 @@ -package techpick.ranking.domain; - -import org.springframework.amqp.rabbit.annotation.RabbitListener; -import org.springframework.stereotype.Component; - -import lombok.RequiredArgsConstructor; - -@Component -@RequiredArgsConstructor -public class messageComponent { - - @RabbitListener(queues = "queue1") - public void receiveMessage1(String message) { - System.out.println("Queue1 : " + message); - } - - @RabbitListener(queues = "queue2") - public void receiveMessage2(String message) { - System.out.println("Queue2 : " + message); - } - - @RabbitListener(queues = "queue3") - public void receiveMessage3(String message) { - System.out.println("Queue3 : " + message); - } - -} diff --git a/backend/techpick-ranking/src/main/java/techpick/ranking/dto/MessageDto.java b/backend/techpick-ranking/src/main/java/techpick/ranking/dto/MessageDto.java deleted file mode 100644 index a93c8aba6..000000000 --- a/backend/techpick-ranking/src/main/java/techpick/ranking/dto/MessageDto.java +++ /dev/null @@ -1,22 +0,0 @@ -package techpick.ranking.dto; - -import lombok.AccessLevel; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.ToString; - -@Getter -@ToString -@NoArgsConstructor(access = AccessLevel.PROTECTED) -public class MessageDto { - - private String title; - private String message; - - @Builder - public MessageDto(String title, String message) { - this.title = title; - this.message = message; - } -} diff --git a/backend/techpick-ranking/src/main/java/techpick/ranking/entity/Message.java b/backend/techpick-ranking/src/main/java/techpick/ranking/entity/Message.java deleted file mode 100644 index 6a7b7cdf0..000000000 --- a/backend/techpick-ranking/src/main/java/techpick/ranking/entity/Message.java +++ /dev/null @@ -1,26 +0,0 @@ -package techpick.ranking.entity; - -import org.springframework.data.mongodb.core.mapping.Document; - -import jakarta.persistence.Id; -import lombok.AccessLevel; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; - -@Document(collection = "message") -@Getter -@NoArgsConstructor(access = AccessLevel.PROTECTED) -public class Message { - - @Id - private String id; - private String title; - private String message; - - @Builder - private Message(String title, String message) { - this.title = title; - this.message = message; - } -} diff --git a/backend/techpick-ranking/src/main/java/techpick/ranking/entity/PickPick.java b/backend/techpick-ranking/src/main/java/techpick/ranking/entity/PickPick.java deleted file mode 100644 index 6a0298349..000000000 --- a/backend/techpick-ranking/src/main/java/techpick/ranking/entity/PickPick.java +++ /dev/null @@ -1,36 +0,0 @@ -package techpick.ranking.entity; - -import java.util.List; - -import org.springframework.data.mongodb.core.mapping.Document; - -import jakarta.persistence.Id; -import lombok.AccessLevel; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; - -@Document(collection = "pick") -@Getter -@NoArgsConstructor(access = AccessLevel.PROTECTED) -public class PickPick { - - @Id - private String id; - - private String url; - - private String title; - - private Long parentFolderId; - - private List tagIdOrderedList; - - @Builder - private PickPick(String url, String title, Long parentFolderId, List tagIdOrderedList) { - this.url = url; - this.title = title; - this.parentFolderId = parentFolderId; - this.tagIdOrderedList = tagIdOrderedList; - } -} diff --git a/backend/techpick-ranking/src/main/java/techpick/ranking/repository/MessageRepository.java b/backend/techpick-ranking/src/main/java/techpick/ranking/repository/MessageRepository.java deleted file mode 100644 index c3969d9a4..000000000 --- a/backend/techpick-ranking/src/main/java/techpick/ranking/repository/MessageRepository.java +++ /dev/null @@ -1,11 +0,0 @@ -package techpick.ranking.repository; - -import org.springframework.data.mongodb.repository.MongoRepository; -import org.springframework.stereotype.Repository; - -import techpick.ranking.entity.Message; - -@Repository -public interface MessageRepository extends MongoRepository { - -} diff --git a/backend/techpick-ranking/src/main/java/techpick/ranking/repository/PickPickRepository.java b/backend/techpick-ranking/src/main/java/techpick/ranking/repository/PickPickRepository.java deleted file mode 100644 index 7e8f887bb..000000000 --- a/backend/techpick-ranking/src/main/java/techpick/ranking/repository/PickPickRepository.java +++ /dev/null @@ -1,10 +0,0 @@ -package techpick.ranking.repository; - -import org.springframework.data.mongodb.repository.MongoRepository; -import org.springframework.stereotype.Repository; - -import techpick.ranking.entity.PickPick; - -@Repository -public interface PickPickRepository extends MongoRepository { -} diff --git a/backend/techpick-ranking/src/main/java/techpick/ranking/test/EmitLog.java b/backend/techpick-ranking/src/main/java/techpick/ranking/test/EmitLog.java deleted file mode 100644 index 8a515b5d3..000000000 --- a/backend/techpick-ranking/src/main/java/techpick/ranking/test/EmitLog.java +++ /dev/null @@ -1,29 +0,0 @@ -package techpick.ranking.test; - -import java.nio.charset.StandardCharsets; - -import com.rabbitmq.client.Channel; -import com.rabbitmq.client.Connection; -import com.rabbitmq.client.ConnectionFactory; - -public class EmitLog { - - private static final String EXCHANGE_NAME = "logs"; - - public static void main(String[] argv) throws Exception { - ConnectionFactory factory = new ConnectionFactory(); - factory.setHost("localhost"); - factory.setUsername("root"); - factory.setPassword("1234"); - - try (Connection connection = factory.newConnection(); Channel channel = connection.createChannel()) { - channel.exchangeDeclare(EXCHANGE_NAME, "fanout"); - - String message = argv.length < 1 ? "info: Hello World!" : - String.join(" ", argv); - - channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes(StandardCharsets.UTF_8)); - System.out.println(" [x] Sent '" + message + "'"); - } - } -} \ No newline at end of file diff --git a/backend/techpick-ranking/src/main/java/techpick/ranking/test/NewTask.java b/backend/techpick-ranking/src/main/java/techpick/ranking/test/NewTask.java deleted file mode 100644 index deee08bad..000000000 --- a/backend/techpick-ranking/src/main/java/techpick/ranking/test/NewTask.java +++ /dev/null @@ -1,30 +0,0 @@ -package techpick.ranking.test; - -import com.rabbitmq.client.Channel; -import com.rabbitmq.client.Connection; -import com.rabbitmq.client.ConnectionFactory; -import com.rabbitmq.client.MessageProperties; - -public class NewTask { - - private static final String TASK_QUEUE_NAME = "task_queue"; - - public static void main(String[] argv) throws Exception { - ConnectionFactory factory = new ConnectionFactory(); - factory.setHost("localhost"); - factory.setUsername("root"); - factory.setPassword("1234"); - - try (Connection connection = factory.newConnection(); Channel channel = connection.createChannel()) { - channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null); - - String message = String.join(" ", argv); - - channel.basicPublish("", TASK_QUEUE_NAME, - MessageProperties.PERSISTENT_TEXT_PLAIN, - message.getBytes("UTF-8")); - System.out.println(" [x] Sent '" + message + "'"); - } - } - -} diff --git a/backend/techpick-ranking/src/main/java/techpick/ranking/test/ReceiveLogs.java b/backend/techpick-ranking/src/main/java/techpick/ranking/test/ReceiveLogs.java deleted file mode 100644 index b74bbba36..000000000 --- a/backend/techpick-ranking/src/main/java/techpick/ranking/test/ReceiveLogs.java +++ /dev/null @@ -1,32 +0,0 @@ -package techpick.ranking.test; - -import com.rabbitmq.client.Channel; -import com.rabbitmq.client.Connection; -import com.rabbitmq.client.ConnectionFactory; -import com.rabbitmq.client.DeliverCallback; - -public class ReceiveLogs { - private static final String EXCHANGE_NAME = "logs"; - - public static void main(String[] argv) throws Exception { - ConnectionFactory factory = new ConnectionFactory(); - factory.setHost("localhost"); - factory.setUsername("root"); - factory.setPassword("1234"); - Connection connection = factory.newConnection(); - Channel channel = connection.createChannel(); - - channel.exchangeDeclare(EXCHANGE_NAME, "fanout"); - String queueName = channel.queueDeclare().getQueue(); - channel.queueBind(queueName, EXCHANGE_NAME, ""); - - System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); - - DeliverCallback deliverCallback = (consumerTag, delivery) -> { - String message = new String(delivery.getBody(), "UTF-8"); - System.out.println(" [x] Received '" + message + "'"); - }; - channel.basicConsume(queueName, true, deliverCallback, consumerTag -> { - }); - } -} \ No newline at end of file diff --git a/backend/techpick-ranking/src/main/java/techpick/ranking/test/Recv.java b/backend/techpick-ranking/src/main/java/techpick/ranking/test/Recv.java deleted file mode 100644 index f98f3e0cc..000000000 --- a/backend/techpick-ranking/src/main/java/techpick/ranking/test/Recv.java +++ /dev/null @@ -1,31 +0,0 @@ -package techpick.ranking.test; - -import com.rabbitmq.client.Channel; -import com.rabbitmq.client.Connection; -import com.rabbitmq.client.ConnectionFactory; -import com.rabbitmq.client.DeliverCallback; - -public class Recv { - - private final static String QUEUE_NAME = "hello"; - - public static void main(String[] args) throws Exception { - ConnectionFactory factory = new ConnectionFactory(); - factory.setHost("localhost"); - factory.setUsername("root"); - factory.setPassword("1234"); - - Connection connection = factory.newConnection(); - Channel channel = connection.createChannel(); - - channel.queueDeclare(QUEUE_NAME, false, false, false, null); - System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); - - DeliverCallback deliverCallback = (consumerTag, delivery) -> { - String message = new String(delivery.getBody(), "UTF-8"); - System.out.println(" [x] Received '" + message + "'"); - }; - channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> { - }); - } -} diff --git a/backend/techpick-ranking/src/main/java/techpick/ranking/test/Send.java b/backend/techpick-ranking/src/main/java/techpick/ranking/test/Send.java deleted file mode 100644 index 14fd56b9e..000000000 --- a/backend/techpick-ranking/src/main/java/techpick/ranking/test/Send.java +++ /dev/null @@ -1,24 +0,0 @@ -package techpick.ranking.test; - -import com.rabbitmq.client.Channel; -import com.rabbitmq.client.Connection; -import com.rabbitmq.client.ConnectionFactory; - -public class Send { - - private final static String QUEUE_NAME = "hello"; - - public static void main(String[] argv) throws Exception { - ConnectionFactory factory = new ConnectionFactory(); - factory.setHost("localhost"); - factory.setUsername("root"); - factory.setPassword("1234"); - - try (Connection connection = factory.newConnection(); Channel channel = connection.createChannel()) { - channel.queueDeclare(QUEUE_NAME, false, false, false, null); - String message = "Hello World!"; - channel.basicPublish("", QUEUE_NAME, null, message.getBytes()); - System.out.println(" [x] Sent '" + message + "'"); - } - } -} \ No newline at end of file diff --git a/backend/techpick-ranking/src/main/java/techpick/ranking/test/Worker.java b/backend/techpick-ranking/src/main/java/techpick/ranking/test/Worker.java deleted file mode 100644 index c81d89536..000000000 --- a/backend/techpick-ranking/src/main/java/techpick/ranking/test/Worker.java +++ /dev/null @@ -1,50 +0,0 @@ -package techpick.ranking.test; - -import com.rabbitmq.client.Channel; -import com.rabbitmq.client.Connection; -import com.rabbitmq.client.ConnectionFactory; -import com.rabbitmq.client.DeliverCallback; - -public class Worker { - - private static final String TASK_QUEUE_NAME = "task_queue"; - - public static void main(String[] argv) throws Exception { - ConnectionFactory factory = new ConnectionFactory(); - factory.setHost("localhost"); - factory.setUsername("root"); - factory.setPassword("1234"); - final Connection connection = factory.newConnection(); - final Channel channel = connection.createChannel(); - - channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null); - System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); - - channel.basicQos(1); - - DeliverCallback deliverCallback = (consumerTag, delivery) -> { - String message = new String(delivery.getBody(), "UTF-8"); - System.out.println(" [x] Received '" + message + "'"); - try { - doWork(message); - } finally { - System.out.println(" [x] Done"); - channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); - } - }; - channel.basicConsume(TASK_QUEUE_NAME, false, deliverCallback, consumerTag -> { - }); - } - - private static void doWork(String task) { - for (char ch : task.toCharArray()) { - if (ch == '.') { - try { - Thread.sleep(1000); - } catch (InterruptedException _ignored) { - Thread.currentThread().interrupt(); - } - } - } - } -} \ No newline at end of file diff --git a/backend/techpick-ranking/src/main/resources/application.yaml b/backend/techpick-ranking/src/main/resources/application.yaml index 2d2426090..9a90db60b 100644 --- a/backend/techpick-ranking/src/main/resources/application.yaml +++ b/backend/techpick-ranking/src/main/resources/application.yaml @@ -1,8 +1,11 @@ # 로컬에서 techpick-api와 동시에 켜야 할 경우 아래 주석을 해제하세요 -#server: -# port: 8082 +server: + port: 8082 spring: + profiles: + include: + - core application: name: techpick-ranking output: @@ -26,7 +29,7 @@ spring: spring: config: activate: - on-profile: dev + on-profile: dev, prod data: mongodb: uri: mongodb://${DOCKER_MONGO_USERNAME}:${DOCKER_MONGO_PASSWORD}@${DOCKER_MONGO_URL}/${DOCKER_MONGO_DATABASE}?authSource=${DOCKER_MONGO_AUTH} @@ -35,9 +38,3 @@ spring: password: ${DOCKER_MONGO_PASSWORD} --- -spring: - rabbitmq: - host: ${RABBITMQ_HOST} - port: ${RABBITMQ_PORT} - username: ${RABBITMQ_USERNAME} - password: ${RABBITMQ_PASSWORD} \ No newline at end of file diff --git a/backend/techpick-ranking/src/main/resources/logback-spring.xml b/backend/techpick-ranking/src/main/resources/logback-spring.xml index 01bfdf735..86344bbe2 100644 --- a/backend/techpick-ranking/src/main/resources/logback-spring.xml +++ b/backend/techpick-ranking/src/main/resources/logback-spring.xml @@ -2,6 +2,10 @@ + + + +