Messaging middleware uses efficient and reliable messaging mechanism to communicate platform-independent data and integrate distributed systems based on data communication. It extends interprocess communication in a distributed environment by providing a messaging and message queuing model. For message-oriented middleware, there are generally producers and consumers.

Common message-oriented middleware products:

1). ActiveMQ

ActiveMQ is the most popular and powerful open source message bus produced by Apache. ActiveMQ is a JMS Provider implementation that fully supports JMS1.1 and J2EE 1.4 specifications. We introduce the use of ActiveMQ in this course.

2). RabbitMQ

AMQP protocol lead implementation, support a variety of scenarios. Taobao’s MySQL cluster uses it for internal communication,

The communication component of OpenStack open source cloud platform was first applied in the financial industry.

3). ZeroMQ

Add the author wechat YDT939, there are more organized Java micro service notes, source code, selected interview questions!

Fastest message queuing system ever

4). Kafka

A subproject under Apache. Features: high throughput, can reach 10W/s on a common server

Throughput rate; A completely distributed system. Suitable for handling large amounts of data

1). Decoupling: Producers in the middleware only send messages, and consumers only need to obtain messages from queues for consumption, so as to achieve business decoupling.

2). Redundant storage: In some cases, the process of processing data will fail. Message-oriented middleware avoids the risk of data loss by persisting data until it has been fully processed. Before removing a message from the message middleware, your processing system needs to explicitly indicate that the message has been processed to ensure that your data is safely stored until you are done using it.

3). Recoverability: When a part of the system component fails, the whole system will not be affected. Message-based middleware reduces compliance between processes, so that even if a message-processing process dies, messages added to the message-based middleware can still be processed after the system recovers.

4). Order guarantee: In most usage scenarios, the order of data processing is very important, and most message-oriented middleware supports a certain degree of order.

5). Buffering: In any significant system, there will be elements that require different processing times. Message-oriented middleware uses a layer of buffering to help tasks execute most efficiently, and writing to message-oriented middleware is processed as quickly as possible.

6). Asynchronous communication: Many times applications do not want or need to process messages immediately. Message-oriented middleware provides asynchronous processing mechanisms that allow applications to put some messages into the message-oriented middleware, but not process them immediately and process them later as needed.

3.1 Installing dependent Environments

The RPM – the ivh Erlang 20.3.8.6-1. El6. X86_64. RPM

yum -y install epel-release

yum -y install socat

3.2 install the rabbitMQ

The RPM – the ivh the rabbitmq server – 3.7.7-1. El6. Noarch. RPM

Add the author’s wechatYDT939, there are more organized Java microservices notes, source code, selected interview questions!

3.3 Adding a User

By default, the management page can be accessed only from the local host running Linux. To enable the management page to be accessed from other hosts, configure the following parameters:

rabbitmq-plugins enable rabbitmq_management

Adding an access user:

rabbitmqctl add_user admin admin

3.4 Starting/Stopping RabbitMQ

Start: service rabbitmq-server start

Stop: service rabbitmq-server stop

Check the rabbitmq-server status

4.1 the Overview to

This column mainly shows the summary information of MQ, such as the number of messages, connections, channels, Exchanges, queues, and consumers.

RabbitMQ Usage Guide

4.2 Exchange

This column mainly shows the switch under the current virtual host, you can also add a new switch here, and configure the corresponding switch rule attribute.

RabbitMQ Usage Guide

4.3 the Queues team

This column displays message queue information, which contains the summary information of each queue. You can also add queues in this column

Queue

RabbitMQ Usage Guide

4.4 Admin System management

This column displays the user management information, including the display of the user list, adding users, adding virtual hosts and other information

RabbitMQ Usage Guide

5.1 Producers and consumers

5.1.1 producers

A Producer is a party that delivers messages.

The producer creates the message and publishes it to RabbitMQ. A message can generally contain two parts: the message body and the Label. The body of a message can also be called a payload. In practical applications, the body is usually a piece of data with a business logical structure, such as a JSON string. You can, of course, serialize the message body further. The label of the message is used to represent the message, such as the name of a switch and a routing key. The producer sends the message to RabbitMQ, which then sends the message to interested consumers based on the label.

