What are AMQP and JMS?

AMQP: Advanced Message Queuing Protocol, which is a standard application-layer Advanced Message Queuing Protocol, and provides unified messaging services. Is an open standard for application layer protocols, designed for message-oriented middleware. Based on this protocol, the client and message middleware can transmit messages, and it is not limited by the different products of the client/middleware, different development languages and other conditions. Implementations of Erlang include RabbitMQ.

JMS: Java Message Service (Java Message Service) application program interface (JMS), proposed by Sun, and Sun defines the interface. Include create, Send, and Recieve. If you want to use it, you have to implement the interface it defines. Messaging services is a platformer independent API, and most MOM providers support JMS. The downside is that the language level is limited to JAVA, which is a bit of a counterpoint to the microservice idea. The language must be JAVA, not PY and so on.


Common MQ products

ActiveMQ: Based on JMS, Apache

RocketMQ :(Rocket) alibaba product, based on JMS, currently maintained by Apache based

Kafka: A distributed messaging system. Highlights: Extremely high throughput, hundreds of thousands of concurrent messages per second.

The RabbitMQ: Developed by the Erlang language and based on the AMQP protocol, RabbitMQ is more stable than other MQ products thanks to the features of the Erlang language. Erlang itself is a language for highly concurrent programming, so RabbitMQ is very fast. And it is based on THE AMQP protocol, more distributed, microservice friendly.


Install the RabbitMQ

I have prepared the RPM package for the installation in advance, as follows:

Operating system: Centos7.3

Recommended: I downloaded these three packages in advance, the background reply: RBMQ can directly get.

If you want to go to the trouble of downloading your own document, see the link at the end of this article.

To understand the science:

For example, if you install software A, it may depend on software B. Therefore, if you install software A directly, you will receive an error message saying that software B is missing in the current operating system environment, so you should install software B first and then try to install software A.

If you’ve read about Linux, you should know that RMP already handles all kinds of dependency packages, so it’s relatively easy to install.

# install ErlangYum install esl-erlang_23.0-1_centos_7_amd64. RPM -y yum install esl-erlang-compat-18.1-1.noarch# to install the rabbitmqThe RPM - the ivh the rabbitmq server - 3.8.9-1. El7. Noarch. RPMCopy the code

For example, if you encounter the following installation package error, just follow the prompts to solve it

Download the dependencies and try again to complete the installation.


Start the RabbitMQ

Run the following command to enable it:

service rabbitmq-server start
Copy the code

You can start immediately after installation as shown above.

The default configuration parameters are used for RabbitMQ. Generally, however, we want RabbitMQ to start with a configuration file that we can change, so that we can control it later. Let’s take a look at rabbitMQ authentication, authorization, access control, and configuration files.

You can also start the Web plug-in as follows.

After the web management module plug-in is enabled, visit http:// server IP address :15672/ to find the login entrance.

Set RabbitMQ to start:

chkconfig rabbitmq-server on
Copy the code


What is Authentication?

Once RabbitMQ is started, we want to use it only if we are connected to it with a username and password. Username and passowrd are users that have been granted certain permissions.

A user can connect to RabbitMQ to create a virtual host MQ. Username =guest, password=guest, virtualHost =/

This user is restricted to logging in to RabbitMQ locally (you are not allowed to use this user to log in to RabbitMQ remotely via IP +port), as follows:

Authentication is when you use a specific user to connect to MQ


Specify the RabbitMQ startup configuration file

Rabbitmq provides us with a template configuration file, which defaults to /usr/share/doc/rabbitmq-server-xxx/rabitmq.conf.example

It doesn’t matter if you don’t find, go on making copy a template configuration, manually create the/etc/rabbitmq/rabbitmq conf configuration file, and then put in you copy configuration is also ok.

Rabbitmq github addr: github.com/rabbitmq/ra…

These are basic shell commands, so I won’t go into detail here.

Note The file name is rabbitmq.config and must be stored in the /etc/rabbitmq directory.


