From 779ca93f95e4aff9482da0ad3064a51be05cd391 Mon Sep 17 00:00:00 2001 From: rumos Date: Fri, 9 Aug 2024 23:43:53 +0900 Subject: [PATCH 01/12] =?UTF-8?q?[chore]=20rabbit=20mq=20build=20gradle=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=20(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 3 +++ 1 file changed, 3 insertions(+) diff --git a/build.gradle b/build.gradle index c2432e4..f7582a0 100644 --- a/build.gradle +++ b/build.gradle @@ -57,6 +57,9 @@ dependencies { // Local Cache implementation 'org.springframework.boot:spring-boot-starter-cache' + + // Rabbit MQ + implementation 'org.springframework.boot:spring-boot-starter-amqp' } tasks.named('test') { From f2428995287228e69ca0db025731b8c64a853bcc Mon Sep 17 00:00:00 2001 From: rumos Date: Fri, 9 Aug 2024 23:44:22 +0900 Subject: [PATCH 02/12] =?UTF-8?q?[feat]=20rabbit=20mq=20properties=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1=20(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/config/RabbitMqProperties.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/main/java/gdsc/cau/puangbe/auth/config/RabbitMqProperties.java diff --git a/src/main/java/gdsc/cau/puangbe/auth/config/RabbitMqProperties.java b/src/main/java/gdsc/cau/puangbe/auth/config/RabbitMqProperties.java new file mode 100644 index 0000000..d3a4500 --- /dev/null +++ b/src/main/java/gdsc/cau/puangbe/auth/config/RabbitMqProperties.java @@ -0,0 +1,20 @@ +package gdsc.cau.puangbe.auth.config; + + +import lombok.AllArgsConstructor; +import lombok.Getter; +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * spring.rabbitmq 의 prefix 을 가지는 값들을 + * RabbitMqProperties 클래스 필드로 바인딩 한 후 사용 + */ +@ConfigurationProperties(prefix = "spring.rabbitmq") +@AllArgsConstructor +@Getter +public class RabbitMqProperties { + private String host; + private int port; + private String username; + private String password; +} From 1692bc9267fbb1d99ef9b34bb64a702dd0f8840b Mon Sep 17 00:00:00 2001 From: rumos Date: Fri, 9 Aug 2024 23:45:11 +0900 Subject: [PATCH 03/12] =?UTF-8?q?[feat]=20rabbit=20mq=20config=20=EC=84=A4?= =?UTF-8?q?=EC=A0=95=20(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../puangbe/common/config/RabbitMqConfig.java | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 src/main/java/gdsc/cau/puangbe/common/config/RabbitMqConfig.java diff --git a/src/main/java/gdsc/cau/puangbe/common/config/RabbitMqConfig.java b/src/main/java/gdsc/cau/puangbe/common/config/RabbitMqConfig.java new file mode 100644 index 0000000..6d0063b --- /dev/null +++ b/src/main/java/gdsc/cau/puangbe/common/config/RabbitMqConfig.java @@ -0,0 +1,94 @@ +package gdsc.cau.puangbe.common.config; + +import gdsc.cau.puangbe.auth.config.RabbitMqProperties; +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.amqp.core.Binding; +import org.springframework.amqp.core.BindingBuilder; +import org.springframework.amqp.core.DirectExchange; +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.amqp.core.Queue; + +@RequiredArgsConstructor +@Configuration +public class RabbitMqConfig { + private final RabbitMqProperties rabbitMqProperties; + + /** + * Exchange : Producer로부터 전달받은 메시지를 어떤 메시지 큐로 전송할 지 결정하는 장소 + */ + @Value("${rabbitmq.exchange.name}") + private String exchangeName; + + /** + * Routing : Exchange에서 해당하는 key에 맞게 Queue에 분배 + */ + @Value("${rabbitmq.routing.key}") + private String routingKey; + + /** + * Queue : Consumer가 소비하기 전까지 메시지가 보관되는 장소 + */ + @Value("${rabbitmq.queue.name}") + private String queueName; + + @Bean + public Queue queue() { + return new Queue(queueName); + } + + /** + * 지정된 Exchange 이름으로 Direct Exchange Bean 을 생성 + */ + @Bean + public DirectExchange directExchange() { + return new DirectExchange(exchangeName); + } + + /** + * 주어진 Queue 와 Exchange 을 Binding 하고 Routing Key 을 이용하여 Binding Bean 생성 + * Exchange 에 Queue 을 등록한다고 이해하자 + **/ + @Bean + public Binding binding(Queue queue, DirectExchange exchange) { + return BindingBuilder.bind(queue).to(exchange).with(routingKey); + } + + /** + * RabbitMQ 연동을 위한 ConnectionFactory 빈을 생성하여 반환 + **/ + @Bean + public CachingConnectionFactory connectionFactory() { + CachingConnectionFactory connectionFactory = new CachingConnectionFactory(); + connectionFactory.setHost(rabbitMqProperties.getHost()); + connectionFactory.setPort(rabbitMqProperties.getPort()); + connectionFactory.setUsername(rabbitMqProperties.getUsername()); + connectionFactory.setPassword(rabbitMqProperties.getPassword()); + return connectionFactory; + } + + /** + * RabbitTemplate + * ConnectionFactory 로 연결 후 실제 작업을 위한 Template + */ + @Bean + public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) { + RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory); + rabbitTemplate.setMessageConverter(jackson2JsonMessageConverter()); + return rabbitTemplate; + } + + /** + * 직렬화(메세지를 JSON 으로 변환하는 Message Converter) + */ + @Bean + public MessageConverter jackson2JsonMessageConverter() { + return new Jackson2JsonMessageConverter(); + } +} \ No newline at end of file From a9058d6b1e4777a24a4782def931c0fc41bc8573 Mon Sep 17 00:00:00 2001 From: rumos Date: Fri, 9 Aug 2024 23:49:15 +0900 Subject: [PATCH 04/12] =?UTF-8?q?[refactor]=20rabbit=20mq=20config?= =?UTF-8?q?=EB=A5=BC=20photorequest=EB=A1=9C=20=EC=9D=B4=EA=B4=80=20(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{common => photorequest}/config/RabbitMqConfig.java | 3 +-- .../{auth => photorequest}/config/RabbitMqProperties.java | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) rename src/main/java/gdsc/cau/puangbe/{common => photorequest}/config/RabbitMqConfig.java (97%) rename src/main/java/gdsc/cau/puangbe/{auth => photorequest}/config/RabbitMqProperties.java (91%) diff --git a/src/main/java/gdsc/cau/puangbe/common/config/RabbitMqConfig.java b/src/main/java/gdsc/cau/puangbe/photorequest/config/RabbitMqConfig.java similarity index 97% rename from src/main/java/gdsc/cau/puangbe/common/config/RabbitMqConfig.java rename to src/main/java/gdsc/cau/puangbe/photorequest/config/RabbitMqConfig.java index 6d0063b..0093244 100644 --- a/src/main/java/gdsc/cau/puangbe/common/config/RabbitMqConfig.java +++ b/src/main/java/gdsc/cau/puangbe/photorequest/config/RabbitMqConfig.java @@ -1,6 +1,5 @@ -package gdsc.cau.puangbe.common.config; +package gdsc.cau.puangbe.photorequest.config; -import gdsc.cau.puangbe.auth.config.RabbitMqProperties; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; diff --git a/src/main/java/gdsc/cau/puangbe/auth/config/RabbitMqProperties.java b/src/main/java/gdsc/cau/puangbe/photorequest/config/RabbitMqProperties.java similarity index 91% rename from src/main/java/gdsc/cau/puangbe/auth/config/RabbitMqProperties.java rename to src/main/java/gdsc/cau/puangbe/photorequest/config/RabbitMqProperties.java index d3a4500..5268428 100644 --- a/src/main/java/gdsc/cau/puangbe/auth/config/RabbitMqProperties.java +++ b/src/main/java/gdsc/cau/puangbe/photorequest/config/RabbitMqProperties.java @@ -1,4 +1,4 @@ -package gdsc.cau.puangbe.auth.config; +package gdsc.cau.puangbe.photorequest.config; import lombok.AllArgsConstructor; From 005ab40118a85f909f8c3ba22fae475bd8c5caf4 Mon Sep 17 00:00:00 2001 From: rumos Date: Sat, 10 Aug 2024 00:08:16 +0900 Subject: [PATCH 05/12] =?UTF-8?q?[feat]=20rabbit=20mq=20service=20interfac?= =?UTF-8?q?e,=20sendMessage=20=EA=B5=AC=ED=98=84=20(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../photorequest/service/RabbitMqService.java | 7 ++++ .../service/RabbitMqServiceImpl.java | 38 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/main/java/gdsc/cau/puangbe/photorequest/service/RabbitMqService.java create mode 100644 src/main/java/gdsc/cau/puangbe/photorequest/service/RabbitMqServiceImpl.java diff --git a/src/main/java/gdsc/cau/puangbe/photorequest/service/RabbitMqService.java b/src/main/java/gdsc/cau/puangbe/photorequest/service/RabbitMqService.java new file mode 100644 index 0000000..1c3c147 --- /dev/null +++ b/src/main/java/gdsc/cau/puangbe/photorequest/service/RabbitMqService.java @@ -0,0 +1,7 @@ +package gdsc.cau.puangbe.photorequest.service; + +import gdsc.cau.puangbe.photorequest.dto.ImageInfo; + +public interface RabbitMqService { + public void sendMessage(String message); +} diff --git a/src/main/java/gdsc/cau/puangbe/photorequest/service/RabbitMqServiceImpl.java b/src/main/java/gdsc/cau/puangbe/photorequest/service/RabbitMqServiceImpl.java new file mode 100644 index 0000000..0ac63b7 --- /dev/null +++ b/src/main/java/gdsc/cau/puangbe/photorequest/service/RabbitMqServiceImpl.java @@ -0,0 +1,38 @@ +package gdsc.cau.puangbe.photorequest.service; + +import gdsc.cau.puangbe.photorequest.dto.ImageInfo; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +@Slf4j +public class RabbitMqServiceImpl implements RabbitMqService{ + + @Value("${rabbitmq.queue.name}") + private String queueName; + + @Value("${rabbitmq.exchange.name}") + private String exchangeName; + + @Value("${rabbitmq.routing.key}") + private String routingKey; + + private final RabbitTemplate rabbitTemplate; + + /** + * 1. Queue 로 메세지를 발행 + * 2. Producer 역할 -> Direct Exchange (메시지의 routing key와 정확히 일치하는 binding된 Queue로 routing) + **/ + public void sendMessage(String message) { + log.info("**Message Send**: {}",message); + log.info("messagge queue: {}", queueName); + log.info("messagge exchange: {}", exchangeName); + log.info("messagge routingKey: {}", routingKey); + this.rabbitTemplate.convertAndSend(exchangeName, routingKey, message); + } + +} From 3d1445b2a4dd9200f9af2f0b1c46bf421cdc3907 Mon Sep 17 00:00:00 2001 From: rumos Date: Sat, 10 Aug 2024 23:13:36 +0900 Subject: [PATCH 06/12] =?UTF-8?q?[refactor]=20rabbit=20mq=20config=20commo?= =?UTF-8?q?n=20=EC=9D=B4=EA=B4=80=20(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config => common/config/RabbitMq}/RabbitMqConfig.java | 2 +- .../config => common/config/RabbitMq}/RabbitMqProperties.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename src/main/java/gdsc/cau/puangbe/{photorequest/config => common/config/RabbitMq}/RabbitMqConfig.java (98%) rename src/main/java/gdsc/cau/puangbe/{photorequest/config => common/config/RabbitMq}/RabbitMqProperties.java (90%) diff --git a/src/main/java/gdsc/cau/puangbe/photorequest/config/RabbitMqConfig.java b/src/main/java/gdsc/cau/puangbe/common/config/RabbitMq/RabbitMqConfig.java similarity index 98% rename from src/main/java/gdsc/cau/puangbe/photorequest/config/RabbitMqConfig.java rename to src/main/java/gdsc/cau/puangbe/common/config/RabbitMq/RabbitMqConfig.java index 0093244..038a438 100644 --- a/src/main/java/gdsc/cau/puangbe/photorequest/config/RabbitMqConfig.java +++ b/src/main/java/gdsc/cau/puangbe/common/config/RabbitMq/RabbitMqConfig.java @@ -1,4 +1,4 @@ -package gdsc.cau.puangbe.photorequest.config; +package gdsc.cau.puangbe.common.config.RabbitMq; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Value; diff --git a/src/main/java/gdsc/cau/puangbe/photorequest/config/RabbitMqProperties.java b/src/main/java/gdsc/cau/puangbe/common/config/RabbitMq/RabbitMqProperties.java similarity index 90% rename from src/main/java/gdsc/cau/puangbe/photorequest/config/RabbitMqProperties.java rename to src/main/java/gdsc/cau/puangbe/common/config/RabbitMq/RabbitMqProperties.java index 5268428..2dd2dc5 100644 --- a/src/main/java/gdsc/cau/puangbe/photorequest/config/RabbitMqProperties.java +++ b/src/main/java/gdsc/cau/puangbe/common/config/RabbitMq/RabbitMqProperties.java @@ -1,4 +1,4 @@ -package gdsc.cau.puangbe.photorequest.config; +package gdsc.cau.puangbe.common.config.RabbitMq; import lombok.AllArgsConstructor; From 8f497d13f85163fae09d3151ece535ba20a1d93e Mon Sep 17 00:00:00 2001 From: rumos Date: Mon, 12 Aug 2024 23:02:07 +0900 Subject: [PATCH 07/12] =?UTF-8?q?[feat]=20message=20queue=20=EC=A0=80?= =?UTF-8?q?=EC=9E=A5=EC=9D=84=20=EC=9C=84=ED=95=9C=20rabbitmq=20sendmessag?= =?UTF-8?q?e=20=ED=98=B8=EC=B6=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../puangbe/photorequest/service/PhotoRequestServiceImpl.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/gdsc/cau/puangbe/photorequest/service/PhotoRequestServiceImpl.java b/src/main/java/gdsc/cau/puangbe/photorequest/service/PhotoRequestServiceImpl.java index 917e94f..0f6123c 100644 --- a/src/main/java/gdsc/cau/puangbe/photorequest/service/PhotoRequestServiceImpl.java +++ b/src/main/java/gdsc/cau/puangbe/photorequest/service/PhotoRequestServiceImpl.java @@ -36,6 +36,7 @@ public class PhotoRequestServiceImpl implements PhotoRequestService { private final UserRepository userRepository; private final RedisTemplate redisTemplate; private final ObjectMapper mapper; + private final RabbitMqService rabbitMqService; //이미지 처리 요청 생성 (RabbitMQ호출) @Override @@ -67,8 +68,7 @@ public void createImage(CreateImageDto dto, Long userId){ .build(); String message = mapper.writeValueAsString(imageInfo); - // TODO: RabbitMQ 호출 - // 1. RabbitMQ를 호출해서 message를 큐에 함께 넣어서 파이썬에서 접근할 수 있도록 한다. + rabbitMqService.sendMessage(message); // 1. RabbitMQ를 호출해서 message를 큐에 함께 넣어서 파이썬에서 접근할 수 있도록 한다. // 2. Redis에 형식으로 진행되고 있는 request 정보를 저장한다. // 3. 추후 사진이 완성된다면 requestId를 통해 request를 찾아서 상태를 바꾸고 1:1 관계인 result에 접근해서 imageUrl를 수정한다. // 4. 즉, 파이썬에서 스프링으로 향하는 POST API는 {requestId, imageUrl}이 필수적으로 존재해야 한다. From 02e379bd90b7cd08d369b75c9735ed0cfa35c662 Mon Sep 17 00:00:00 2001 From: rumos Date: Tue, 13 Aug 2024 22:48:37 +0900 Subject: [PATCH 08/12] =?UTF-8?q?[refactor]=20rabbit=20template=EC=99=80?= =?UTF-8?q?=20log=20=EC=B6=9C=EB=A0=A5=EC=9D=98=20=EC=88=9C=EC=84=9C=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 --- .../cau/puangbe/photorequest/service/RabbitMqServiceImpl.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/gdsc/cau/puangbe/photorequest/service/RabbitMqServiceImpl.java b/src/main/java/gdsc/cau/puangbe/photorequest/service/RabbitMqServiceImpl.java index 0ac63b7..e435fe5 100644 --- a/src/main/java/gdsc/cau/puangbe/photorequest/service/RabbitMqServiceImpl.java +++ b/src/main/java/gdsc/cau/puangbe/photorequest/service/RabbitMqServiceImpl.java @@ -28,11 +28,10 @@ public class RabbitMqServiceImpl implements RabbitMqService{ * 2. Producer 역할 -> Direct Exchange (메시지의 routing key와 정확히 일치하는 binding된 Queue로 routing) **/ public void sendMessage(String message) { + this.rabbitTemplate.convertAndSend(exchangeName, routingKey, message); log.info("**Message Send**: {}",message); log.info("messagge queue: {}", queueName); log.info("messagge exchange: {}", exchangeName); log.info("messagge routingKey: {}", routingKey); - this.rabbitTemplate.convertAndSend(exchangeName, routingKey, message); } - } From 9e25c6cd5a9e4c47197651cbc7fcae19e7f708d1 Mon Sep 17 00:00:00 2001 From: rumos Date: Tue, 13 Aug 2024 23:03:45 +0900 Subject: [PATCH 09/12] =?UTF-8?q?[refactor]=20prefix=20rabbitmq=20?= =?UTF-8?q?=EB=B3=80=EC=88=98=EB=93=A4=EC=9D=84=20RabbitMqInfo=EB=A1=9C=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/config/RabbitMq/RabbitMqInfo.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/main/java/gdsc/cau/puangbe/common/config/RabbitMq/RabbitMqInfo.java diff --git a/src/main/java/gdsc/cau/puangbe/common/config/RabbitMq/RabbitMqInfo.java b/src/main/java/gdsc/cau/puangbe/common/config/RabbitMq/RabbitMqInfo.java new file mode 100644 index 0000000..6211141 --- /dev/null +++ b/src/main/java/gdsc/cau/puangbe/common/config/RabbitMq/RabbitMqInfo.java @@ -0,0 +1,26 @@ +package gdsc.cau.puangbe.common.config.RabbitMq; + +import lombok.Getter; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +@Getter +@Configuration +@ConfigurationProperties(prefix = "rabbitmq") +public class RabbitMqInfo { + + /** + * Exchange : Producer로부터 전달받은 메시지를 어떤 메시지 큐로 전송할 지 결정 + */ + private String exchangeName; + + /** + * Routing : Exchange에서 해당하는 key에 맞게 Queue에 분배 + */ + private String routingKey; + + /** + * Queue : Consumer가 소비하기 전까지 메시지 보관 + */ + private String queueName; +} From 0bdb68ea14e5e8b90d779795673f5fe1e2f26b08 Mon Sep 17 00:00:00 2001 From: rumos Date: Tue, 13 Aug 2024 23:04:28 +0900 Subject: [PATCH 10/12] =?UTF-8?q?[refactor]=20RabbitMqConfig=EC=97=90?= =?UTF-8?q?=EC=84=9C=20RabbitMqInfo=20get=ED=95=98=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=20/=20=EC=A3=BC=EC=84=9D=20=EC=A0=95?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/RabbitMq/RabbitMqConfig.java | 33 +++++-------------- 1 file changed, 8 insertions(+), 25 deletions(-) diff --git a/src/main/java/gdsc/cau/puangbe/common/config/RabbitMq/RabbitMqConfig.java b/src/main/java/gdsc/cau/puangbe/common/config/RabbitMq/RabbitMqConfig.java index 038a438..1a581ea 100644 --- a/src/main/java/gdsc/cau/puangbe/common/config/RabbitMq/RabbitMqConfig.java +++ b/src/main/java/gdsc/cau/puangbe/common/config/RabbitMq/RabbitMqConfig.java @@ -18,45 +18,28 @@ @Configuration public class RabbitMqConfig { private final RabbitMqProperties rabbitMqProperties; - - /** - * Exchange : Producer로부터 전달받은 메시지를 어떤 메시지 큐로 전송할 지 결정하는 장소 - */ - @Value("${rabbitmq.exchange.name}") - private String exchangeName; - - /** - * Routing : Exchange에서 해당하는 key에 맞게 Queue에 분배 - */ - @Value("${rabbitmq.routing.key}") - private String routingKey; - - /** - * Queue : Consumer가 소비하기 전까지 메시지가 보관되는 장소 - */ - @Value("${rabbitmq.queue.name}") - private String queueName; + private final RabbitMqInfo rabbitMqInfo; @Bean public Queue queue() { - return new Queue(queueName); + return new Queue(rabbitMqInfo.getQueueName()); } /** - * 지정된 Exchange 이름으로 Direct Exchange Bean 을 생성 + * 지정된 Exchange 이름으로 Direct Exchange Bean 생성 */ @Bean public DirectExchange directExchange() { - return new DirectExchange(exchangeName); + return new DirectExchange(rabbitMqInfo.getExchangeName()); } /** - * 주어진 Queue 와 Exchange 을 Binding 하고 Routing Key 을 이용하여 Binding Bean 생성 - * Exchange 에 Queue 을 등록한다고 이해하자 + * 주어진 Queue와 Exchange Binding + * Routing Key 을 이용하여 Binding Bean 생성 **/ @Bean public Binding binding(Queue queue, DirectExchange exchange) { - return BindingBuilder.bind(queue).to(exchange).with(routingKey); + return BindingBuilder.bind(queue).to(exchange).with(rabbitMqInfo.getQueueName()); } /** @@ -84,7 +67,7 @@ public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) { } /** - * 직렬화(메세지를 JSON 으로 변환하는 Message Converter) + * 직렬화 (메세지를 JSON 으로 변환하는 Message Converter) */ @Bean public MessageConverter jackson2JsonMessageConverter() { From 1bec202efcda698c02f2cdc53087747671efcf27 Mon Sep 17 00:00:00 2001 From: rumos Date: Tue, 13 Aug 2024 23:21:16 +0900 Subject: [PATCH 11/12] =?UTF-8?q?[refactor]=20rabbimq=20prefix=20Value=20?= =?UTF-8?q?=EC=A0=95=EC=9D=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cau/puangbe/common/config/RabbitMq/RabbitMqConfig.java | 1 - .../gdsc/cau/puangbe/common/config/RabbitMq/RabbitMqInfo.java | 4 ++++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/gdsc/cau/puangbe/common/config/RabbitMq/RabbitMqConfig.java b/src/main/java/gdsc/cau/puangbe/common/config/RabbitMq/RabbitMqConfig.java index 1a581ea..87d9690 100644 --- a/src/main/java/gdsc/cau/puangbe/common/config/RabbitMq/RabbitMqConfig.java +++ b/src/main/java/gdsc/cau/puangbe/common/config/RabbitMq/RabbitMqConfig.java @@ -1,7 +1,6 @@ package gdsc.cau.puangbe.common.config.RabbitMq; import lombok.RequiredArgsConstructor; -import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.amqp.core.Binding; diff --git a/src/main/java/gdsc/cau/puangbe/common/config/RabbitMq/RabbitMqInfo.java b/src/main/java/gdsc/cau/puangbe/common/config/RabbitMq/RabbitMqInfo.java index 6211141..0045c77 100644 --- a/src/main/java/gdsc/cau/puangbe/common/config/RabbitMq/RabbitMqInfo.java +++ b/src/main/java/gdsc/cau/puangbe/common/config/RabbitMq/RabbitMqInfo.java @@ -1,6 +1,7 @@ package gdsc.cau.puangbe.common.config.RabbitMq; import lombok.Getter; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; @@ -12,15 +13,18 @@ public class RabbitMqInfo { /** * Exchange : Producer로부터 전달받은 메시지를 어떤 메시지 큐로 전송할 지 결정 */ + @Value("${rabbitmq.exchange.name}") private String exchangeName; /** * Routing : Exchange에서 해당하는 key에 맞게 Queue에 분배 */ + @Value("${rabbitmq.routing.key}") private String routingKey; /** * Queue : Consumer가 소비하기 전까지 메시지 보관 */ + @Value("${rabbitmq.queue.name}") private String queueName; } From 8aca07b2feee3bc5b7f225cc5fe486de2de6d7c7 Mon Sep 17 00:00:00 2001 From: rumos Date: Tue, 13 Aug 2024 23:28:39 +0900 Subject: [PATCH 12/12] =?UTF-8?q?[refactor]=20RabbitMqServiceImpl=EC=97=90?= =?UTF-8?q?=EC=84=9C=20RabbitMqInfo=20get?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/RabbitMqServiceImpl.java | 23 ++++++------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/src/main/java/gdsc/cau/puangbe/photorequest/service/RabbitMqServiceImpl.java b/src/main/java/gdsc/cau/puangbe/photorequest/service/RabbitMqServiceImpl.java index e435fe5..148dd1d 100644 --- a/src/main/java/gdsc/cau/puangbe/photorequest/service/RabbitMqServiceImpl.java +++ b/src/main/java/gdsc/cau/puangbe/photorequest/service/RabbitMqServiceImpl.java @@ -1,10 +1,9 @@ package gdsc.cau.puangbe.photorequest.service; -import gdsc.cau.puangbe.photorequest.dto.ImageInfo; +import gdsc.cau.puangbe.common.config.RabbitMq.RabbitMqInfo; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.rabbit.core.RabbitTemplate; -import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @Service @@ -12,26 +11,18 @@ @Slf4j public class RabbitMqServiceImpl implements RabbitMqService{ - @Value("${rabbitmq.queue.name}") - private String queueName; - - @Value("${rabbitmq.exchange.name}") - private String exchangeName; - - @Value("${rabbitmq.routing.key}") - private String routingKey; - private final RabbitTemplate rabbitTemplate; + private final RabbitMqInfo rabbitMqInfo; /** * 1. Queue 로 메세지를 발행 * 2. Producer 역할 -> Direct Exchange (메시지의 routing key와 정확히 일치하는 binding된 Queue로 routing) **/ public void sendMessage(String message) { - this.rabbitTemplate.convertAndSend(exchangeName, routingKey, message); - log.info("**Message Send**: {}",message); - log.info("messagge queue: {}", queueName); - log.info("messagge exchange: {}", exchangeName); - log.info("messagge routingKey: {}", routingKey); + this.rabbitTemplate.convertAndSend(rabbitMqInfo.getExchangeName(), rabbitMqInfo.getRoutingKey(), message); + log.info("**Message Send**: {}", message); + log.info("messagge queue: {}", rabbitMqInfo.getQueueName()); + log.info("messagge exchange: {}", rabbitMqInfo.getExchangeName()); + log.info("messagge routingKey: {}", rabbitMqInfo.getRoutingKey()); } }