Add the author’s wechatYDT939, there are more organized Java microservices notes, source code, selected interview questions!

5.1.2 consumers

Consumer: The party that receives the message.

The consumer connects to the RabbitMQ server and subscribes to the queue. When a consumer consumes a message, it consumes only the payload of the message. In the process of message routing, the label of the message is discarded, and the message stored in the queue is only the message body, and the consumer only consumes the message body, so the producer of the message is not known, of course, the consumer does not need to know.

5.2 the queue

Queue: The internal object of RabbitMQ for storing messages.

RabbitMQ Usage Guide

5.3 Switches, Routing Keys, and Bindings

5.3.1 switch

Exchange: indicates a switch. In the figure above we can temporarily understand that the producer sends a message to the queue, but this does not happen in RabbitMQ. In reality, a producer sends a message to an Exchange, which routes the message to one or more queues. If the route is not available, it may be returned to the producer or discarded. Here you can think of the switch in RabbitMQ as a simple entity.

RabbitMQ Usage Guide

There are four types of switches in RabbitMQ: FANout, Direct, Topic, and headers. Each type has its own routing policy.

5.3.2 routing key

RoutingKey: indicates a RoutingKey.

When a producer sends a message to an exchange, it typically specifies a RoutingKey that specifies the routing rules for the message, and this RoutingKey needs to be used in conjunction with the exchange type and the BindingKey to take effect.

With a fixed exchange type and BindingKey, producers can specify a RoutingKey when sending a message to the exchange to determine where the message will go.

5.3.3 binding

Binding: Binding. RabbitMQ binds the switch to the queue, usually specifying a BindingKey so RabbitMQ knows how to route messages to the queue correctly.

5.4 Switch Type

1).fanout: it routes all messages sent to the switch to all queues bound to the switch.

2).direct: This type of exchange routing rule is also simple, it will route messages to those

Queue where BindingKey and RoutingKey exactly match.

3). Topic: As mentioned above, the routing rules of the direct type switch completely match BindingKey and RoutingKey, but this strict matching mode cannot meet the requirements of actual business in many cases. Topic exchanges extend the matching rules. Similar to direct exchanges, they route messages to queues that match BindingKey and RoutingKey, but the matching rules are a little different:

A RoutingKey is a string separated by a dot “.”, such as com.itcast.client, com.itheima.exam.

BindingKey, like a RoutingKey, is a string separated by a dot (.).

BindingKey can have two special strings “*” and “#” for fuzzy matching, where

“#” is used to match a word, and “*” is used to match multiple individuals (which can be zero).

4).headers: This type of exchange does not rely on the matching rules of the routing key to route the message, but matches the HEADERS attribute in the content of the sent message.

6.1 Queue Binding

6.1.1 Creating a Queue

Create a queue and name it in the RabbitMQ admin window.

RabbitMQ Usage Guide

6.1.2 Creating an Exchange

Create a switch in the RabbitMQ admin window, specify the name and type of the switch.

RabbitMQ Usage Guide

6.1.3 Binding queues and Switches

Click the corresponding Exchange in the Exchange list to enter the binding interface, specify the queue name, specify the RoutingKey, and bind the queue to the Exchange using the RoutingKey.

RabbitMQ Usage Guide

Later, when a message is sent, you can route it to the queue by specifying an Exchange and a RoutingKey.

RabbitMQ Usage Guide

6.2 Logical code for sending messages

6.2.1 Importing dependencies

RabbitMQ Usage Guide

6.2.2 Sending Messages

RabbitMQ Usage Guide

RabbitMQ Usage Guide

6.3 Monitoring the sending platform

RabbitMQ Usage Guide

7.1 Importing Dependencies

RabbitMQ Usage Guide

7.2 Receiving Messages

RabbitMQ Usage Guide

RabbitMQ Usage Guide

7.3 Result Output

RabbitMQ Usage Guide

Among them:

ConsumerTag: Tag used to send messages to consumers

Properties: Header information data for message content

Envelope: PACKET of message body containing information such as the exchange, routingKey specified at the time the message was sent.