Skip to main content

Getting started with RabbitMQ

Getting started with RabbitMQ

1. Brief description of asynchronous communication


benefit:

  • Throughput improvement: no need to wait for subscriber processing to complete, faster response
  • Fault isolation: the service is not directly called, and there is no cascading failure problem
  • There is no blocking between calls and no invalid resource usage.
  • The degree of coupling is extremely low, and each service can be flexibly plugged in and replaced.
  • Traffic peak shaving: No matter how much the traffic of published events fluctuates, it will be received by the Broker, and subscribers can process events at their own speed.

shortcoming:

  • The structure is complex, the business has no obvious process lines, and it is difficult to manage.
  • Need to rely on the reliability, security and performance of the Broker

2. Common message models


SpringAMQP is a set of templates based on RabbitMQ encapsulation, and it also uses SpringBoot to implement automatic assembly, which is very convenient to use.

Three functions are provided:

  • Automatically declare queues, switches and their binding relationships
  • Annotation-based listener mode, asynchronously receiving messages
  • Encapsulates the RabbitTemplate tool for sending messages
2.1 SimpleQueue

Add dependencies in pom.xml

    <!--AMQP依赖,包含RabbitMQ-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>


2.1.1 Message sending

First configure the MQ address and configure it in application.yml

    spring:
[rabbitmq](/search?q=rabbitmq):
host: 192.168.150.101 # 主机名
port: 5672 # 端口
virtual-host: / # 虚拟主机
username: itcast # 用户名
password: 123321 # 密码

Write producer code to produce messages

    package cn.itcast.mq.listener;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class SpringRabbitListener {

@RabbitListener(queues = "simple.queue")
public void listenSimpleQueueMessage(String msg) throws InterruptedException {
System.out.println("spring 消费者接收到消息:【" + msg + "】");
}
}

Write consumer code and consume messages

    public class SpringRabbitListener {

@RabbitListener(queues = "simple.queue")
public void listenSimpleQueueMessage(String msg) throws InterruptedException {
System.out.println("spring 消费者接收到消息:【" + msg + "】");
}
}

2.2 Work queue

To put it simply, multiple consumers are bound to a queue and jointly consume messages in the queue .
Getting started with RabbitMQ
When message processing is time-consuming, the speed of message production may be much greater than the speed of message consumption. If things go on like this, more and more messages will accumulate, making it impossible to process them in a timely manner.

At this time, the work model can be used, and multiple consumers can handle message processing together, and the speed can be greatly improved.

Similar to a simple queue, except that one more consumer is bound to the queue. Code omitted.

2.3 Fanout (broadcast queue)

The producer generates messages and sends them to the exchange , and the exchange decides which queue to send the message to.
In broadcast mode, the switch will send it to all queues bound to it. Note here that each message will be sent to all queues bound to the switch, that is, one message will be sent to multiple queues.

    import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FanoutConfig {
/**
* 声明交换机
* @return Fanout类型交换机
*/
@Bean
public FanoutExchange fanoutExchange(){
return new FanoutExchange("it.fanout");
}

/**
* 第1个队列
*/
@Bean
public Queue fanoutQueue1(){
return new Queue("fanout.queue1");
}

/**
* 绑定队列和交换机
*/
@Bean
public Binding bindingQueue1(Queue fanoutQueue1, FanoutExchange fanoutExchange){
return BindingBuilder.bind(fanoutQueue1).to(fanoutExchange);
}

/**
* 第2个队列
*/
@Bean
public Queue fanoutQueue2(){
return new Queue("fanout.queue2");
}

/**
* 绑定队列和交换机
*/
@Bean
public Binding bindingQueue2(Queue fanoutQueue2, FanoutExchange fanoutExchange){
return BindingBuilder.bind(fanoutQueue2).to(fanoutExchange);
}
}


![Getting started with RabbitMQ](6b44e99974d17195ee2722671aabdc1f.png)

Message production and consumption are the same as before.
The difference between routing and broadcasting is that the switch does not send information to the queue brainlessly, but needs to 'secretly' and only the queue will receive the message if the code is correct.
The difference between a topic and a route is that the topic supports wildcards, while the route is a hard-coded password.