Application scenarios

  • Asynchronous execution
  • The application of decoupling
  • Message is sent
  • Message delivery

Introduce the RabbitMQ

RabbitMQ is a middleware that implements AMQP (Advanced Message Queuing Protocol). The server is written in ErLang and is an important part of a distributed system

Relevant concepts

Roles: sender, switch, queue, receiver, rabbit Adds a layer of switch concepts to the normal message queue

Schematic diagram:



  • P for producer
  • C stands for consumer
  • The middle one, RabbitMQ, contains switches and queues

A few more important concepts:

  • Virtual host: A virtual host holds a set of switches, queues, and bindings. What virtual hosts do: Access to virtual hosts in Rabbit is limited to A fine-grained level of control. To deny user A access to virtual hosts (including queues and bindings) from user B, create virtual hosts for user A and user B respectively. Each Rabbit server has a default virtual host “/”
  • Switch: Forwards messages, but does not store them. If no queue is bound to the switch, it will discard messages sent by the producer
  • Routing key: When a message arrives at the switch, the switch needs to determine which queue to send it to based on the routing key
  • Bind: The switch is bound to queues in a many-to-many relationship

switches

The switch only receives and forwards messages to the relevant queue, it does not store messages. An error is returned if the queue is not found in ACK mode.

Four types of switches

Direc (Binding – delivery)

Direc is rabbit’s default switch mode, and is the easiest way to find queues based on full-text matches

Direc’s behavior is “bind first, deliver later.” That is, a routing key (routing_key) is set before binding. Messages are forwarded to the bound queue only when the routing key matches

model



In the figure, X-Q1 has one routing key: Orange, and X-Q2 has two routing keys: black and green. When the message comes, it will be forwarded to the bound queue according to the bound routing key

One switch — Queues can bind multiple routing keys. Q2 requires both black and green, in case other queues require either green or black

Topic (most flexible)

Topic relies on wildcards to forward messages. The binding between the switch and the route defines a routing pattern, and the switch can forward messages only if the pattern matches

Note:

  • The routing key must be a string of characters separated by periods (.)
  • The routing mode must contain an *

Similarities and differences between topic and direct

Send code similar to:

rabbitTemplate.convertAndSend(“topicExchange”,”key1.key2.key3″,”msg”)

The first parameter indicates the switch, the second parameter indicates the routing key, and the third parameter indicates the message to be sent

Topic is similar to Redic except that patterns are supported in matching, and two wildcards can be used in dotted routing keys:

  • * : indicates a word
  • # : indicates zero or more

Headers 

Headres also matches by pattern, and topic differs from Redic in that it does not use a fixed routing key, but a custom matching type

Fanout

The broadcast message mode, whether it is a routing key or a routing key, sends the message to all queues bound to it. If a routing key is configured, the message is ignored



Spring Boot inherits RabbitMQ

Pom package

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

The configuration file

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=username
spring.rabbitmq.password=passwordCopy the code


See sample code for details

RabbitMQ: Thank you very much