How do I log in to RabbitMQ remotely as user guest

You can modify your MQ configuration file as follows:

Then restart MQ with the service command, try logging in on the web page, and you will log in successfully:

Official: It is strongly not recommended to allow the default user to log in to RabbitMQ remotely. Any programmer who has used RabbitMQ will know what the default user name and password are.

The recommended approach is to delete the default user and create a new user using the new security credentials


Manage users and rights

Actually article see here, what is a user? What are permissions? I’m sure you know that by now.

So what is managing users and permissions? Something as simple as adding/removing a User, which may belong to a line of service, to use RabbitMQ middleware. And assign a virtual host to the User that he can read and write.

In the next section we’ll talk more about what is a virtual host

This section is mainly through the experimental way, the actual Rabbit user and permission management!

There are two main ways:

**1. Manage ** through the Web console

2. Use the CLI

Since we just allowed the super administrator guest to remotely log in to MQ, you can manage users on the Web page as shown below. For example, I can add A new user, Changwu01, for line A and give it administrator privileges, and then the line of business can use MQ through that user.

You can also use the CLI to add users from the command line as follows:

Try logging in with that user and you will see: Error saying Daydream 01 is not the administrator. Cannot log in to console.

If you change the tags of Bairimeng01 to management and try logging in again, it will prompt you:

You can log in as guest and change the permissions of Bairimeng01 to administrator

Then change the permissions for Bairiemeng01 and click Update User

Re-use bairimeng01 after modification:

You will find that Bairimeng01 can be successfully logged in!

Check the current RabbitMQ users:

Create user airimeng03 from the command line and allow Daydream 03 to read and write virtualHost =/ from the command line

You can check with the console that our configuration is actually working.


Concepts in RabbitMQ


What is a virtual host

The relationship between RabbitMQ and virtual hosts can be understood by looking at MySQL and MySQL databases.

MySQL database is used by multiple lines of business. As shown in the following figure, each line of business creates its own database in MySQL and stores data in its own database without interfering with each other.

The relationship between RabbitMQ and virtual hosts is similar. Multiple lines of service can use RabbitMQ at the same time by binding different virtual hosts to each line of service:


Create a virtual host and specify that users can use it

Step1:

Step2:

Step3:

Step4: check


Five messaging models for RabbitMQ

RabbitMQ supports the following five message models. The sixth TYPE of RPC is essentially a service invocation and therefore does not count as a service communication message model.


Hello World

P (Producer/Publisher) : producer, a service that sends messages

C (consumer) : the service that receives the message

The red area is the Queue of MQ, which you can think of as a mailbox

  • First of all, when a letter comes, you don’t have to get it immediately
  • Second, it has a maximum capacity (limited by host and disk, it is a cache).
  • Allows multiple consumers to listen to the same queue and compete for messages


The Worker model

There is also only one work queue in the Worker model. But it is a competitive consumption pattern. As you can see, we have multiple consumers bound to the same queue, and consumers are competing to consume messages, which can effectively avoid message accumulation.

For example, it is possible to use this messaging model for SMS microservice clusters, and to request people to rush to consume.

How to implement this architecture: For HelloWorld, this is the same service that we started several times.


The subscription model

The subscription model is implemented with the help of a new concept: Exchange, and the different subscription models are essentially divided according to the type of Exchange.

There are three subscription models

  1. Fanout (broadcast model) : Sends messages to all queues bound to the switch (because they use the same RoutingKey).
  2. Direct: Sends messages to a queue with a specified Routing Key.
  3. Topic (wildcard) : Messages are passed to queues that have a Routing Patten(Routing pattern).


Subscribe to the Fanout model

The feature of this model is that when it sends a message, it doesn’t specify a Rounting Key, or it specifies a Routing Key, but all the consumers know that, and everyone gets the message, just like listening to the radio.


Direct model to subscribe to

P: the producer that sends a message to Exchange. When sending a message, it specifies a routing key.

X: Exchange, which receives a message from the producer and delivers it to a queue that matches the routing key exactly

