This is the second day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021

preface

The first two articles have introduced RabbitMQ infrastructure and the basic use of RabbitMQ, which are necessary to learn the basics of MQ and to use in our projects. In this article, we will introduce how to integrate RabbitMQ with Springboot. Messages are sent to other microservices through this middleware.

I. Establishment of producer services

1.1. Establish springboot father-son project.

Add a RabbitMQ producer service to xiaolei-server, which is the parent project of RabbitMq-producer. This gives the code structure. Middleware – RabbitMQ is a late SDK for RabbitMQ that packages methods and configurations for later use.

1.2. Configure the application.yml file

Spring: rabbitMQ: username: test password: test virtual-host: test host: 192.168.81.102 port: 5672Copy the code

1.3. Import dependencies into your project

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
Copy the code

1.4. Define producers and send messages

Producers, we continue to send the case of the movie described in the previous article.

@RestController @RequestMapping("/firm") public class SendFirmMsgController { @Autowired private RabbitTemplate rabbitTemplate; Private String exchangeName = "exchange_firm"; Private String routeKey1 = "private String routeKey1 =" Wu "; Private String routeKey2 = "patriotic. Shen Teng "; Private String routeKey3 = "Action. Wu "; Private String routeKey4 = "Comedy. Shen Teng "; @PostMapping("/send") public void sendMsg(){ for (int i = 1; i <=40; I++) {// @params1: switch exchange // @params2: queue name /routing // @params3: property configuration // @params4: Send the message content if (I % 4 = = 0) {rabbitTemplate. ConvertAndSend (exchangeName routeKey1, (" patriotic. Wu Jing, say "+ I +" times. ).getBytes()); } else if (I % 4 = = 1) {rabbitTemplate. ConvertAndSend (exchangeName routeKey2, (" patriotic. Shen Teng, say "+ I +" again. ).getBytes()); } else if (I % 4 = = 2) {rabbitTemplate. ConvertAndSend (exchangeName routeKey3, (" action. Wu Jing, say "+ I +" times. ).getBytes()); } else if (I % 4 = = 3) {rabbitTemplate. ConvertAndSend (exchangeName routeKey4, (" comedy. Shen Teng, say "+ I +" again. ).getBytes()); } system.out. println(" send "+ I); }}}Copy the code

1.5 Initializing the relationship between queues and switches

package com.xiaolei.rabbitmq.config; 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.context.annotation.Bean; import org.springframework.context.annotation.Configuration; * @author xiaolei * @date 2021/10/29 11:03 **/ @configuration public class TopicRabbitConfig {/** * * @return */ @bean public Queue firstQueue() {// durable: Persistent (false by default), durable Queue: Stored on disk and still exists when message broker restarts, temporary Queue: Current connection valid // EXCLUSIVE: The default is false and can only be used by the currently created connection, and the queue is deleted when the connection is closed. It has a higher priority than the durable // autoDelete: Indicates whether the queue is automatically deleted. When no producer or consumer uses the queue, the queue is automatically deleted. // return new Queue("TestDirectQueue",true,true,false); Return new Queue ("queue1",true); } @Bean public Queue SecondQueue() { return new Queue ("queue2",true); } @Bean public Queue ThreeQueue() { return new Queue ("queue3",true); } @Bean public Queue FourQueue() { return new Queue ("queue4",true); } @bean public TopicExchange TopicExchange (){return new TopicExchange("exchange_firm",true,false); } @Bean public Binding bindingTopic1(){ return BindingBuilder.bind(firstQueue()).to(topicExchange()).with("*. Wujing "); } @bean public Binding bindingTopic2(){return bindingBuilder.bind (SecondQueue()).to(topicExchange()).with(" Wujing "); } @bean public Binding bindingTopic3(){return bindingBuilder.bind (ThreeQueue()).to(topicExchange()).with(" *"); } @Bean public Binding bindingTopic4(){ return BindingBuilder.bind(FourQueue()).to(topicExchange()).with("#. Shen Teng "); }}Copy the code

1.6 Invoking the Postman Interface Test

A new switch and topCI information exists on RabbitMQ

2. Consumer service construction

2.1 Importing Dependencies

We already imported this process in the parent project, so we can omit it here.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
Copy the code

2.2 application. Yml configuration

Spring: rabbitMQ: username: test password: test virtual-host: test host: 192.168.81.102 port: 5672Copy the code

2.3 Define four consumers

@restController public class MsgController {@rabbitListener (Bindings = @queuebinding (// Specify Queue name value = @queue (value =) "queue1",autoDelete = "false"), // Specify the switch name exchange = @exchange (value = "exchange_firm",type = Exchangetypes.topic)) @rabbithandler public void Consumrmsg1 (String MSG) {System. Out. Println (" wu fan -- -- -- -- -- -- -- -- -- -- -- -- -- -- > "+ MSG); } @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "queue2",autoDelete = "false"),exchange = @Exchange(value = "exchange_firm",type = ExchangeTypes.TOPIC))) @RabbitHandler public void consumrmsg2(String msg){ System. The out. Println (" patriotic wu fan -- -- -- -- -- -- -- -- -- -- -- -- -- -- > "+ MSG); } @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "queue3",autoDelete = "false"),exchange = @Exchange(value = "exchange_firm",type = ExchangeTypes.TOPIC))) @RabbitHandler public void consumrmsg3(String msg){ System.out.println(" -------------->" + MSG); } @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "queue4",autoDelete = "false"),exchange = @Exchange(value = "exchange_firm",type = ExchangeTypes.TOPIC))) @RabbitHandler public void consumrmsg4(String msg){ System.out.println(" -------------->" + MSG); }}Copy the code

The printing effect is as follows:

2.4 Details

The consumer class defines a method as a message listener with the @RabbitListener and @Rabbithandler annotations.

The other types are similar, so we just need to configure the corresponding classes ourselves. The basic configuration is complete.

Third, summary

This article introduces the integration of RabbitMq with Springboot. This is a preliminary application, but we will consider other issues and explore RabbitMq features in future articles.