도커를 사용하여 RabbitMQ를 설치합니다.
docker run -d --name rabbitmq -p5672:5672 -p 15672:15672 --restart=unless-stopped rabbitmq:management
- localhost:15672에 접속하면 로그인 페이지가 보입니다. guest/guest를 입력하여 접속하면 대시보드를 볼 수 있습니다.
흐름도
Order Application
- build.gradle
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-amqp'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.amqp:spring-rabbit-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
- application.properties
spring.application.name=order
message.exchange=market
message.queue.product=market.product
message.queue.payment=market.payment
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
server.port=8080
- OrderApplicationQueueConfig
package com.market.order;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class OrderApplicationQueueConfig {
@Value("${message.exchange}")
private String exchange;
@Value("${message.queue.product}")
private String queueProduct;
@Value("${message.queue.payment}")
private String queuePayment;
@Bean
public TopicExchange exchange() {
return new TopicExchange(exchange);
}
@Bean
public Queue queueProduct() {
return new Queue(queueProduct);
}
@Bean
public Queue queuePayment() {
return new Queue(queuePayment);
}
@Bean
public Binding bindingProduct() {
return BindingBuilder.bind(queueProduct()).to(exchange()).with(queueProduct);
}
@Bean
public Binding bindingPayment() {
return BindingBuilder.bind(queuePayment()).to(exchange()).with(queuePayment);
}
}
- OrderService.java
package com.market.order;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class OrderService {
@Value("${message.queue.product}")
private String productQueue;
@Value("${message.queue.payment}")
private String paymentQueue;
// RabbitMQ와 상호작용하기 위해 사용하는 RabbitTemplate 객체
private final RabbitTemplate rabbitTemplate;
public void createOrder(String orderId) {
// productQueue 큐에 orderId를 메시지로 전송합니다.
rabbitTemplate.convertAndSend(productQueue, orderId);
// paymentQueue 큐에 orderId를 메시지로 전송합니다.
rabbitTemplate.convertAndSend(paymentQueue, orderId);
}
}
- 애플리케이션을 런하여 /order/1 로 요청을 보냅니다. 그후 http://localhost:15672로 접속하여 RabbitMQ의 Exchange와 Queue 를 확인 할 수 있습니다. 또한 Queue 에서는 현재 발행된 메시지가 Total 에 쌓여 있는것을 확인 할 수 있습니다.
- Queue and Stream 페이지에서 큐 이름을 클릭하여 상세페이지로 이동한 수 스크롤하여 Get Messages섹션으로 가서 Get Message를 클릭하면 현재 큐에 쌓여있는 메시지를 조회 할 수 있습니다.
Payment Application
- build.gradle
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-amqp'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.amqp:spring-rabbit-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
- appication.properties
spring.application.name=payment
message.queue.payment=market.payment
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
server.port=8081
- PaymentEndpoint.java
package com.market.payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class PaymentEndpoint {
@Value("${spring.application.name}")
private String appName;
@RabbitListener(queues = "${message.queue.payment}")
public void receiveMessage(String orderId) {
log.info("received orderId: {}, appName: {}", orderId, appName);
}
}
'대규모 시스템' 카테고리의 다른 글
SAGA Pattern (0) | 2024.08.14 |
---|---|
Kafka 기본 설정 (0) | 2024.08.14 |
Kafka란? (0) | 2024.08.14 |
RabbitMQ란? (0) | 2024.08.14 |
대규모 시스템이란? (0) | 2024.08.14 |