SpringBoot integrates RabbitMQ series This article mainly organizes the brief introduction of RabbitMQ, installation, configuration, using rabbitMQ to send messages, several types of switches, consumption messages, message callback mechanism, manual confirmation mechanism, and I will follow up after I learn and practice dead letter related knowledge Do other articles on RabbitMQ.
This article mainly organizes the brief introduction of RabbitMQ, installation, configuration, using rabbitMQ to send messages, several types of switches, consumption messages, message callback mechanism, manual confirmation mechanism, and I will do other things about RabbitMQ after I learn and practice dead letter related knowledge article.
foreword
Originally, RabbitMQ was just a simple understanding before, but later a broken function was uploaded in PDF, which was nothing to upload, and I had to transfer pictures. At this time, I was wondering why I didn’t just upload pictures directly. That makabaka stuffy.... . Speaking of business,,, the result is really slow anyway, and then introduce the asynchronous processing of RabbitMQ, and then take this opportunity to learn about MQ, this article is used as a colleague to understand MQ, and I like to use the article1. What is RabbitMQ?
RabbitMQ is an open source message broker software (also known as message-oriented middleware) that implements the Advanced Message Queuing Protocol (AMQP). The RabbitMQ server is written in the Erlang language, while the clustering and failover are built on the Open Telecom Platform framework. All major programming languages have client libraries for communicating with broker interfaces. (Baidu Encyclopedia)
2. Use steps
1. Install
1.1 windows
It is more troublesome to download and directly paste the address of the online disk
Link: https://pan.baidu.com/s/1RvBRkI8RpOkxdlDBYZiyrA
Extraction code: 46i3
version is as follows
Then there are a bunch of installation methods on the Internet
1.2 mac
mac can use docker image or brew installation
2 build
After installing RabbitMQ, open the webpage and enter http://localhost:15672 to access the login page. The default account and password are both guest
and then we can create users, switches, queues, bindings, etc. on this page. To put it bluntly , When writing a program, it is equivalent to using the program to create and bind these things.
2.1 Warm-up phase (pom, xml)
It is also relatively simple to use Rabbit. You only need to put it in the pom file. Of course, you can define the version yourself. Mine is to comply with the 2.1.4 stable version used by SpringCloud.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
The yml configuration is also relatively simple. Here we only write the configuration that can link to rabbitmq. The subsequent message callback and the yml configuration of the manual confirmation mechanism will be supplemented later.
spring:
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
2.2 just litter coder stage
Enter the actual operation of the code below. The relationship between provider and customer can be that A produces B and consumes, B produces A and consumes, and self-produces and sells. Both A and B here refer to a single service of the distributed SOA architecture.
2.2.1 Switch Type
These types of switches are briefly drawn for direct-connected switches (DIRECT), topic switches (TOPIC), and fan-shaped switches (FONOUT).
2.2.1.1 direct
take a break and write again
2.2.1.2 topic
take a break and write again
2.2.1.3 fonout
take a break and write again
2.2.2 Message callback mechanism
take a break and write again
2.2.3 Automatic Confirmation and Manual Confirmation
take a break and write again
2.2.3.1 Automatic confirmation
take a break and write again
2.2.3.2 Manual confirmation
spring:
rabbitmq:
host: 172.0.0.1
port: 5672
username: guest
password: guest
publisher-confirms: true #开启发送确认
publisher-returns: true #开启发送失败回退
listener:
direct:
acknowledge-mode: manual #采取手动应答
simple:
acknowledge-mode: manual
retry:
enabled: true # 是否支持重试
package com.newclass.config.mq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Author : DC
**/
@Slf4j
@Configuration
public class RabbitConfig {
@Bean
public RabbitTemplate createRabbitTemplate(ConnectionFactory connectionFactory){
RabbitTemplate rabbitTemplate = new RabbitTemplate();
rabbitTemplate.setConnectionFactory(connectionFactory);
//设置开启Mandatory,才能触发回调函数,无论消息推送结果怎么样都强制调用回调函数
rabbitTemplate.setMandatory(true);
rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
//①消息推送到server,但是在server里找不到交换机
//消息推送到sever,交换机和队列啥都没找到
@Override
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
if(ack==true){
log.info("ConfirmCallback 确认情况,MQ消息发送 :"+ack);
}else {
log.error("ConfirmCallback 确认情况,MQ消息未发送 :"+ack);
log.error("ConfirmCallback 原因 :"+cause);
}
}
});
rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
//消息推送到server,找到交换机了,但是没找到队列
@Override
public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
log.warn("ReturnCallback: 消息:{}",message);
log.warn("ReturnCallback: 回应码:{}",replyCode);
log.warn("ReturnCallback: 回应信息:{}",replyText);
log.warn("ReturnCallback: 交换机:{}",exchange);
log.warn("ReturnCallback: 路由键:{}",routingKey);
}
});
return rabbitTemplate;
}
}