Table of contents
②. Message queue characteristics
2. Docker installation and deployment of RabbitMQ
3. springboot connection configuration
2.springboot project construction
1. Message queue
1.Message queue interpretation
The most common communication method between services is to directly call each other to communicate
.
The message can reach the other end immediately after being sent from one end
,
which is called instant messaging
(
synchronous communication
) .
After a message is sent from one end
,
it first enters a container for temporary storage
.
When a certain condition is reached
,
the container then sends it to the other end
,
which is called delayed message communication
(
asynchronous communication
) .
2. Problem thinking
Assume that after we place an order on Taobao
,
the Taobao backend needs to do the following things:
①.
Message notification system: Notify the merchant
that
you have a new order
,
please ship it in time
②.
Recommendation system: Update user portraits
and
re-recommend products that the user may be interested in.
③.Membership
system: Update user’s points and level information
3. There are problems
① Overcoupling: If
you need to trigger a new action when creating an order later
,
you have to change the code . At the end of the original order creation function , add another line of code
② Lack of buffering: If the membership system happens to be very busy or down when the order is created
,
then updating the member information will fail . We need a place to temporarily store messages that cannot be consumed.
4. Optimization scheme
We need a message middleware
to
achieve decoupling and buffering functions
.
5. Example:
Xiao Hong hopes that Xiao Ming will read more
and
often look for good books for Xiao Ming to read
.
The previous method was as follows: Xiao Hong asked Xiao Ming when he was free
,
sent the book to Xiao Ming
,
and personally supervised Xiao Ming before leaving after reading the book
.
Over time
,
the two Everyone finds it troublesome
.
Later, the method was changed to: Xiao Hong said to Xiao Ming, "You have to read all the books I put on the bookshelf."
Then
every time Xiao Hong found a good book, he put it on the bookshelf
,
and Xiao Ming saw a book on the bookshelf. Take it down and take a look
The bookshelf is a message queue , Xiaohong is the producer , and Xiaoming is the consumer
.
①.Benefits brought by
(1).
When Xiao Hong wants to give Xiao Ming a book
,
she doesn’t have to ask when Xiao Ming is free
. She
hands the book to him personally
.
Xiao Hong just puts the book on the bookshelf
.
In this way, Xiao Hong and Xiao Ming have more free time.
.
(2).
Xiao Hong believes in Xiao Ming’s reading consciousness and reading ability
.
There is no need to observe Xiao Ming’s reading process with her own eyes
.
Xiao Hong only needs to make an action of putting the book down
,
which saves time
.
(3).
When Xiaoqiang, another little friend who loves reading, joins tomorrow
,
Xiaohong still only needs to put the book on the shelf
,
and Xiaoming and Xiaoqiang take the book from the shelf.
(4).
The books on the bookshelf are placed there
.
If Xiao Ming has a fast reading speed, he will finish them early
. If his
reading speed is slow, he will finish them later
.
It doesn’t matter
.
Compared with Xiao Hong’s way of handing the book to Xiao Ming and supervising Xiao Ming to finish reading
,
Xiao Ming’s The pressure will be less
.
②.Message queue characteristics
(1).
Decoupling
:
Each member does not have to be affected by other members
, and
can be more independent
,
and is only connected through a simple container
.
(2).
Speed up
:
Xiaohongxuan only needs to do one action to put the book
,
which saves a lot of time for herself
.
(3).
Broadcast
:
Xiaohong only needs to work once
to
provide books for multiple friends to read
,
which greatly saves her time
and
makes the cost of joining new friends very low
.
④.
Peak shifting and flow control
:
The frequency of books given by Xiao Hong is unstable
.
If he gives five books today and tomorrow
,
and then gives another one every three months
,
then Xiao Ming only needs to read them from the bookshelf within three months. Just pick up five books and read them
, and
the pressure will not be so great
.
6. Email case:
There are a large number of users registering your software
,
and registration requests begin to have some problems under high concurrency conditions
.
For example, the email interface cannot bear the load
, or
the CPU is fully loaded due to
the large amount of calculations required when analyzing information
. This will cause the user data records to be quickly added to the database , but it will be stuck when sending emails or analyzing information .
As a result, the response time of the request increases significantly
,
or even times out
,
which is a bit uneconomical
.
Faced with this situation, these operations are generally put into the message queue
(
producer-consumer model
), and
the message queue slowly processes it
.
Registration can be completed quickly. Please
It will
not
affect the user's use of other functions
.
7.AMQP
An application layer standard advanced message queuing protocol that provides unified messaging services
,
and is a general application layer protocol
The message sending and receiving parties comply with this protocol to achieve asynchronous communication
.
This protocol specifies the format and working method of the message
8.Technical selection
9.RabbitMq
RabbitMQ
is a message queuing service that implements
AMQP (Advanced Message Queuing Protocol)
advanced message queuing protocol
,
using
Erlang
language
.
Server (Broker): A process
that receives client connections
and
implements
the message queue and routing functions of the
AMQP protocol
.
Virtual Host
: The concept of a virtual host
is
similar to a permission control group
.
There can be multiple Exchanges and Queues in
a
Virtual Host
.
Exchange:
A switch
that
receives messages sent by producers
and
routes them to the queue in the server
based on
the Routing Key
.
ExchangeType:
The switch type determines the routing message behavior .
There are three types of Exchange in
RabbitMQ ,
namely fanout , direct , and topic.
Message Queue
: The message queue
is
used to store messages that have not been consumed by consumers
.
Message
:
It consists of
Header
and
body
. Header is a collection of various attributes added by the producer , including whether the Message is persisted, what its priority is, which Message Queue receives it, etc. The body is the data that really needs to be sent.
Capacity
.
BindingKey
: Binding keywords
,
binding
a specific
Exchange
to a specific
Queue
.
2. Docker installation and deployment of RabbitMQ
1.
docker pull rabbitmq : managementNote that when obtaining the image, you must obtain the management version . Do not obtain the last version . Only the management version has a management interface.
2.
docker run - d \-- name my - rabbitmq \- p 5672 : 5672 - p 15672 : 15672 \- v / home / rabbitmq : / var / lib / rabbitmq \-- hostname my - rabbitmq - host \- e RABBITMQ_DEFAULT_VHOST = my_vhost \- e RABBITMQ_DEFAULT_USER = admin \- e RABBITMQ_DEFAULT_PASS = admin \-- restart = always \rabbitmq : management
--hostname : hostname ( an important note with RabbitMQ is that it stores data based on what is called a " node name " , which defaults to the hostname )-e : Specify environment variables :RABBITMQ_DEFAULT_VHOST : Default virtual machine nameRABBITMQ_DEFAULT_USER : Default usernameRABBITMQ_DEFAULT_PASS : Password for default username
After the container is started , you can view the logs through the docker logs container
docker logs my - rabbitmq
Enter the management background
http://ip:15672
3. springboot connection configuration
1. Configure account
Remember to require authorization
2. springboot project construction
3. Required dependencies
< dependency >< groupId > org . springframework . boot </ groupId >< artifactId > spring - boot - starter - amqp </ artifactId ></ dependency >
4.yml file configuration
server: port: 8080 spring: application: name: xx rabbitmq: #虚拟机id host: 192.168.208.137 password: admin port: 5672 username: springboot virtual-host: my_vhost
5. Producer Provider
①.RabbitConfig
package com.example.demo;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@SuppressWarnings("all")
public class RabbitConfig {
@Bean
public Queue firstQueue() {
return new Queue("firstQueue");
}
}
②.Sender
package com.example.demo;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
@SuppressWarnings("all")
public class Sender {
@Autowired
private AmqpTemplate rabbitTemplate;
public void sendFirst() {
rabbitTemplate.convertAndSend("firstQueue", "Hello World");
}
}
如有报:java.lang.IllegalStateException: Found multiple @SpringBootConfiguration annotated classes
Just comment out the @SpringBootApplication annotation on the pojo project startup class.
6. Consumer _
①.Receiver
package com.example.demo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@SuppressWarnings("all")
@Slf4j
@RabbitListener(queues = "firstQueue")
public class Receiver {
@RabbitHandler
public void process(String msg) {
log.warn("接收到:" + msg);
}
}
7. Custom data sending
package com.example.demo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@SuppressWarnings("all")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {
private String username;
private String userpwd;
}
package com.example.demo;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class ProviderApplicationTests {
@Autowired
private Sender sender;
@Test
@SneakyThrows
void contextLoads() {
//sender.sendFirst(new User("影子","123"));
User user=new User("yingz","123");
ObjectMapper mapper=new ObjectMapper();
sender.sendFirst(mapper.writeValueAsString(user));
}
}
public void sendFirst(String json) {
rabbitTemplate.convertAndSend("firstQueue", json);
}
package com.example.demo;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@SuppressWarnings("all")
@Slf4j
@RabbitListener(queues = "firstQueue")
public class Receiver {
@RabbitHandler
@SneakyThrows
public void process(String json) {
log.warn("接收到了:" + json);
ObjectMapper mapper=new ObjectMapper();
log.warn("接收到:" + mapper.readValue(json,User.class));
}
}