Spring Boot + RabbitMQ

Spring Boot + RabbitMQ

2021, Sep 27    

Hướng dẫn tích hợp RabbitMQ trong Spring Boot application

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>
</dependencies>    
  • Thêm configuration sử dụng rabbitmq trong file application.yml
spring:
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: guest
    password: guest
  • Tạo các bean Exchange, QueueBinding để bind Queue với Exchange
@Configuration
public class RabbitConfiguration {

  public String queueName = "GREETING_QUEUE";
  public String exchangeName = "GREETING_EXCHANGE";
  public String routingKey = "greet.*";

  @Bean
  Queue queue() {
    return new Queue(queueName, false);
  }

  @Bean
  TopicExchange exchange() {
    return new TopicExchange(exchangeName);
  }

  @Bean
  Binding binding(Queue queue, TopicExchange exchange) {
    return BindingBuilder.bind(queue).to(exchange).with(routingKey);
  }

}
  • Tạo producer bằng cách sử dụng RabbitTemplate để send message đến RabbitMQ
@Component
public class GreetingRabbitProviderImpl implements GreetingRabbitProvider {

  @Autowired
  RabbitConfiguration rabbitConfiguration;

  @Autowired
  RabbitTemplate rabbitTemplate;

  @Override
  public void sendGreeting(Greeting greeting) {
    rabbitTemplate.convertAndSend(
        rabbitConfiguration.exchangeName,
        rabbitConfiguration.routingKey,
        GsonUtils.toJson(greeting));
  }
}
  • Sử dụng annotation RabbitListener để tạo consumer/listener
@Slf4j
@Component
public class GreetingRabbitListener {

  @RabbitListener(queues = "#{rabbitConfiguration.queueName}")
  public void greet(String greetingMessage) {
    log.info("Listen message from Rabbit: {}", greetingMessage);

    Greeting greeting = GsonUtils.fromJson(greetingMessage, Greeting.class);
    log.info("Message id={}, message={}", greeting.getId(), greeting.getMessage());
  }

}

Source code ở đây