Message middleware

Message Queue Middleware (MQ) : Leverages efficient and reliable messaging mechanisms for platform-independent data communication and distributed system integration based on data communication. It extends interprocess communication in a distributed environment by providing a messaging and message queuing model.

The role of messaging middleware

The decoupling

You only need to follow the same interface constraint between projects. You can modify and extend the business of both ends of the messaging middleware independently, and this interface constraint must be implemented in both ends of the processing process.

redundant

In many cases, directly under the condition of high concurrency “system” to deal with not to come over to the request of the front end, it needs to be a “container” to store data “redundant”, and then direct system one to one “container” data to do the business, this container is the message queue, to put a message in the queue, and then slowly consumption process, Message queues hold data until you are finished using it, and there is no loss of data.

Peak clipping

In visits to the sudden increase in time, system to support the highest peak is uneconomic to invest their resources, but the problem still need to run normally, of the highest mountain in the less is the number of occurrence peak, peak is high, but require the system to normal operation, and can’t go to the highest peak forecast resources, this time with a message queue peak clipping, support for emergency access pressure, The system will not crash due to sudden overload requests.

recoverability

When a part of the system fails, it will not affect the whole system. The message-oriented middleware reduces the coupling degree between processes. Even if a few message-processing processes fail, messages added to the message-oriented middleware can still be processed after the system recovers.

sequential

In many cases, data processing needs to ensure the sequence of data processing, put the sequential business data into a queue for processing, to a certain extent to save the sequence of data.

Asynchronous processing

In many cases, messages do not need to be processed immediately, and messaging middleware provides an asynchronous processing mechanism that allows messages to be queued for asynchronous processing.

RabbitMQ

Introduction of the RabbitMQ

RabbitMQ is a messaging middleware that uses Erlang to implement AMQP (Advanced Message Queuing Protocol). It originated in financial systems and is used to store and forward messages in distributed systems. RabbitMQ is developed and commercially supported by RabbitMQ Technologies Ltd. Rabbit is named after the creators of RabbitMQ because rabbits are very fast and breed like crazy.

The RabbitMQ characteristics

  • Reliability: Persistence of RabbitMQ, confirmation of transmission and publication, etc.
  • Flexible routing: Messages are routed through the switch before they are queued, and then put into a queue bound to the switch. For more complex routing functions, multiple switches can be bound together, and plug-ins can be used to implement their own switches.
  • Scalability: Multiple RabbitMQ nodes can form clusters that can be expanded based on service requirements.
  • High availability: Queues can be mirrored on machines in the cluster so that they are available even if some nodes fail.
  • Multiple protocols: RabbitMQ supports multiple messaging middleware protocols, including STOMP MQ, in addition to AMQP.
  • Administration Interface: RabbitMQ provides an easy-to-use user interface that allows users to monitor and manage messages, nodes in a cluster, etc.

The RabbitMQ model

RabbitMQ is basically a producer, consumer model, responsible for receiving messages, routing them to queues, storing them, forwarding them and consuming them. Think of message delivery as a process of delivery. Mail sending (RabbitMQ initiating), mail sending (RabbitMQ routing and queuing), mail receiving (RabbitMQ consuming). At the computer level, RabbitMQ is more like a switch model.

  • Producer: A Producer posts a message to RabbitMQ, such as a JSON string, through the Producer – to the switch – to the queue – and then to the queue’s consumers.
  • Consumer: the party that receives the message. The Consumer connects to the corresponding RabbitMQ server and subscribes to the corresponding queue to consume the message sent by the producer.
  • Exchange: Can be understood as a channel service between producers and queues. A switch can be bound to one queue or to multiple queues.

  • Queue: RabbitMQ stores messages internally. It has a first-in, first-out feature, and a Queue can be subscribed to by multiple consumers. Messages are round-robin to different consumers, rather than each receiving all messages.

  • RountingKey: When a producer sends a message to the switch, it specifies a RountingKey to specify the routing rule for the message. This RountingKey must be used together with BindingKey.
  • Binding (Binding) : RountingKey can understand the routing rules of the producer and the switch. BindingKey is the key that the switch and the queue are bound to. The messages are then matched together by RountingKey and BindingKey, so RabbitMQ knows how to route the messages correctly to the queue. BindingKey does not work in all cases, depending on the switch type, such as fanout switches that ignore BindingKey. Instead, the message is routed to all queues bound to the exchange.

