Core concepts
The important components are described below:
Broker: The Message Queuing service process, which consists of two parts: Exchange and Queue. (Post Office / Express)
Exchange: A message queue switch that routes messages to a queue according to certain rules and cares about messages.
(courier in post office/courier in express company)
Queue: A message queue, a queue that stores messages, arrives in the queue and forwards them to a specified consumer. (Recipient's mailbox/rookie post)
Producer: The message producer, the producer client, sends the message to the MQ. (Sender/Shipper)
Consumer: Message consumers, that is, consumer clients, receive messages forwarded by MQ. (Recipient/consignee)
-----Send a message-----
1. Maker and Broker establish TCP connections.
2. Producers and Broker set up channels.
3. Producers send messages to Broker via channel, which Exchange forwards.
4. Exchange forwards messages to a specified Queue
--Receive messages--
1. Consumer and Broker establish TCP connection
2. Consumers and Broker Establish Channels
3. Consumer listens on a specified Queue
4. Broker pushes messages to consumers by default when they reach Queue.
5. Consumers receive messages.
Workflow
Producers send data via RabbitMQ-client to exchange in RabbitMQ-server, which is distributed to Queue based on routing configuration, and consumers get data from Queue
Generation of distributed transactions
Multiple systems work together to create data consistency problems.
For example, in a takeaway scenario, the order center and the shipping bill center must ensure data consistency between the two systems to work together.
The wrong solution:
Use API interface calls, insert data into the next single center, call API interface of shipping bill center to process data, and start transaction rollback.
At first glance, there is no problem with this scenario. After all, there are transaction rollbacks and we both succeeded in failing together, but there is actually a timeout of API calls. At this time, the order center thinks that the call failed to rollback, while the bill of lading center just timeout will continue to execute the program, resulting in inconsistency between the two system data.
Assuming the API call succeeds, it may also fail when the order center commits a transaction, when the order center rolls back, the API has been called, the data from the order center has been generated, and the data is inconsistent.
Solving distributed transactions using message queues
The core of the problem is to ensure reliable production and consumption
Reliable production: The next single center should handle data and status table changes to ensure transaction consistency. When a producer sends data to a message queue, a status table is created locally to see if it is successfully sent to the queue. Use the confirmation mechanism of RabbitMQ to see whether to resend or periodically scan the status table to ensure reliable production. Doing so still periodically scans the status table.
Code
/** * Single Processing Queue */ public static final String QUEUE_NAME_TRANSACTION = "xucheng.distribute.queue"; /** * Single Handling Switch */ public static final String EXCHANGE_NAME_TRANSACTION = "xucheng.distribute.exchange"; /** * Message Queuing Service process, which consists of two parts: Exchange and Queue. */ public static final String ROUTE_NAME_TRANSACTION = "xucheng.distribute.route"; /** * Replenishment Queue */ //public static final String CREATE_QUEUE_NAME_TRANSACTION = "xucheng.order.reCreate.queue"; ### Last Read the above points if you feel deeply Java The foundation is not solid enough or the brush is not enough or the knowledge is not complete Xiaobian tailored a set for you<Java Analysis Set of Top Job Interview Questions for Large Front-line Companies: JAVA Basics-intermediate-Advanced Interview+SSM frame+Distributed+performance tuning+Microservices+Concurrent programming+network+Design Mode+Data Structure and Algorithms> ![image](https://img-blog.csdnimg.cn/img_convert/b3aaf46c65ce43f7d16bbf21dbb61c99.png) For lack of knowledge, don't panic! There's a whole set of<Java Core Advanced Manual>,Instant leak detection and filling ![image](https://img-blog.csdnimg.cn/img_convert/d0f551e76412102090a61b09db8d71a5.png) > All are lost collections, sorted out by pure hands - sorted out in***[[My Learning Notes Complete)](https://codechina.csdn.net/m0_60958482/java-p7)***, friends in need can take their own There are also freehand outlines of various knowledge systems that can be combed: Java Building foundations, MySQL,Redis,Concurrent programming, Spring,Distributed High Performance Architecture, Micro Services Architecture, Open Source Framework Knowledge Points, etc. xmind Hand drawing~ ![image](https://img-blog.csdnimg.cn/img_convert/c4cc7073e7d382bc4a1515e261964049.png) ,Distributed High Performance Architecture, Micro Services Architecture, Open Source Framework Knowledge Points, etc. xmind Hand drawing~ [Outer Chain Picture Transfer in Progress...(img-cD87w8ok-1630029156710)] ![image](https://img-blog.csdnimg.cn/img_convert/d894ac5e56375735a350ca9914c638a7.png)