-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[feat] Spring RabbitMQ 설정 및 PhotoRequest Producer 생성 (#22)
- Loading branch information
Showing
7 changed files
with
164 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/main/java/gdsc/cau/puangbe/common/config/RabbitMq/RabbitMqConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package gdsc.cau.puangbe.common.config.RabbitMq; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
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; | ||
private final RabbitMqInfo rabbitMqInfo; | ||
|
||
@Bean | ||
public Queue queue() { | ||
return new Queue(rabbitMqInfo.getQueueName()); | ||
} | ||
|
||
/** | ||
* 지정된 Exchange 이름으로 Direct Exchange Bean 생성 | ||
*/ | ||
@Bean | ||
public DirectExchange directExchange() { | ||
return new DirectExchange(rabbitMqInfo.getExchangeName()); | ||
} | ||
|
||
/** | ||
* 주어진 Queue와 Exchange Binding | ||
* Routing Key 을 이용하여 Binding Bean 생성 | ||
**/ | ||
@Bean | ||
public Binding binding(Queue queue, DirectExchange exchange) { | ||
return BindingBuilder.bind(queue).to(exchange).with(rabbitMqInfo.getQueueName()); | ||
} | ||
|
||
/** | ||
* 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(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/gdsc/cau/puangbe/common/config/RabbitMq/RabbitMqInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
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; | ||
|
||
@Getter | ||
@Configuration | ||
@ConfigurationProperties(prefix = "rabbitmq") | ||
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; | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/gdsc/cau/puangbe/common/config/RabbitMq/RabbitMqProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package gdsc.cau.puangbe.common.config.RabbitMq; | ||
|
||
|
||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/java/gdsc/cau/puangbe/photorequest/service/RabbitMqService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/gdsc/cau/puangbe/photorequest/service/RabbitMqServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package gdsc.cau.puangbe.photorequest.service; | ||
|
||
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.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class RabbitMqServiceImpl implements RabbitMqService{ | ||
|
||
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(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()); | ||
} | ||
} |