This is the 8th day of my participation in Gwen Challenge

Basic concepts of AMQP

AMQP (Advanced Message Queuing Protocol) is an application-layer standard that provides unified messaging services. It is an open application-layer Protocol designed for message-oriented middleware. AMQP (RabbitMQ) must have three parts: switch, queue and binding

Message: Consists of playloads and labels. Where the payload transmits both data. Producer: Creates messages and publishes them to the Message Broker. Message Broker: An application that receives and distributes messages. RabbitMQ Server is a Message Broker. There are many concepts involved. Channel, queue, Exchange, routing key, binding key, vhost, etc.

RabbitMQ is an open source message broker and queue server that implements the AMQP protocol standard.

Rabbitmq brief introduction

Message middleware

Producer: sends messages Switch: receives messages bound to a specified queue Queue: stores messages, fifO consumers: Listens on the specified message queue, receives the message, and processes it

There are four Exchange types: Direct: the default mode. The simplest mode specifies a BindingKey when creating message queues. When the sender sends a message, specify the corresponding Key. When the Key matches the BindingKey of the message queue, the message will be sent to the message queue.

Topic: The forwarding information is based on the wildcard character, and the binding between queues and switches is based on the wildcard character + string. When sending messages, messages are sent to the message queue only when the specified key matches the mode.

Headers: A set of key-value rules is specified when the message queue is bound to the switch, and a set of key-value rules is specified when the message is sent. When the two sets of key-value rules match, the message is sent to the matching message queue.

Fanout: A broadcast route that sends messages to all queues bound to it. Even if keys and rules are set, messages are ignored.

Easiest integration with SpringBoot

  1. Rely on
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
Copy the code
  1. configuration
spring:
  data:
    neo4j:
      uri: Bolt: / / 192.168.1.41:7687
      username: neo4j
      password: 12345

  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

Copy the code
@Configuration
public class RabbitConfig {

    @Bean
    public Queue helloQueue(a) {
    // Define a Hello queue. It can have four parameters: queue name, durable Message queue Default true, auto-delete Message queue automatically deleted when it is not in use, durable message queue default true, auto-delete Message queue automatically deleted when it is not in use, durable message queue default true The default value is false. 4. Whether exclusive message queues apply only to current Connnection
        return new Queue("hello"); }}Copy the code
  1. use

Send a message

@Component
public class Sender {
    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void send(a) {
        String context = "hello " + new Date();
        System.out.println("Sender : " + context);
        this.rabbitTemplate.convertAndSend("hello", context); }}Copy the code

Receives the message

@component @rabbitListener (queues = "hello") // @rabbitlistener (queues = "#{xx}") @RabbitHandler public void process(String hello) { System.out.println("Receiver : " + hello ); }}Copy the code