RabbitMQ - AMQP를 따르는 오픈소스 Message Broker

728x90
반응형
SMALL

RabbitMQ

AMQP를 따르는 오픈소스 Message Broker

사용

Gradle

implementation 'org.springframework.boot:spring-boot-starter-amqp'

 

Consumer

spring-boot-starter-amqp에서 기본적인 커넥션을 연동해주기 때문에 Properties 설정만 추가하면 된다.

spring:
    rabbitmq:
        host: IP
        port: 5672
        username: UserName
        password: UserPassword

 

Configuration

@Configuration
public class RabbitConfig {
     
    private static final String EXCHANGE_NAME = "jeonghoonb.topic";
    private static final String QUEUE_NAME = "jeonghoonb.queue";
    private static final String ROUTING_KEY = "jeonghoonb.key#";
 
    @Bean
    Queue queue() {
        return new Queue(QUEUE_NAME, false);
    }
  
    @Bean
    TopicExchange exchange() {
        return new TopicExchange(EXCHANGE_NAME);
    }

    @Bean
    Binding binding(Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY);
    }
 
    @Bean
    RabbitTemplate rabbitTemplate(ConncetionFactory connectionFactory, MessageConverter messageConverter) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(messageConverter);
        return rabbitTemplate;
    }

    @Bean
    MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }
}

 

Listener

@Component
public class MessageListener {
     
    @RabbitListener(queue = "jeonghoonb.queue")
    public void receiveMEssage(final Message message) {
        System.out.println(message);
    }
}

 

Producer

@RestController
@RequiredArgsConstructor
public class MessageController {
     
    private final RabbitTemplate rabbitTemplate;
    private static final String EXCHANGE_NAME = "jeonghoonb.topic";
  
    @GetMapping("/default")
    public String testMessagePub() {
        rabbitTemplate.converAndSend(EXCHANGE_NAME, "jeonghoonb.key.first", "Message");
        return "ok";
    }
}
728x90
반응형
LIST

'Develope > ELK' 카테고리의 다른 글

[Kafka] 아파치 카프카 (Apache Kafka)란?  (0) 2020.03.17