Spring-Boot --------- RabbitMQ (message queue)

First, the news service scene

1, asynchronous processing

Here Insert Picture Description
A user login, registration information into the database, and send e-mail and SMS registration. Such synchronization call needs every step of 50ms, it will be very slow. (E-mail and send text messages instead of the user needs to immediately see the results, it can slow process).
Here Insert Picture Description
There is a second way, using multi-threaded implementation of e-mail and text messages, so you only need 100ms, this is still too slow.
Here Insert Picture Description
In the third embodiment, after the registration information database to write, then the information is written to use the message queue, and then sends an SMS message and can be read from the message queue subsequent processing by asynchronous read.

2, the application of decoupling

Here Insert Picture Description
Order and inventory system write system in which one application, is to be coupled, single inventory reduction. Process will be very slow.
Here Insert Picture Description

The inventory system and order system is made separately extracted micro-services, it immediately orders the system to write a single message queue, receives order information immediately inventory system, the system calculates the stock inventory related operations.

3, flow clipping

Here Insert Picture Description
It applied to the spike, if there are 10,000 of goods, 100,000 individuals to spike, everyone send requests, tomcat for processing, it is easy to get stuck. Application message queue, the user's request message queue, add the maximum value to the message queue, only store ten thousand, who soon will come first to enter the queue. Removing spike business logic for processing a message from the message queue slowly.

Second, the concept

  1. In most applications, middleware message service may be by lifting asynchronous communication system, the decoupling capacity expansion

  2. Two important concepts:

    1. Message broker (message broker) and destination (destination)
    2. When the message sender to send a message, the message will take over the agency, the Message Agent to ensure message delivery to a specified destination.
  3. There are two main forms of the message queue of the destination

    1. Queue (queue): peer messaging (point-to-point)
      • Message sender sends a message, the message broker put it in a queue, the message recipient acquires the content of the message from the queue, after reading the message dequeued
      • News only the only the sender and accepted by, but not to say that only one recipient
    2. Theme (topic): publishing (publish) / subscribe (subscribe) messaging
      • 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
  4. JMS (the Java the Message Service) JAVA Message Service:

    • JVM specification is based on the message broker. ActiveMQ, HornetMQ is JMS implementation
  5. AMQP(Advanced Message Queuing Protocol)

    • Advanced Message Queuing Protocol, is a message broker specification compatible JMS
    • RabbitMQ AMQP is to achieve a

      ETC. AMQP
      definition Java fire Line-level network protocol
      Cross-language no Yes
      Cross-platform no Yes
      Model Message provides two models: (1), Peer-2-Peer (2), Pub / sub Provides five message model: (1), direct exchange (2), fanout exchange (3), topic change (4), headers exchange (5), system exchange essence, 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 Various message types: TextMessage MapMessage BytesMessage StreamMessage ObjectMessage Message (only the message header and properties) byte [] When practical 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.
  6. Spring support

    • spring-jms provides JMS support

    • spring-rabbit provides AMQP support

    • You need ConnectionFactory implementations to connect to the message broker

    • Providing the JmsTemplate , RabbitTemplate to send a message

    • @JmsListener ( JMS ), @RabbitListener ( AMQP ) message broker comment listens for messages posted on the method

    • @EnableJms , @EnableRabbit open support

  7. Spring Boot auto-configuration

    • JmsAutoConfiguration

    • RabbitAutoConfiguration

Three, RabbitMQ Profile

1, the basic concept

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

  1. 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 messages) , MODE-Delivery ** (indicates that the message It may require persistent storage) ** and so on.
  2. Publisher

    • News producer , is also a client application to publish messages to the switch .
  3. 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
  4. 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.
  5. Binding

    • Binding , for association between a 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 relationships.

  6. Connection

    • Network connection , such as a TCP connection.
  7. 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.
  8. Consumer

    • Consumers message indicating a get messages from the message queue, the client application.
  9. 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 /.
  10. Broker

    • Message Queue server entity represents

2, operation process

Here Insert Picture Description
process:

  1. Post Owner ( Publisher ) sends a message (message) to the message broker, that is, the message server ( Broker ).
  2. A server inside vhost called web hosting , web hosting which has many of its own switch (the Exchange) , queue (Queue) .
  3. Message to the virtual host (Vhost) , to the designated virtual host exchanger (exchange).
  4. Exchanger (Exchange) message according to the routing key ( routing-Key in the end take) is determined message routed to the message queue ** (Queue) ** inside, routing rules by binding relationship ( the Binding ) represented. When the message reaches the message queue, the consumer ** (Consumer) ** the message can be removed from the message queue.
  5. Consumers message queue and a connection is established (connection), to establish a connection in order to save resources, multiplexing, open up a lot in each TCP connection inside the pipe (Channel) , which exchanges data in the pipeline, to get the message from the queue data is returned to the consumer (consumer) through the pipeline

