一、下载镜像

docker pull rabbitmq:3.8-management

二、启动容器

应用访问端口5672,web访问端口15672

version: '3'
services:
  rabbitmq:
    image: rabbitmq:3.8-management
    container_name: rabbitmq
    restart: always
    hostname: rabbitmq
    ports:
      - 5672:5672
      - 15672:15672
    volumes:
      - /home/qiang/data/rabbitmq:/var/lib/rabbitmq
    environment:
      - RABBITMQ_DEFAULT_USER=root
      - RABBITMQ_DEFAULT_PASS=123456
mkdir -p /home/qiang/data/rabbitmq/conf/
docker cp rabbitmq:/etc/rabbitmq/rabbitmq.conf /home/qiang/data/rabbitmq/conf/rabbitmq.conf

rabbitmq.conf

loopback_users.guest = false
listeners.tcp.default = 5672
default_pass = SjHlvlQVevNU0x8G
default_user = rabbitmq
management.tcp.port = 15672
management.path_prefix = /rabbitmq
version: '3'
services:
  rabbitmq:
    image: rabbitmq:3.8-management
    container_name: rabbitmq
    restart: always
    hostname: rabbitmq
    ports:
      - 5672:5672
      - 15672:15672
    volumes:
      - /home/qiang/data/rabbitmq/data:/var/lib/rabbitmq
      - /home/qiang/data/rabbitmq/conf/rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf

注意:

  1. application.yml文件中推荐为docker容器设置hostname,因为rabbitmq默认使用hostname作为存储数据的节点名,设置hostname可以避免生成随机的节点名,方便追踪数据。
  2. RABBITMQ_DEFAULT_USER 和 RABBITMQ_DEFAULT_PASS 用来设置超级管理员的账号和密码,如果不设置,默认都是 guest。
  3. docker镜像使用带有后缀-management的镜像,就可以访问页面,否则不能访问。

image-20220223000223482

三、整合应用

pom.xml

<!-- rabbitmq依赖-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

application.yml

spring:
  application:
    name: rabbitmq-service
  rabbitmq:
    host: 192.168.1.51
    port: 5627
    username: root
    password: 123456