728x90
1. docker로 Rabbit MQ 설치
컨테이너 이름 : rabbitmq
백그라운드로 실행
docker run -d --name rabbitmq -p 5672:5672 -p 8080:15672 --restart=unless-stopped rabbitmq:management
2. management url 접속
# default username, password는 둘다 guest로 입력
http://localhost:15672/
3. admin, vhost 추가 (옵션)
4. Queue, Exchange 생성
queue : SKT, KT, LG,emailbroker1, emailbroker2라는 이름의 총 5개의 큐 생성
exchange : SKT, KT, LG 3개의 큐는 각자의 exchage 생성 / emailbroker1, emailbroker2는 동일한 1개의 exchange 생성후 바인딩
5. Build.gradle dependency 추가
implementation 'org.springframework.boot:spring-boot-starter-amqp'
testImplementation 'org.springframework.amqp:spring-rabbit-test'
6. application.yml 수정
spring:
rabbitmq:
host: localhost
port: 5672
# username: guest
# password: guest
username: gutsandgun # 3. admin, vhost 추가한 경우
password: gutsandgun # 3. admin, vhost 추가한 경우
virtual-host: gutsandgun # 3. admin, vhost 추가한 경우
rabbitmq:
api: # rabbit MQ API 호출을 위해 작성
port: 5672
queue: /api/queues
queue-detail: /api/queues/
queue: # queue 연동을 위해 작성
queue1:
name: SKT
exchange: SKT-exchange
queue2:
name: KT
exchange: KT-exchange
queue3:
name: LG
exchange: LG-exchange
queue4:
name: emailbroker1
exchange: email-exchange
queue5:
name: emailbroker2
exchange: email-exchange
routing:
key: # exchange
queue1: SKT
queue2: KT
queue3: LG
queue5: broker1
queue6: broker2
7. rabbitmqConfing 파일 생성
- EnableRabbit 어노테이션 추가
- yml에 작성한 값 변수 선언(queue name, exchange, routing key)
@Configuration
@EnableRabbit
public class RabbitmqConfig {
@Value("${spring.rabbitmq.host}")
private String host;
@Value("${spring.rabbitmq.virtual-host}")
private String vhost;
@Value("${spring.rabbitmq.port}")
private int port;
@Value("${spring.rabbitmq.username}")
private String username;
@Value("${spring.rabbitmq.password}")
private String password;
}
1. 큐 선언
// queue name
@Value("${rabbitmq.queue1.name}")
private String queue1;
@Value("${rabbitmq.queue2.name}")
private String queue2;
@Value("${rabbitmq.queue3.name}")
private String queue3;
@Value("${rabbitmq.queue4.name}")
private String emailQueue1;
@Value("${rabbitmq.queue5.name}")
private String emailQueue2;
@Bean
Queue queue1() {
return new Queue(queue1, true);
}
@Bean
Queue queue2() {
return new Queue(queue2, true);
}
@Bean
Queue queue3() {
return new Queue(queue3, true);
}
2. exchange 선언
// exchange
@Value("${rabbitmq.queue1.exchange}")
private String exchange1;
@Value("${rabbitmq.queue2.exchange}")
private String exchange2;
@Value("${rabbitmq.queue3.exchange}")
private String exchange3;
@Value("${rabbitmq.queue4.exchange}")
private String emailExchange;
@Bean
DirectExchange directExchange1() {
return new DirectExchange(exchange1);
}
@Bean
DirectExchange directExchange2() {
return new DirectExchange(exchange2);
}
@Bean
DirectExchange directExchange3() {
return new DirectExchange(exchange3);
}
@Bean
DirectExchange emailDirectExchange1() {
return new DirectExchange(emailExchange);
}
3. 라우팅 키 선언
@Value("${rabbitmq.routing.key.queue1}")
private String routingKey1;
@Value("${rabbitmq.routing.key.queue2}")
private String routingKey2;
@Value("${rabbitmq.routing.key.queue3}")
private String routingKey3;
@Value("${rabbitmq.routing.key.queue4}")
private String emailRoutingKey1;
@Value("${rabbitmq.routing.key.queue5}")
private String emailRoutingKey2;
4. 바인딩
@Bean
Binding binding1() {
return BindingBuilder.bind(queue1()).to(directExchange1()).with(routingKey1);
}
@Bean
Binding binding2() {
return BindingBuilder.bind(queue2()).to(directExchange2()).with(routingKey2);
}
@Bean
Binding binding3() {
return BindingBuilder.bind(queue3()).to(directExchange3()).with(routingKey3);
}
@Bean
Binding emailBinding1() {
return BindingBuilder.bind(emailQueue1()).to(emailDirectExchange1()).with(emailRoutingKey1);
}
@Bean
Binding emailBinding2() {
return BindingBuilder.bind(emailQueue2()).to(emailDirectExchange1()).with(emailRoutingKey2);
}
5. 컨버터 및 rabbitTemplate 선언
@Bean
MessageConverter messageConverter() {
return new Jackson2JsonMessageConverter();
}
@Bean
RabbitTemplate rabbitTemplate
(ConnectionFactory connectionFactory, MessageConverter messageConverter) {
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
rabbitTemplate.setMessageConverter(messageConverter);
return rabbitTemplate;
}
@Bean
ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setHost(host);
connectionFactory.setPort(port);
connectionFactory.setVirtualHost(vhost);
connectionFactory.setUsername(username);
connectionFactory.setPassword(password);
return connectionFactory;
}
더보기
// queue name
@Configuration
@EnableRabbit
public class RabbitmqConfig {
@Value("${spring.rabbitmq.host}")
private String host;
@Value("${spring.rabbitmq.virtual-host}")
private String vhost;
@Value("${spring.rabbitmq.port}")
private int port;
@Value("${spring.rabbitmq.username}")
private String username;
@Value("${spring.rabbitmq.password}")
private String password;
// queue name
@Value("${rabbitmq.queue1.name}")
private String queue1;
@Value("${rabbitmq.queue2.name}")
private String queue2;
@Value("${rabbitmq.queue3.name}")
private String queue3;
@Value("${rabbitmq.queue4.name}")
private String emailQueue1;
@Value("${rabbitmq.queue5.name}")
private String emailQueue2;
// exchange
@Value("${rabbitmq.queue1.exchange}")
private String exchange1;
@Value("${rabbitmq.queue2.exchange}")
private String exchange2;
@Value("${rabbitmq.queue3.exchange}")
private String exchange3;
@Value("${rabbitmq.queue4.exchange}")
private String emailExchange;
@Value("${rabbitmq.routing.key.queue1}")
private String routingKey1;
@Value("${rabbitmq.routing.key.queue2}")
private String routingKey2;
@Value("${rabbitmq.routing.key.queue3}")
private String routingKey3;
@Value("${rabbitmq.routing.key.queue4}")
private String emailRoutingKey1;
@Value("${rabbitmq.routing.key.queue5}")
private String emailRoutingKey2;
@Bean
Queue queue1() {
return new Queue(queue1, true);
}
@Bean
Queue queue2() {
return new Queue(queue2, true);
}
@Bean
Queue queue3() {
return new Queue(queue3, true);
}
@Bean
Queue emailQueue1() {
return new Queue(emailQueue1, true);
}
@Bean
Queue emailQueue2() {
return new Queue(emailQueue2, true);
}
@Bean
DirectExchange directExchange1() {
return new DirectExchange(exchange1);
}
@Bean
DirectExchange directExchange2() {
return new DirectExchange(exchange2);
}
@Bean
DirectExchange directExchange3() {
return new DirectExchange(exchange3);
}
@Bean
DirectExchange emailDirectExchange1() {
return new DirectExchange(emailExchange);
}
@Bean
Binding binding1() {
return BindingBuilder.bind(queue1()).to(directExchange1()).with(routingKey1);
}
@Bean
Binding binding2() {
return BindingBuilder.bind(queue2()).to(directExchange2()).with(routingKey2);
}
@Bean
Binding binding3() {
return BindingBuilder.bind(queue3()).to(directExchange3()).with(routingKey3);
}
@Bean
Binding emailBinding1() {
return BindingBuilder.bind(emailQueue1()).to(emailDirectExchange1()).with(emailRoutingKey1);
}
@Bean
Binding emailBinding2() {
return BindingBuilder.bind(emailQueue2()).to(emailDirectExchange1()).with(emailRoutingKey2);
}
@Bean
MessageConverter messageConverter() {
return new Jackson2JsonMessageConverter();
}
@Bean
RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory, MessageConverter messageConverter) {
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
rabbitTemplate.setMessageConverter(messageConverter);
return rabbitTemplate;
}
@Bean
ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setHost(host);
connectionFactory.setPort(port);
connectionFactory.setVirtualHost(vhost);
connectionFactory.setUsername(username);
connectionFactory.setPassword(password);
return connectionFactory;
}
}
728x90