头图

Overview

Today I would like to share with you the following three parts:

  • Docker install RabbitMQ
  • Spring Boot and RabbitMQ demo
  • RabbitMQ delay queue

Docker install RabbitMQ

Step 1: Pull the image

 docker pull rabbitmq:management

Step 2: Start

 docker run -d \
--name rabbitmq \
-p 5672:5672 \
-p 15672:15672 \
-v /data/rabbitmq:/var/lib/rabbitmq \
--hostname myRabbit \
-e RABBITMQ_DEFAULT_VHOST=my_vhost \
-e RABBITMQ_DEFAULT_USER=admin \
-e RABBITMQ_DEFAULT_PASS=admin \
rabbitmq:management

illustrate:

-d run the container in the background;

--name specifies the container name;

-p specifies the port on which the service runs (5672: application access port; 15672: console web port number);

-v map directory or file;

--hostname hostname (an important note of RabbitMQ is that it stores data based on the so-called "nodename", which defaults to the hostname);

-e specifies environment variables; (RABBITMQ_DEFAULT_VHOST: default virtual machine name; RABBITMQ_DEFAULT_USER: default user name; RABBITMQ_DEFAULT_PASS: default user name password)

Spring Boot and RabbitMQ demo

rely

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

configure

 spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: admin
    password: admin
    virtual-host: my_vhost

configure

RabbitMQ Config

send

send

take over

receive

test

GET http://localhost:8080/sendMsg?key=item.test&msg=Hello

RabbitMQ delay queue

First look at a mistake:

channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no

The reason is that the delay queue is a plugin of RabbitMQ, which is not installed by default and needs to be installed.

Related Links:

Install the plugin:

  • 1. Download the delay queue plugin
  • 2. Copy the plugin to the /plugins directory of RabbitMQ. Copy method: docker cp rabbitmq_delayed_message_exchange-3.10.2.ez containerId:/plugins
  • 3. Execute, execute in the /plugins directory: rabbitmq-plugins enable rabbitmq_delayed_message_exchange
  • 4. Restart RabbitMQ

Configuration:

delay config

send

delay send

Receive message:

delay receive

test:

GET http://localhost:8080/sendDelayMsg/10000/iPhone13


冯文议
183 声望20 粉丝

软件开发工程师,专注于程序设计与开发。