Whole process dry goods! RabbitMQ and SpringBoot simple integration details

catalogue

  • to configure

  • Realize producers and consumers

  • producer

  • consumer

  • test

  • summary

RabbitMQ is a very popular message middleware, which is widely used by both Internet manufacturers and small and medium-sized enterprises. The rise of Spring Boot greatly simplifies the development of Spring. This paper will simply integrate Spring Boot and RabbitMQ to realize production and consumption messages.

to configure

Spring Boot has been used for so long that the routine is almost clear. Integrating Spring Boot with other components is nothing more than adding pom dependencies, then configuring some basic information, and then developing with relevant annotations.

RabbitMQ is the same routine. The first step is to introduce dependencies. The dependencies to be introduced are easy to remember. RabbitMQ implements AMQP protocol and introduces spring boot starter AMQP.

           <!-- rabbitmq rely on -->
           <dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-amqp</artifactId>
           </dependency>

The second step is to configure RabbitMQ connection information, including host, port number, user name and password. RabbitMQ configuration information:

spring.rabbitmq.host=192.168.16.128
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

If RabbitMQ is not installed, I recommend using Docker for quick installation and startup. The startup command is:

docker run -d --hostname my-rabbit -p 5672:5672 -p 15672:15672 rabbitmq:3.8.0-beta.4-management

Realize producers and consumers

The third step is to realize production and consumers.

producer

Producers use it to produce messages and send them. RabbitTemplate is required. RabbitTemplate is a key class for sending messages. The convertAndSend method can specify the switch, routing key and message content for sending messages.

@Component
public class Producer {
    @Autowired
    RabbitTemplate rabbitTemplate;

    public void produce() {
        String message =  new Date() + "Beijing";
        System.out.println("Producer production news=====" + message);
        rabbitTemplate.convertAndSend("rabbitmq_queue",  message);
    }
}

consumer

Consumers consume messages sent by producers. The annotation @ RabbitListener is mainly used by the implementation consumer@ RabbitListener is a powerful annotation. In this annotation, you can annotate and configure @ QueueBinding, @ Queue, @ Exchange, and directly use this combined annotation to handle multiple switches, bindings, routes, and configure monitoring functions at one time.

  1. Create a queue in the RabbitMQ control panel and use @ RabbitListener to listen to the queue.
@RabbitListener(queues = "rabbitmq_queue")
  1. Use @ RabbitListener to automatically create queues.
@RabbitListener(queuesToDeclare = @Queue("myQueue"))
  1. Use @ RabbitListener to automatically create a Queue and bind Exchange and Queue.
@RabbitListener(bindings = @QueueBinding(value =  @Queue("myQueue"), key = "mobi", exchange = @Exchange("myExchange")))

This article uses @ RabbitListener to automatically create a queue.

@Component
public class Consumer {
    @RabbitHandler
    @RabbitListener(queuesToDeclare = @Queue("rabbitmq_queue"))
    public void process(String message) {
        System.out.println("Consumer News=====" + message);
    }
}

test

Step 4 test. For convenience, write a test class production message. Then start the project and run the test class to make the producer send messages. No accident, consumers will consume messages and output information on the console.

@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {

    @Autowired
    Producer producer;

    @Test
    public void contextLoads() {
        producer.produce();
    }

}

After the operation, you can see that the producer message is sent successfully on the test console and the message consumption is successful on the console of the project.

Visit the RabbitMQ control panel and you will also see a message.

summary

last

I also prepared a systematic learning package for architects and BAT interview materials for your reference and learning, You can get it here for free

The knowledge system has been sorted out (source code, notes, PPT, learning video) for free.

(2) free of charge.

[external chain picture transferring... (img-N4GeCUSl-1623615385117)]

[external chain picture transferring... (IMG vtrgcylj-1623615385119)]

[external chain picture transferring... (IMG uleumwxd-1623615385120)]

Keywords: Java Interview Programmer

Added by chrischen on Wed, 02 Feb 2022 16:51:09 +0200