3, exchange type

Exchange have time to distribute 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
    Here Insert Picture Description

Message routing keys (routing key), and if the binding key Binding consistent , the switch will send a message to a corresponding queue. Routing keys with the name of the queue exact match .

  1. fanout

Here Insert Picture Description

Each hair switch types to fanout messages are assigned to all queues bound up. fanout switch does not process routing keys , but simply binds to switch queues , each of the messages sent to the switch are 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 forwarding the message type is the fastest .

  1. Topic Topic switch allocation message match routing key attributes by mode, the routing keys and a pattern matching , the case needs to bind to a queue mode. It will cut the string and binding routing keys into key words, which are separated by points between words . It will also identify two wildcards: "#" symbol and the symbol " " . # Match 0 or more words ** **** match a word.
    Here Insert Picture Description

Four, Rabbit MQ integration

1, the installation and start RabbitMQ

  1. Use docker within a virtual machine installed RabbitMQ

    docker pull rabbitmq:3.8-management
    
  2. start up

    docker run -d -p 5672:5672 -p 15672:15672 --name myrabbismq rabbitmq的id
    
  3. View Run

    docker ps
    

2, switches and add the message queue according to the configuration requirements

Here Insert Picture Description

Reference is still Silicon Valley Video Configuration

3, the integration of RabbitMQ

Add pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
  1. Unicast achieve
/**
* @Description: 1、单播(点对点)
* @param:
* @return:
* @auther: zqq
* @date: 19/12/23 15:24
*/
@Test
void contextLoads() {
    //rabbitTemplate.send(exchange,routingKey,message);
    //Message需要自己后制造一个,定义消息体内容和消息头

    //object默认当成消息体,只需要传入要发送的对象,自动序列化发送给rabbit
    //rabbitTemplate.convertAndSend(exchange,routeKey,Object);

    HashMap<String, Object> map = new HashMap<>();
    map.put("msg","这是第一个消息");
    map.put("data", Arrays.asList("helloworld",123,true));
    //对象被默认序列化后发送出去
    rabbitTemplate.convertAndSend("exchange.direct","atguigu.news",map);
}

//接收
@Test
public void receive(){
    Object o = rabbitTemplate.receiveAndConvert("atguigu.news");
    System.out.println("o.getClass() = " + o.getClass());
    System.out.println(o);
}
  1. Broadcast achieve
/**
* @Description: 广播
* @param:
* @return:
* @auther: zqq
* @date: 19/12/23 16:58
*/
@Test
public void sendMsg(){
    rabbitTemplate.convertAndSend("exchange.fanout","",new Book("西游记","吴承恩"));
}

3. Receive a message queue

/*
*@EnableRabbit + @RabbitListtener监听消息队列
*/
@Service
public class BookService {

    @RabbitListener(queues = "atguigu.news")
    public void receive(Book book){
        System.out.println("book = " + book);
    }

    @RabbitListener(queues = "atguigu")
    public void receive2(Message message){
        System.out.println(message.getBody());
        System.out.println(message.getMessageProperties());
    }
}
  1. Messageconvter using custom serialization json
@Configuration
public class MyAMQPConfig {

    /**
     * @Description: 自定义messageconvter
     * @param: 
     * @return: 
     * @auther: zqq
     * @date: 19/12/23 17:01
     */
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }
}
  1. AmqpAdmin create and delete Queue, exchange, Binding
/*
*AmqpAdmin:RabbitMQ系统功能组件
*    AmqpAdmin  : 创建和删除Queue,exchange,Binding
*/

@Test
public void createExchange(){
    //创建一个exchange
    amqpAdmin.declareExchange(new DirectExchange("amqpadmin.exchange"));
    //创建一个消息队列
    amqpAdmin.declareQueue(new Queue("amqpadmin.queue",true));
    //绑定
    amqpAdmin.declareBinding(new Binding("amqpadmin.queue", Binding.DestinationType.QUEUE,"amqpadmin.exchange",
                                         "amqp.haha",null));
}

dmin.declareExchange (new new DirectExchange ( "amqpadmin.exchange"));
// create a message queue
amqpAdmin.declareQueue (new new Queue ( "amqpadmin.queue", to true));
// Bind
amqpAdmin.declareBinding (new Binding ( " amqpadmin.queue ", Binding.DestinationType.QUEUE," amqpadmin.exchange ",
" amqp.haha ", null));
}


Published 26 original articles · won praise 27 · views 6854

Guess you like

Origin blog.csdn.net/qq_40705355/article/details/103672023