SpringBoot (xv) ------ messaging middleware RabbitMQ, SpringBoot integrate RabbitMQ

RabbitMQ official website: https://www.rabbitmq.com

  • Outline

In most applications, the asynchronous communication system to enhance the service through the message middleware, extended decoupling ability.

  • Messaging services in two key concepts:
  1. Message broker (message broker) and a destination (Where do you want) When the message sender to send the message, taken over by the message broker, the message broker to ensure the message to the specified destination.
  • There are two main forms of the message queue of the destination
  1. Queue (queue): peer messaging (point-to-point)
  2. Theme (topic): publishing (publish) / subscribe (subscribe) messaging
  • Point to point:
  1. Message sender to send a message, the message broker put it in a queue, the message recipient gets the message content from the queue, the message is read out of the queue after the message has only one sender and receiver, but does not mean that there can be only a recipient
  • Published subscribe:
  1. The sender (publisher) sends a message to the topic, multiple recipients (subscribers) listening (subscribe to) this theme, it will receive a message at the same time when a message arrives
  • JMS (Java Message Service) JAVA Message Service:
  1. JVM specification is based on the message broker. ActiveMQ, HornetMQ is JMS implementation
  • AMQP(Advanced Message Queuing Protocol)
  1. Advanced Message Queuing Protocol, is a message broker specification-compliant JMS RabbitMQ AMQP is to achieve a

  • JMS and AMQP compare

 

ETC.

AMQP

definition

Java fire

Line-level network protocol

Cross-language

no

Yes

Cross-platform

no

Yes

Model

Offers two messaging models:

(1)、Peer-2-Peer

(2)、Pub/sub

Offers five models Message:

(1)、direct exchange

(2)、fanout exchange

(3)、topic change

(4)、headers exchange

(5)、system exchange

Essentially, the latter four and JMS pub / sub model is not much difference, only to do a more detailed breakdown on the routing mechanism;

Support message type

Multiple message types:

TextMessage

MapMessage

Changing Message

StreamMessage

ObjectMessage

Message (only the message header and properties)

byte[]

When the actual application, has a complex message, the message can be sent after the serialization.

Overview

JMS defines JAVA API-level standard; in java system, multiple client can interact through both JMS, do not need to modify the application code, but its poor support for cross-platform;

AMQP defines a standard wire-level protocol layer; natural cross-platform, cross-language features.

  •  RabbitMQ Profile

  • RabbitMQ profile:

RabbitMQ is an erlang developed by the AMQP (Advanved Message Queue Protocol) is an open source implementation.

  • The core concept Message

Message, the message is anonymous, which consists of a message header and a message body. Message body is opaque, and the message header by a series of optional attributes, these attributes include routing-key (routing keys), priority (priority relative to other message), delivery-mode (indicating that the message may need to persistent storage) and so on.

  • Publisher

The client application producers message, also issued a message to the switch.

  • Exchange

Switch, for receiving a message sent by the producer and to route the messages to the server queues. Exchange There are four types: direct (default), fanout, topic, and headers, forwarding of messages of different types of Exchange policies differ

  • Queue

Message queues, used to save the messages until sent to the consumer. It is a container for the message, the message is the end. A message can be put into one or more queues. Message has been in the queue inside, waiting for consumers to connect to the queue will remove it.

  • Binding

Binding an association between the message queue and switches. Binding is a key-based routing rule and the switch connecting the message queue, the switch can be understood to be a routing table constituted by the binding. Exchange Queue and binding can be many to many relationship.

  • Connection

Network connection, such as a TCP connection.

  • Channel

Channel, an independent bi-directional data connection flow channel multiplexing. Channel virtual connection is established in the real TCP connections, AMQP commands are sent out through the channel, whether it is announced that the subscription queue or receive messages, these actions are done through the channel. Since the establishment of TCP and destruction are very expensive overhead for the operating system, so the introduction of the concept of the channel, in order to reuse a TCP connection.

  • Consumer

Consumers message indicating a client application to obtain information from the message queue.

  • Virtual Host

Web hosting, represents a group of switches, message queues, and related objects. Web hosting is shared the same stand-alone server domain authentication and encryption environment. Is essentially a mini version of RabbitMQ server, with its own queue, switches, binding mechanisms and permissions for each vhost. AMQP is the basis of the concept of vhost must be specified at the time of connection, RabbitMQ is the default vhost /.

  • Broker

Message Queue server entity represents

  • RabbitMQ operating mechanism

  • The message routing AMQP

AMQP message routing process and Java developers are familiar with JMS there are some differences, AMQP added to Exchange and Binding role. Producers put the message posted on the Exchange, the message queue and eventually reach the consumer receives, and Binding decision to switch the message should be sent to the queue.

  • Exchange Type

There Exchange when distributing messages depending on the type of distribution policy differences, there were four types: direct, fanout, topic, headers. AMQP message headers match exactly the routing header instead of keys, switches and headers direct exchange, but the performance is poor lot, almost not used anymore, so a direct look further into three types:

1)、direct Exchange

Key message routing (routing key), and if the binding key Binding consistent switch will send a message to a corresponding queue. Routing keys exact match with the name of the queue, if the queue is bound to a key switch as in claim route "dog", only forward the message routing key labeled "dog" and not forwarded "dog.puppy", and does not deliver " dog.guard "and so on. It is an exact match, unicast mode.

2)、fanout Exchange

Each hair switch types to fanout messages are assigned to all queues bound up. fanout switch does not process routing keys, simply bind to the switch queues, each message sent to the exchanger will be forwarded to all queues bound with the switch. Much like the subnet broadcast, the hosts in each subnet have received a copy of the message. fanout type of forwarding the message is the fastest.

3)、topic Exchange

matching the topic switch mode routing key attribute allocation message, the routing key and a pattern matching, the case needs to bind to a queue mode. It will cut the string routing keys and key bindings into words, these words are separated by dots. It will also identify two wildcards: the symbol "#" and the symbol "*." # Matches zero or more words, * matches a word.

  • RabbitMQ installation testing

  • RabbitMQ installation

1), the use of docker mirror mounting rabbitMQ

[root@localhost ~]# docker pull rabbitmq:3-management

 

2), the mirror starting rabbitMQ

[root@localhost ~]# docker run -d -p 5672:5672 -p 15672:15672 --name myRabbitMq 985adbf1306

-d: background ;-p: 5672 host port mapping port mapping to port docker container, which is the port the client and rabbitmq communication port 15672 management interface; - name: the name of the currently running; 985adbf1306 (IMAGE ID)

 3), access management RabbitMQ page

User name, password guest

 

  •  RabbitMQ test

1), RabbitMQ message schematics

2) Create exchangers exchange

 exchange.direct、exchange.fanout、exchange.topic

 

3) create message queues Queues

hello、hello.news、hello.emps、helloWorld.news

4), and switches the queue binding

To work queue and switches must be binding 

Respectively switch exchange.direct, exchange.fanout, exchange.topic respectively, are bound hello, hello.news, hello.emps, helloWorld.news

 Topic binding rules are not the same

5), sends a message to the message queue

  •  exchange.direct switch sends a message

 

 

  •  exchange.fanout switch sends a message

 

 

  • exchange.topic switch sends a message

 

 

 

  • SpringBoot integrate RabbitMQ

 1) Create a project SpringBoot

 

Published 102 original articles · won praise 10 · views 110 000 +

Guess you like

Origin blog.csdn.net/WMY1230/article/details/104763030