C1: consumer whose queue has a message with a routing key of ERROR

C2: consumer whose queue has messages with routing keys as INFO, Error, and Warning

Consumers with different routingKeys will receive different messages from the switch, rather than using the same RoutingKey and broadcast model to distinguish them.


Subscribe to the Topic model

Similar to the Direct model. The difference is that Topic Routing keys support wildcard characters.


The background reply: RBMQ to obtain the following information:

You can use yum to install RPM packages for Golang Case, Java Case, Erlang virtual machine, rabbitMQ. Server, etc.


Hello World

In this section you can focus on what happens in the Web View as you connect, channel, send, and receive messages.

Send. Java:

To view the newly created connection:

View the newly created channel:

Check the status of messages in RabbitMQ:

Recv.java:

Execute the following message receiver to receive the sent message.

Check the RabbitMQ message consumption status in the Web view:

Check the status of the connection in the system, because I did not show the closed connection and channl, so you can see that there are two connections in the system:

The channel also exists:


The Worker model

It’s essentially the same service that we’ve started several times, so that’s the architecture.

Supplement point 1: An attribute can be added to the queue instead of the queue dividing tasks equally among consumers. Instead, they ask for new tasks from the queue when they’re done, so that everyone can do more.

// Set each consumer to handle only one message at a time
channel.basicQos(1);
Copy the code

Bonus point 2: When a recipient receives a message, you can configure a manual ACK as shown in the following figure


The subscription model

The subscription model is implemented with the help of a new concept: Exchange, and the different subscription models are essentially divided according to the type of Exchange.

There are three subscription models

  1. Fanout (broadcast model) : Sends messages to all queues bound to the switch (because they use the same RoutingKey).
  2. Direct: Sends messages to a queue with a specified Routing Key.
  3. Topic (wildcard) : Messages are passed to queues that have a Routing Patten(Routing pattern).


Subscribe to the Fanout model

The feature of this model is that when it sends a message, it doesn’t specify a Rounting Key, or it specifies a Routing Key, but all the consumers know that, and everyone gets the message, just like listening to the radio.

The sender:

Go to the Web View to check the status:

Run the receiver to consume the message


Direct model to subscribe to

Similar to the Fanout model, the sender sends: with a routingKey specified as follows

When the receiver accepts, it also specifies a routingKey as follows:


Subscribe to the Topic model

The Topic model is similar to the Direc model.

Differences: Switch types: Topic, RoutingKey: Supports regular expressions

The sender:

Receiver:


Message acknowledgement mechanism


ACK mechanism

The so-called ACK confirmation mechanism:

Automatic ACK: The consumer automatically sends an ACK to RabbitMQ upon receiving the message.

Manual ACK: We manually control consumers to send an ACK to RabbitMQ after receiving a successful message.

You can see above: with automatic ACK, RabbitMQ deletes the message immediately after the sender takes it out of the channel. Then, unfortunately, the consumer hung up before he could process the message. That means the message is actually lost.

You might say: Is there duplication of spending? This is not really an MQ issue. You can avoid this problem by doing logic at the logical level of your code, such as deduplicating, checking to see if it already exists before inserting, etc.

The specific implementation method can refer to the above: JAVA client /Worker model


Persistent switch


Persistent queue


Persistent message


SpringAMQP

SpringAMQP helps us implement a producer acknowledgement mechanism that tells the producer to resend if the message exchange is not routable


Environment set up

Configuration file: producer

The producer sends the message using the AmqpTemplate template

The AmqpTemplate template is not required by the consumer to send messages, so it is not configured

Virtual-host is the name of the virtual host bound to the current user. In RabbitMQ, users can only see the information under the associated virtual host.


Golang client

Pay attention to daydream, reply: RBMQ in the background to get the following information:

You can use yum to install RPM packages for Golang Case, Java Case, Erlang virtual machine, rabbitMQ. Server, etc.

There is a QR code at the end

To download dependency packages:

go get github.com/streadway/amqp
Copy the code


Hello World

