Detailed explanation of AMQP protocol and AMQP in JAVA system

Table of contents

1 Overview

1.1. Introduction

1.2. Abstract model

2. amqp in spring

2.1.spring amqp

2.2.spring boot amqp


1 Overview

1.1. Introduction

AMQP, Advanced Message Queuing Protocol, Advanced Message Queuing Protocol.

Introduction on Baidu Encyclopedia:

An application-layer standard advanced message queuing protocol that provides unified messaging services is an open standard for application-layer protocols designed for message-oriented middleware. The client and the message middleware based on this protocol can transmit messages, and are not restricted by different products of the client middleware, different development languages ​​and other conditions.

Translating the above words into human language, AMQP is a protocol, and its core content is to propose an abstract model for message middleware, which specifies which entities should be composed of message middleware. Currently, there are various message middlewares on the market, and their architectures are also varied. AMQP actually hopes to provide a unified standard for message middleware. Using uniform standard message middleware is easier to manage. Even if there are multiple message middleware in a large system, because the entities in the architecture are the same, a set of unified API can be abstracted for operation.

1.2. Abstract model

The abstract model defined by AMQP is as follows:

  • Broker (message broker)

  • Producer

  • Consumer

  • Exchange

  • Queue

  • Binding

  • Connection

  • Channel

Broker (message broker)

It can be understood as an MQ node. Message brokers in AMQP are the core component of messaging. It is responsible for receiving, storing and delivering messages, and routing them to the correct destination. There can be multiple message brokers to form a message broker cluster for distributed and highly available message delivery.

Producer

The producer is the sender of the message, which is responsible for creating and sending the message to the message broker. The producer does not need to care about the specific routing of the message, it only needs to send the message to the specified exchange.

Consumer

The consumer is the receiver of the message, it subscribes to the message of interest, receives and processes the message from the message broker. Consumers can subscribe to one or more queues to receive eligible messages.

Exchange

An exchange is a message router that receives messages sent from producers and routes them into one or more queues based on the message's routing key. Exchanges send messages to different queues according to different routing strategies.

Queue

A queue is a storage location for messages, and it holds messages to be consumed. After the message agent sends the message to the queue, it waits for the consumer to take the message from the queue for processing.

Binding

A binding is an association between an exchange and a queue. Through binding, exchanges route messages to queues so that messages sent by producers can be received by consumers.

Connection

A connection is a physical connection between a client and a message broker. Clients use connections to communicate with message brokers, sending and receiving messages.

Channel

A channel is a virtual connection within an AMQP connection used for communication between clients and message brokers. Channels allow clients to create and use exchanges, queues, bindings, and send and receive messages without creating a new TCP connection for each communication.

2. amqp in spring

2.1.spring amqp

As a "glue" of a java backend, spring provides its own support for various JAVA EE scenarios, such as Spring Data for accessing databases, Spring Security for security, etc. Of course, there is also spring for accessing MQ amqp, as the name implies, spring amqp is the default support provided by Spring for operating MQ that meets the amqp protocol standard.

Spring AMQP provides a simple yet powerful abstraction layer that makes it easier to use message queues in Spring applications. Its main goal is to provide a unified API so that developers can easily interact with different message queue systems without paying attention to the underlying implementation details.

Key Features and Functions:

  1. Connection management and resource abstraction : Spring AMQP manages connections to message brokers and provides a set of abstract classes and interfaces to manage messaging resources such as exchanges, queues, bindings, etc.

  2. Message listening container : Spring AMQP provides a message listening container for registering message listeners in applications and processing messages received from message queues.

  3. Message conversion : Spring AMQP supports message conversion, making it easier to convert messages from Java objects to the format required by the message queue, such as JSON or bytes.

  4. Transaction support : Spring AMQP allows messaging operations to be used in conjunction with Spring's declarative transaction management, ensuring reliable delivery and processing of messages.

  5. Message sending and receiving : Spring AMQP provides an API for sending and receiving messages, making it simple and flexible to send and receive messages in applications.

  6. Asynchronous processing : Spring AMQP supports asynchronous message processing, enabling applications to process large numbers of messages more efficiently.

  7. Integration with the Spring ecosystem : Spring AMQP is tightly integrated with other Spring projects, such as Spring Boot and Spring Integration, making it easier to build distributed and message-driven applications in the Spring ecosystem.

The use of spring amqp is described in detailed official documents on the official website, which will not be explained here.

It should be noted that Spring AMQP is specially designed to support message queuing systems that conform to the AMQP protocol, such as RabbitMQ. If you want to interact with a message queuing system (such as Kafka, RocketMQ) that does not conform to the AMQP protocol, you need to use their official or community-provided self-implemented Spring integration library.

2.2.spring boot amqp

Now for development, of course we use more spring boot. In fact, the underlying package is spring amqp. Here is an example of using spring-boot-starter-amqp to operate rabbitmq. For more detailed content, if you are interested, you can move to another article of the blogger, which has a detailed introduction:

Six modes of SpringBoot RabbitMq_springboot integrates six modes of rabbitmq_BugMan's Blog-CSDN Blog

Here we take the routing mode of RabbitMQ as an example:

The routing mode of RabbitMQ is the publish-subscribe mode. Different messages are delivered to different queues through the routing key, and consumers can find different queues according to the routing key they want to subscribe to.

 

rely:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

RabbitMQ configuration:

spring:
  rabbitmq:
    host: 192.168.31.10
    port: 5672 #You can view it through the console
    username: guest
    password: guest
    virtual-host: /vhost_sys_logs #It can not be configured, and the default virtual-host will be used

Configuration class:

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
@Configuration
public class RabbitMQConfig {
    @Bean
    public Queue queue_01() {
        //durable,是否开启持久化
        return new Queue("queue_01",false);
    }
    @Bean
    public Queue queue_02(){
        return new Queue("queue_02",false);
    }
 
    //路由模式的交换机
    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange("direct_exchange",false,false);
    }
 
    //将队列绑定到交换机上
    @Bean
    public Binding bindingSmsQueue_01(@Qualifier("queue_01") Queue logsAccess, DirectExchange directExchange) {
        return BindingBuilder.bind(logsAccess).to(directExchange).with("routing_key_01");
    }
    @Bean
    public Binding bindingSmsQueue_02(@Qualifier("queue_02") Queue logsError, DirectExchange directExchange) {
        return BindingBuilder.bind(logsError).to(directExchange).with("routing_key_02");
    }
}

Producer:

@SpringBootTest(classes=PrivilegeSystemMain.class)
public class RabbitMQTest {
    @Autowired
    private RabbitTemplate rabbitTemplate;
 
    @Test
    public void simpleTest() {
        rabbitTemplate.convertAndSend("direct_exchange","routing_key_01","helo world!");
    }
}

consumer:

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
 
@Component
@Slf4j
public class ConsumeBean {
    @RabbitListener(queues={"queue_01"})
    public void consumer_01(String message){
        log.info("consumer_01 get message "+message);
    }
    @RabbitListener(queues={"queue_02"})
    public void consumer_02(String message){
        log.info("consumer_02 get message "+message);
    }
}

 

Guess you like

Origin blog.csdn.net/Joker_ZJN/article/details/132037929