About RabbitMQ
- RabbitMQ is an open source message broker software (also known as message oriented middleware) that implements the advanced message queuing protocol (AMQP). RabbitMQ server is written in Erlang language, and clustering and failover are built on the framework of open telecommunications platform. All major programming languages have client libraries that communicate with proxy interfaces.
- Compliance Agreement - AMQP:
- AMQP (Advanced Message Queuing Protocol) is an application layer standard that provides unified message service. Advanced Message Queuing Protocol is an open standard of application layer protocol, which is designed for message oriented middleware. The client and message middleware based on this protocol can deliver messages, which is not limited by different products and development languages of the client / middleware.
Synchronous call and asynchronous call
Synchronous call
- Advantages of synchronous call: strong timeliness and immediate results
- Problems of synchronous call: high coupling, degradation of performance and throughput, additional resource consumption and cascading failure
Asynchronous call
- Advantages of MQ asynchronous communication: low coupling, improved throughput, fault isolation and traffic peak shaving
- Disadvantages of asynchronous communication: it depends on the reliability, security and throughput of Broker, the architecture is complex, the business has no obvious process line, and it is difficult to track and manage
What is MQ
- MQ (message queue), which is a message queue in Chinese, is literally the queue where messages are stored. That is, the Broker in the event driven architecture.
RabbitMQ quick start
RabbitMQ overview
- RabbitMQ is an open source message communication middleware developed based on Erlang language. Its official website address is: https://www.rabbitmq.com/
- Some roles in RabbitMQ:
-publisher: producer
-consumer: consumer
-exchange: a switch responsible for message routing
-Queue: queue, storing messages
-virtualHost: virtual host, which isolates the exchange, queue and message of different tenants
Common message model
- The official document of MQ gives five Demo examples of MQ, corresponding to several different usages:
Basic message queue
Work message queue
Publish and Subscribe (publish, Subscribe)- There are three types of switches:
Fanout Exchange: Broadcast
Direct Exchange: Routing
Topic Exchange: Topics
- There are three types of switches:
Introductory case
- 1. Import SpringBoot dependency of related parent project:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency>
- 2. Send message transaction side (publisher)
- Idea: establish connection, create channel, declare queue, send message, close connection and channel
- code implementation
public class PublisherTest { @Test public void testSendMessage() throws IOException, TimeoutException { // 1. Establish connection ConnectionFactory factory = new ConnectionFactory(); // 1.1. Set connection parameters: host name, port number, vhost, user name and password factory.setHost("192.168.150.101"); factory.setPort(5672); factory.setVirtualHost("/"); factory.setUsername("xunmeng"); factory.setPassword("123321"); // 1.2. Establish connection Connection connection = factory.newConnection(); // 2. Create a Channel Channel channel = connection.createChannel(); // 3. Create queue String queueName = "simple.queue"; channel.queueDeclare(queueName, false, false, false, null); // 4. Send message String message = "hello, rabbitmq!"; channel.basicPublish("", queueName, null, message.getBytes()); System.out.println("Message sent successfully:[" + message + "]"); // 5. Close channels and connections channel.close(); connection.close(); } }
- 3. Receiving message transaction end (consumer)
- Idea: establish connections, create channels, declare queues, and subscribe to messages
- code implementation
public class ConsumerTest { public static void main(String[] args) throws IOException, TimeoutException { // 1. Establish connection ConnectionFactory factory = new ConnectionFactory(); // 1.1. Set connection parameters: host name, port number, vhost, user name and password factory.setHost("192.168.150.101"); factory.setPort(5672); factory.setVirtualHost("/"); factory.setUsername("xunmeng"); factory.setPassword("123321"); // 1.2. Establish connection Connection connection = factory.newConnection(); // 2. Create a Channel Channel channel = connection.createChannel(); // 3. Create queue String queueName = "simple.queue"; channel.queueDeclare(queueName, false, false, false, null); // 4. Subscribe to messages channel.basicConsume(queueName, true, new DefaultConsumer(channel){ @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { // 5. Processing messages String message = new String(body); System.out.println("Message received:[" + message + "]"); } }); System.out.println("Waiting to receive messages...."); } }
summary
- Message sending process of basic message queue:
-
Establish connection
-
Create channel
-
Using channel to declare queue
-
Sending messages to queues using channel
- Message receiving process of basic message queue:
-
Establish connection
-
Create channel
-
Using channel to declare queue
-
Define the consumer's consumption behavior handleDelivery()
-
Using channel to bind consumers to queues