RabbitMQ(Docker 单机部署)

序言

本文给大家介绍如何使用 Docker 单机部署 RabbitMQ 并与 SpringBoot 整合使用。

一、部署流程

  1. 拉取镜像

    shell 复制代码
    docker pull rabbitmq:3-management
  2. 镜像拉取成功之后使用下面命令启动 rabbitmq 容器

    shell 复制代码
    docker run \
    	# 指定用户名
     -e RABBITMQ_DEFAULT_USER=username \
     # 指定密码
     -e RABBITMQ_DEFAULT_PASS=password \
     # 指定容器名称
     --name mq \
     # 指定主机名称(任意)
     --hostname mq1 \
     # WEB 浏览器访问端口
     -p 15672:15672 \
     # AMQP 协议端口
     -p 5672:5672 \
     # 后台启动
     -d \
     # 引用的镜像
     rabbitmq:3-management
  3. 打开浏览器输入网址 localhost:15672 进入 RabbitMQ 管理页面

二、SpringBoot 整合 RabbitMQ

2.1 使用 RabbitMQ 客户端

  1. 引入依赖

    xml 复制代码
    <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.7.6</version>
    </parent>
    
    <!-- amqp 依赖-->
    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
      </dependency>
    </dependencies>
  2. 使用案例

    java 复制代码
    public void static main() throws IOException, TimeoutException {
        // 配置连接
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("xxx.xxx.xxx.xxx");
        factory.setPort(5672);
        factory.setVirtualHost("/");
        factory.setUsername("rabbit");
        factory.setPassword("rabbit");
    
        // 获取连接对象
        Connection connection = factory.newConnection();
    
        // 获取频道
        Channel channel = connection.createChannel();
    
        // 创建队列
        String queueName = "simple.queue";
        channel.queueDeclare(queueName, false, false, false, null);
    
        // 订阅消息
        channel.basicConsume(queueName, true, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag,
                                       Envelope envelope,
                                       AMQP.BasicProperties properties,
                                       byte[] body) throws IOException {
                String msg = new String(body);
                System.out.println(msg);
            }
        });
    }

2.2 使用 SpringAMQP

  1. 引入依赖

    xml 复制代码
    <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.7.6</version>
    </parent>
    
    <!-- amqp 依赖-->
    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
      </dependency>
    </dependencies>
  2. application.yml 文件中编写配置

    yaml 复制代码
    spring:
      rabbitmq:
        host: xxx.xxx.xxx.xxx
        port: 5672
        virtual-host: /
        username: rabbit
        password: rabbit
  3. 监听消息

    java 复制代码
    @Component
    public class SpringRabbitListener {
    
        @RabbitListener(queues = "simple.queue")
        public void listenMessage(String msg) {
            System.out.println(msg);
        }
    }
  4. 发送消息

    java 复制代码
    @Resource
    private RabbitTemplate rabbitTemplate;
    
    @Test
    public void sendTest() {
        // 需要发送的队列
        String queueName = "simple.queue";
        // 需要发送的消息
        String msg = "This is a cool message!";
        // 发送消息
        rabbitTemplate.convertAndSend(queueName, msg);
    }

推荐阅读

  1. 深入了解 Arthas:Java 应用程序诊断利器
  2. 基于 AI 的数据库助手-Chat2DB
  3. EasyExcel 处理 Excel
  4. 实体映射解决方案-MapStruct
  5. 动态切换数据源的最佳实践
相关推荐
java资料站41 分钟前
Docker 快速部署 MySQL 主从复制(一主一从)
mysql·adb·docker
Alex艾力的IT数字空间4 小时前
在 Kylin(麒麟)操作系统上搭建 Docker 环境
大数据·运维·缓存·docker·容器·负载均衡·kylin
伴我与影10 小时前
【记录】复现论文 Dftpav
c++·docker
SPC的存折11 小时前
2、Docker命令与镜像、容器管理
linux·运维·服务器·docker·容器·eureka
香蕉鼠片13 小时前
Docker
运维·docker·容器
Cat_Rocky15 小时前
docker简单学习
学习·docker·容器
zjeweler15 小时前
linux服务器部署openclaw最新最细教程(非docker版)
linux·服务器·docker·openclaw
杨浦老苏16 小时前
开源自主AI智能体助手Frona
人工智能·docker·ai·群晖
x10n916 小时前
基于提示词驱动的Function Call实现K8s Pod智能诊断
ai·云原生·容器·kubernetes
东北甜妹17 小时前
Docker 基础
linux·docker·开源