The sender:

“No access to this vhost” = “no access to this vhost” = “no access to this vhost”

Step2: create the channel

Step3: declare queue and send messages to this queue

Step5: send the message

Receiver:

The consumer also needs to establish the connection and channel, and then declare the channel we want to consume, which is the same as the producer code above, so it is not stuck.

A consumer receives a message from a channel:

Processing messages:


The Worker model

The same Worker model and Simple model are similar. Nothing more than consumers of the Simple model start multiple instances.

Message distribution policy: By default, messages produced by RabbitMQ are distributed to C1 and C2 using the round-robin policy.

You can also set up a relatively fair distribution strategy like the one below: MQ will not send new messages to consumers until they have finished processing them, so that more work can be done by all.

Message confirmation mechanism:

What is the ACK mechanism? You can see the Golang client/message acknowledgement /ACK mechanism section below.

If the manual ACK is as follows:

When we set up manual ACK as above, we can ensure that the messages in MQ are not lost if the consumer hangs up before the message is processed.

But if MQ hangs up at this point, the message will also be lost.

To avoid this, set the Settings to persist messages in MQ as well


The subscription model

The subscription model is implemented with the help of a new concept: Exchange, and the different subscription models are essentially divided according to the type of Exchange.

There are three subscription models

  1. Fanout (broadcast model) : Sends messages to all queues bound to the switch (because they use the same RoutingKey).
  2. Direct: Sends messages to a queue with a specified Routing Key.
  3. Topic (wildcard) : Messages are passed to queues that have a Routing Patten(Routing pattern).


Fanout model of subscription model

The feature of this model is that when it sends a message, it doesn’t specify a Rounting Key, or it specifies a Routing Key, but all the consumers know that, and everyone gets the message, just like listening to the radio.

Producer: After obtaining the channel, a switch is created. The switch type is fanout.

Notice that fanout corresponds to a routingKey (which is empty)

Consumer: It is required that the consumer also declare the switch after obtaining the channel. The consumer queue has no name, and the queue has no routingKey. Make sure the name of the switch is correct.


Direct model of subscription model

Producer: Similar to Fanout, note that the switch is named Direct and adds a specific routingKey

Consumer:


Subscription model of the Topic model

Similar to the Direct model, but different: The type is topic and routingKey supports regular expressions.

The detailed code is no longer duplicated. Can get their own source learning.


Message acknowledgement mechanism


ACK mechanism

The so-called ACK confirmation mechanism:

Automatic ACK: The consumer automatically sends an ACK to RabbitMQ upon receiving the message.

Manual ACK: We manually control consumers to send an ACK to RabbitMQ after receiving a successful message.

You can see above: with automatic ACK, RabbitMQ deletes the message immediately after the sender takes it out of the channel. Then, unfortunately, the consumer hung up before he could process the message. That means the message is actually lost.

You might say: Is there duplication of spending? This is not really an MQ issue. You can avoid this problem by doing logic at the logical level of your code, such as deduplicating, checking to see if it already exists before inserting, etc.

The specific implementation method can refer to the Golang or JAVA client Worker model section above.


Persistent switch


Persistent queue


Persistent message


Data access

You can use yum to install RPM packages for Golang Case, Java Case, Erlang virtual machine, rabbitMQ. Server, etc. Follow the background reply: RBMQ to obtain the following information:




Reference:

Website: www.rabbitmq.com/

The get start:www.rabbitmq.com/getstarted….

The download rabbitmq:www.rabbitmq.com/download.ht…

Mapping between Rabbitmq and Erlang versions: www.rabbitmq.com/which-erlan…

The download erlang:www.erlang-solutions.com/resources/d…

The rabbitmq recommended erlang:www.rabbitmq.com/releases/er…

To learn more about rabbitmq3.8.X configurations: www.cnblogs.com/masy-lucife…

Rabbitmq3.8.9 GitHub:github.com/rabbitmq/ra…

Authentication, authorization, and access control: www.rabbitmq.com/access-cont…