RabbitMQ. Basic use

Posted by praveenhotha on Thu, 24 Feb 2022 13:11:03 +0100

catalogue

I Message queue

1. Definition of message queue

2. Problem thinking

​3. Existing problems

4. Optimization scheme

5. Example:

①. Benefits

②. Message queue characteristics

6.Email case:

7.AMQP

8. Technical selection

 9.RabbitMq

II Docker installation and deployment RabbitMQ

III springboot connection configuration

1. Configure account

 2.springboot project construction

 3. Required dependencies

4.yml file configuration

5. Producer Provider

①.RabbitConfig

6. Consumer

①.Receiver

7. Custom data sending

 

I Message queue

1. Definition of message queue

The most common way of communication between services is to call each other directly to communicate , Once a message is sent from one end, it can reach the other end immediately , Instant messaging ( Synchronous communication )
After a message is sent from one end , First enter a container for temporary storage , When certain conditions are met , Then sent by this container to the other end , It is called delayed message communication ( asynchronous communication )

2. Problem thinking

   Suppose we place an order on Taobao , Taobao backstage needs to do these things:
  ①. Message notification system: notify merchants , You have a new order , Please deliver the goods in time
  ②. Recommended system: update user portrait , Recommand the products that the user may be interested in
  ③. Membership system: update users' points and grade information

 

3. Existing problems

  
① over coupling: if an order is created later , A new action needs to be triggered , Then you have to change the code , At the end of the original create order function , Another line of code
② lack of buffer: if the order is created , The member system happens to be in a very busy or down state , Then updating member information will fail , We need a place , To temporarily store messages that cannot be consumed

4. Optimization scheme

We need a message oriented middleware , To realize the function of decoupling and buffering .

5. Example:

Xiao Hong hopes Xiao Ming can read more books , Often look for good books for Xiao Ming , The previous way was like this: Xiao Hong asked Xiao Ming when he was free , Send the book to Xiao Ming , And personally supervise Xiao Ming to leave after reading the book . in the course of time , Both felt in trouble .
Later, the method was changed to: Xiao Hong said to Xiao Ming, "you should read all the books I put on the shelf." , Then every time Xiao Hong finds a good book, she puts it on the shelf , Xiao Ming took it down to read when he saw a book on the shelf
The bookshelf is a message queue. Xiaohong is a producer and Xiaoming is a consumer .

①. Benefits

(1). When Xiao Hong wants to give Xiao Ming a Book , Don't ask Xiao Ming when he is free , I handed him the book myself , Xiao Hong just put the book on the shelf . 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 , You don't have to observe Xiao Ming's reading process with your own eyes , Xiao Hong just needs to do an action of putting the book , It saves time .
(3). When another reading partner Xiaoqiang joins in tomorrow , Xiao Hong still only needs to put the books on the shelf , Xiao Ming and Xiao Qiang can take books from the bookshelf
(4). Where are the books on the shelf , Xiao Ming reads fast and finishes it early , If you read slowly, you'll finish it later , No problem , Compared with the way Xiao Hong handed the book to Xiao Ming and supervised Xiao Ming to finish reading it , Xiao Ming will feel less pressure .

②. Message queue characteristics

(1). decoupling : Each member does not have to be affected by other members , Can be more independent , Only through a simple container to contact .
(2). increase speed : Xiao Hongxuan just needs to do an action of putting the book , Save yourself a lot of time .
(3). radio broadcast : Xiao Hong only needs to work once , You can make multiple small partners have books to read , This greatly saved her time , It also makes the joining cost of new small partners very low .
④. Peak shifting and flow control : The frequency of Xiaohong's books is unstable , If you give five copies today and tomorrow , Then I gave another one every three months , Xiao Ming only needs to take five books from the bookshelf and finish reading them in three months , The pressure is not so great .

6.Email case:

A large number of users have registered your software , In the case of high concurrency, some problems begin to appear in the registration request .
For example, the mail interface can't bear it , Or a lot of calculations when analyzing information cpu the full load , This will happen, although user data records will soon be added to the database , But it's stuck when sending e-mail or analyzing information .
Resulting in a significant increase in request response time , Even timeout , This is a little uneconomical . In this case, these operations are generally put into the message queue ( Producer consumer model ), The message queue is processed slowly , At the same time, you can complete the registration quickly. Please
seek , It will not affect users' use of other functions .

7.AMQP

An application layer standard advanced message queuing protocol providing unified messaging service , Is a general application layer protocol
Both sending and receiving sides of messages can realize asynchronous communication by abiding by this protocol . This protocol specifies the format and working mode of the message

8. Technical selection

 9.RabbitMq

RabbitMQ It's a realization AMQP(Advanced Message Queuing Protocol) Message Queuing service of advanced message queuing protocol , use Erlang language .

 

Server(Broker): Receive client connections , realization AMQP The process of message queuing and routing function of the protocol .
Virtual Host : concept of virtual host , Similar permission control group , One Virtual Host There can be more than one in the Exchange and Queue.
Exchange: Switch , Receive messages from producers , And according to Routing Key Route messages to queues in the server Queue.
ExchangeType: The switch type determines the routing message behavior ,RabbitMQ There are three types of Exchange, namely fanout , direct , topic.
Message Queue : Message Queuing , Used to store messages that have not been consumed by consumers .
Message : by Header and body form ,Header Is a collection of various attributes added by the producer , include Message Whether it is persisted, what is the priority and by which Message Queue Receiving, etc .body Is the data that really needs to be sent
Allow .
BindingKey : binding keywords , Put a specific Exchange And a specific Queue Bind .

II Docker installation and deployment RabbitMQ

1.

docker pull rabbitmq : management
Pay attention to obtain the image when obtaining the image management Version , Don't get last Version ,management Version 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 (RabbitMQ An important note is that it is based on the so-called " Node name " Store data , The default is the host name )
-e : specify environment variables :
RABBITMQ_DEFAULT_VHOST : default virtual machine name
RABBITMQ_DEFAULT_USER : default user name
RABBITMQ_DEFAULT_PASS : password for default user name

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

III springboot connection configuration

1. Configure account

Remember that authorization is required

 

 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:
         #Virtual machine 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");
    }
}

If there is a report: 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("Received:" + 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("shadow", "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("Got it:" + json);
        ObjectMapper mapper=new ObjectMapper();
        log.warn("Received:" + mapper.readValue(json,User.class));
    }
}

Topics: RabbitMQ Distribution