20 rabbitmq

1 simple application

1 Producer

1.1.1 Add dependency

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

1.1.2yml file add configuration

  rabbitmq:
    addresses: 127.0.0.1
    port: 5672
    username: guest
    password: guest

1.1.3 Use

Insert picture description here

1.1.4 Use in the project

Insert picture description here

1.2consumer

1.2.1 Add dependency

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

1.2.2yml file add configuration

  rabbitmq:
    addresses: 127.0.0.1
    port: 5672
    username: guest
    password: guest

1.2.3 Use in the project to add Listener

Insert picture description here

@Component
@RabbitListener(queues = "payNotify")
@Slf4j
public class PayMsgListener {
    
    

	@Autowired
	private IOrderService orderService;

	@RabbitHandler
	public void process(String msg) {
    
    
		log.info("【接收到消息】=> {}", msg);

		PayInfo payInfo = new Gson().fromJson(msg, PayInfo.class);
		if (payInfo.getPlatformStatus().equals("SUCCESS")) {
    
    
			//修改订单里的状态
			orderService.paid(payInfo.getOrderNo());
		}
	}
}

1.3 Use

Both the producer and the consumer are started. As long as the producer sends a message, the consumer will listen.

=====================

2 Complex applications

2.1 Confirm, returnCallbake (how to ensure the reliable delivery of messages: producer, consumer)

Whether it is delivered to the exchange correctly: After the confirm
message has been delivered to the exchange, check whether it is correctly routed to the queue. If not received, returnCallbake will be executed

2.1.1Confirm

When the producer sends data to RabbitMQ, the data may be lost halfway, because of network problems or anything, it is possible.
If you have not yet arrived at the big exchange, if you want to make sure that you don’t lose the message writing RabbitMQ, you can turn on the confirm mode. After you set the confirm mode on the producer, every message you write will be assigned a unique id, and then if you write it In RabbitMQ, RabbitMQ will send you an ack message back to tell you that the message is ok. If RabbitMQ fails to process the message, it will call back one of your nack interfaces to tell you that the message has failed to be received, and you can try again.

2.1.2Return:

• Our message producer, by specifying an Exchange and Routingkey, sends the message to a certain queue, and then our consumer listens to the queue for consumption processing operations!
• But in some cases, if the current exchange does not exist or the specified routing key cannot be routed when we send a message, at this time, if we need to listen for such unreachable messages, we must use Return Listener to
do something Follow-up processing, for example, record the message log, or track the record in time, it is possible to reset it.
Insert picture description here

Insert picture description here
Insert picture description here

2.2 Eventual consistency of messages

2.2.1 The design of the database table:

Insert picture description here

2.2.2confirm,return

Insert picture description here
Insert picture description here

Insert picture description here

2.3 Compensation mechanism:

Insert picture description here
Insert picture description here

2.4 Retry after consumption failure

Insert picture description here
The consumer monitors the data in the queue and completes the consumer's consumption.

When an exception occurs to the consumer: a bug may recur, a conversion exception occurs, etc., it will continue to try again. Even if you keep trying again, you can't consume the message. One cycle goes on

A limited number of attempts is enough. Sometimes, because of the jitter of the network, he is allowed to retry a few times, and after a few times, he still fails. Even if we try again, it may not be successful. So we configure the configuration
to retry once every 3 seconds, a total of three times. After three retries, there is no connection, so we will not try. Although the performance problem has been solved and no repeated attempts are made, we discarded the message. Unsuccessful consumption is not allowed in many businesses. ----" To solve the dead letter queue, put these repeated consumption messages in the dead letter queue. You can manually intervene and wait for the news to be consumed again
Insert picture description here

2.5 Dead letter queue: same as normal queue.

Insert picture description here
Insert picture description here

2.6 Idempotence (how to ensure that messages cannot be repeatedly consumed)

After the message was sent, no confirmation of the message was received, which caused it to be sent again. That is, if the same message is sent twice, the consumption will be consumed twice. If the problem of repeated consumption
or the consumption of the consumer fails, it may try again, which can also cause idempotent problems. Originally, one order should be created. Due to multiple delivery or repeated consumption to generate two orders, it must be wrong.
Use red is to solve the problem of repeated consumption.
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/Insist___/article/details/109162913