Spring/Springboot
[Springboot] RabbitMQ 적용하기
늘이
2024. 4. 9. 23:59
1. 의존성 추가
// build.gradle
implementation 'org.springframework.boot:spring-boot-starter-amqp'
2. application.properties
// application.properties
# RabbitMQ
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.queue.name=queue
spring.rabbitmq.exchange.name=exchange
spring.rabbitmq.routing.key=routingkey
3. configuration 파일 작성
package com.douzone.projecttemplate.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
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
@Slf4j
public class RabbitMQConfig {
/* MQTT 서버 정보 설정*/
@Value("${spring.rabbitmq.host}")
private String rabbitmqHost;
@Value("${spring.rabbitmq.port}")
private int rabbitmqPort;
@Value("${spring.rabbitmq.username}")
private String rabbitmqUsername;
@Value("${spring.rabbitmq.password}")
private String rabbitmqPassword;
/* RabbitMQ 큐, 익스체인지, 라우팅 키 설정 */
@Value("${spring.rabbitmq.queue.name}")
private String queueName;
@Value("${spring.rabbitmq.exchange.name}")
private String exchangeName;
@Value("${spring.rabbitmq.routing.key}")
private String routingKey;
/* Queue: Consumer 가 메시지를 소비하기 전 메시지 보관소 */
@Bean
public Queue queue() {
return new Queue(queueName);
}
/* Exchange: Producer 로부터 전달받은 메시지를 어떤 Queue 로 전달할지 결정하는 장소 */
@Bean
public DirectExchange exchange() {
return new DirectExchange(exchangeName);
}
/* Binding: Queue 와 Exchange 관계 정의 */
@Bean
public Binding binding(Queue queue, DirectExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(routingKey);
}
/* RabbitMQ 연결 설정 */
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setHost(rabbitmqHost);
connectionFactory.setPort(rabbitmqPort);
connectionFactory.setUsername(rabbitmqUsername);
connectionFactory.setPassword(rabbitmqPassword);
return connectionFactory;
}
/* 연결 설정으로 연결 후 실제 작업을 위한 RabbitTemplate */
@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
// JSON 형식의 메시지를 직렬화하고 역직렬할 수 있도록 설정
rabbitTemplate.setMessageConverter(jackson2JsonMessageConverter());
return rabbitTemplate;
}
/* 메시지를 JSON 기반으로 변환하는 메시지 컨버터 */
@Bean
public MessageConverter jackson2JsonMessageConverter() {
return new Jackson2JsonMessageConverter();
}
}
4. 메시지 발행하기
// service layer에 구현
/* RabbitMQ publish */
public void sendMessages(String routingKey, Object message) {
rabbitTemplate.convertAndSend(finManagerExchange, routingKey, message);
log.info("RabbitMQ 메시지 발행 성공! routingKey: {}, message: {}", routingKey, message);
}
5. 메시지 구독하기
/* RabbitMQ consumer */
@RabbitListener(queues = "queue이름")
public void receiveMessage(String message) {
log.info("Received message: {}", message);
}