code

  • Introduce RabbitMQ configuration, mainly address, port, username and password configuration.
# # mq basic configuration mq. Rabbit. Send the exchange = default mq. Rabbit. Send. The router = default spring. The rabbitmq. Addresses = 172.0.0.1:5672 spring.rabbitmq.username=admin spring.rabbitmq.password=admin spring.rabbitmq.virtual-host=/ Spring. The rabbitmq. Connection timeout = 15000 # # mq configuration spring consumers. The rabbitmq. Listener. Concurrency = 5 spring.rabbitmq.listener.max-concurrency=10Copy the code
  • To initialize RabbitMQ queues, manually log in to RabbitMQ to create queues, switch bindings, etc.
/ * * * * * defined Queue / @ Bean public Queue noticeQueue () {return new Queue (" cn. LCH. Wxcenter. NoticeQueue "); } /** * Define Exchange **/ @bean DirectExchange noticeExchange() {return new DirectExchange("noticeExchange"); } / * * * * * @ Queue and switches Binding param * @ return * / @ beans Binding bindingExchangeMessageNotice (Queue noticeQueue, DirectExchange noticeExchange) { return BindingBuilder.bind(noticeQueue).to(noticeExchange).with("notice"); }Copy the code
  • Producer content push
/** * @param jsonObject */ public void sendNotice(String jsonObject) { amqpTemplate.convertAndSend("noticeExchange", "notice", jsonObject); }Copy the code
  • Consumer consumption news
@Component @RabbitListener(queues = "cn.lch.wxcenter.noticeQueue") public class WxAddUserBusiness { private Logger logger = LoggerFactory.getLogger(WxAddUserBusiness.class); @RabbitHandler public void receive(String json, Channel channel, IOException {try {system.out.println (" Received cancellation Message: "+ JSON); } catch (Exception e) {logger.info(" message failed: "+ e); e.getStackTrace(); / / message to confirm the channel basicAck (message. GetMessageProperties () getDeliveryTag (), false); }}}Copy the code

Switch Type

  • The main types of switch used for RabbitMQ are fanout direct Topic headers and System and custom headers, which are not discussed here.
  • Fanout: It routes all messages sent to the switch to all queues bound to the switch.
  • Direct: This routes messages to queues where BindingKey and RoutingKey match exactly. Set routing keys for exact matches when sending messages. The message will be routed to queue 1 and the message will be routed to queue 2 with the routing key “Person” and the routing key “student”.

  • Topic: It also matches just a fuzzy match between BindingKey and RoutingKey. A RoutingKey can be passed with a “.” split character, such as lch.com.cn. BindingKey is also made by “. The split sign is nothing more than an “*” and “#” fuzzy match, an asterisk to match a word, and a “#” to match multiple words (which can be zero).
  • Headers: Rarely does this work. This is matched by the headers attribute in the content of the message being sent. Low efficiency Low performance.

RabbitMQ workflow

  • Producers:
  1. A producer connects to a RabbitMQ Broker and establishes a connection to open a Channel.
  2. The producer declares a switch and sets properties such as switch type and persistence.
  3. The producer declares a queue and sets attributes such as whether it is exclusive, persistent, and automatically deleted.
  4. The producer binds the switch to the queue through a routing key.
  5. The producer sends a message to the RabbitMQ Broker containing routing key and switch information.
  6. The switch looks up and matches the corresponding queue based on the routing key sent by the producer. If a corresponding queue is found, the message is put into that queue. If not found, discard or roll back to the producer based on the animal configured by the producer.
  7. Close the channel.
  8. Close the connection.
  • consumers
  1. A consumer connects to a RabbitMQ Broker and establishes a connection to open a Channel.
  2. The consumer requests the RabbitMQ Broker to consume messages in the corresponding queue, possibly setting up the corresponding callback function, and other preparations.
  3. The consumer consumes the message while the RabbitMQ Broker responds and delivers the message to the queue.
  4. Consumer acknowledgement (ACK) of the received message.
  5. RabbitMQ removes the corresponding confirmed message from the queue.
  6. Close the channel.
  7. Close the connection.

summary

I explained the basics of RabbitMQ, including Producer, Consumer, Queue, Exchange, RoutingKey, Binding and so on. This paper introduces the working flow of switch types fanout Direct, topic, HEADERS and RabbitMQ.

note

  • RabbitMQ Combat Guide by Zhu Zhonghua