1. An overview.
-
In most applications, message service middleware can be used to improve the asynchronous communication and extend the decoupling capability of the system.
-
There are two important concepts in messaging services: Message Broker and destination. When the message sender sends the message, it is taken over by the message broker, which ensures that the message is delivered to the specified destination.
-
Message queues have two main forms of destination:
- Queue: Point-to-point message communication
- Topic: Publish/subscribe message communication
-
Point-to-point: a sender sends a message, the message broker places it in a queue, the receiver retrives the message from the queue, and the message is read and removed from the queue. There is only one sender and receiver, but not only one receiver
-
Publish-and-subscribe: a sender (publisher) sends a message to a topic, multiple receivers (subscribers) listen (subscribe) to the topic, and receive the message when it arrives
-
JMS (Java Message Service) Java Message Service: Specification based on JVM Message broker. ActiveMQ and HornetMQ are JMS implementations
-
Advanced Message Queuing Protocol (AMQP) is an Advanced Message Queuing Protocol (AMQP) that is compatible with JMS RabbitMQ
-
Spring supports
- Spring-jms provides support for JMS
- Spring-rabbit provides support for AMQP
- An implementation of ConnectionFactory is required to connect to the message broker
- Provide JmsTemplate, RabbitTemplate to send messages
- The @jMSListener (JMS), @RabbitListener (AMQP) annotations listen on methods for messages published by the message broker
- @enableJMS, @enablerabbit Enable support
-
Spring Boot is automatically configured
- JmsAutoConfiguration
- RabbitAutoConfiguration
2. Application scenarios.
-
Asynchronous processing
- The first case is synchronous processing. When a user initiates a registration request, the server writes data to the database in 50ms, then sends an email in 50ms, and then sends a short message in 50ms, so the user waits for 150ms.
- The second case is a concurrent operation. The user initiates the registration request, the server writes the data to the database in 50ms, and then executes the sending of emails and SMS in 50ms in the way of multi-threading, so that the waiting time of the user is 100ms.
- The third case is message processing. The user sends the registration request, and it takes 50ms for the server to write the data to the database, and about 5ms for the server to write the registration information to the message queue, and then it can respond to the user that the registration is successful. The next step is to listen to the message queue (if there is a message in the queue), get the registered information, and send an email or SMS to the user.
3. The RabbitMQ profile
-
The RabbitMQ introduction:
- RabbitMQ is an open source implementation of the Advanved Message Queue Protocol (AMQP) developed by Erlang.
-
The core concept
-
Message
- A message, which is anonymous, consists of a header and a body. The body of the message is opaque, and the header consists of a set of optional attributes, including routing-key, priority (priority over other messages), delivery-mode (indicating that the message may require persistent storage), and so on.
-
Publisher
- The producer of messages is also a client application that publishes messages to the exchange.
-
Exchange
-
A switch that receives messages sent by producers and routes them to queues in the server.
-
There are four types of Exchange:
- Direct (default, peer-to-peer), Fanout (publish/subscribe), Topic (publish/subscribe, routing key matching), and headers(header matching AMQP messages, similar to Direct, but poor performance, almost not used), Different types of Exchanges use different policies to forward messages.
-
-
Queue
- Message queues, used to hold messages until they are sent to consumers. It is the container and destination of the message. A message can be put into one or more queues. The message remains in the queue, waiting for the consumer to connect to the queue to pick it up.
-
Binding
- Binding for association between message queues and exchanges. A binding is a routing rule that connects a switch to a message queue based on a routing key, so a switch can be thought of as a routing table made up of bindings. Exchange and Queue bindings can be many-to-many.
-
Connection
- A network connection, such as a TCP connection.
-
Channel
- Channel: an independent two-way data channel in a multiplexing connection. A channel is a virtual connection established in a real TCP connection. AMQP commands are sent through the channel. No matter publishing messages, subscribing to queues or receiving messages, these actions are completed through the channel. Because it is very expensive for an operating system to establish and destroy TCP, the concept of a channel was introduced to reuse a TCP connection.
-
Consumer
- Message consumer, representing a client application that retrieves a message from a message queue.
-
Virtual Host
- Virtual host, representing a batch of exchanges, message queues, and related objects. A virtual host is a separate server domain that shares the same authentication and encryption environment. Each Vhost is essentially a mini RabbitMQ server with its own queue, switch, binding and permission mechanism. Vhost is the basis of the AMQP concept and must be specified at connection time. The default vhost for RabbitMQ is /.
-
Broker
- Represents the message queue server entity
-
4. The RabbitMQ integration
- Introducing spring – the boot – starter – closer
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency
Copy the code
- Application. Yml configuration
spring.rabbitmq.host=localhost
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.port=5672
#spring.rabbitmq.virtual-host=
Copy the code
-
Test the RabbitMQ
- AmqpAdmin: management component
- RabbitTemplate: Message sending processing component
@Autowired RabbitTemplate rabbitTemplate; @Autowired AmqpAdmin amqpAdmin; @test public void createExchange(){// createExchange(direct: Point to point mode) / / amqpAdmin declareExchange (new DirectExchange (" amqpAdmin. Exchange ")); // Create a Queue and specify routing-key routing keys and Durable // amqpadmin. declareQueue(new Queue("amqpadmin.queue", true)); / / create binding rules / / amqpAdmin declareBinding (new Binding("amqpadmin.queue",Binding.DestinationType.QUEUE,"amqpadmin.exchange","amqp.haha",null)); } /** * 1, @test public void contextLoads() {// contextLoads(); Rabbittemplate. send(exchage, routeKey, message); //object is the message body by default, just pass in the object to be sent, automatically serialized to rabbitMQ; //rabbitTemplate.convertAndSend(exchage, routeKey, object); Map<String,Object> map = new HashMap<>(); Map. put(" MSG "," this is the first message "); map.put("data", Arrays.asList("helloworld",123,true)); / / object is the default serialization send out rabbitTemplate convertAndSend (" exchange. Direct ", "llsydn. News," the map). } / / receiving data, how the data is automatically converted to json sent @ Test public void the receive () {Object o = rabbitTemplate. ReceiveAndConvert (" llsydn. News "); System.out.println(o.getClass()); System.out.println(o); } /** ** / @test public void sendMsg(){Map<String,Object> Map = new HashMap<>(); Map. put(" MSG ", "this is the first message "); map.put("data", Arrays.asList("helloworld", 123, true)); rabbitTemplate.convertAndSend("exchange.fanout", "", map); }Copy the code