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. 动态切换数据源的最佳实践
相关推荐
辉的技术笔记11 小时前
Dify 自部署为什么跑不动?6 层瓶颈诊断法教你定位
docker
止语Lab1 天前
一次 goroutine 泄漏:pprof 说有 10 万个 goroutine,但问题不在 channel
rabbitmq
程序员老赵1 天前
Docker 部署 Redmine:老牌开源项目管理部署实测记录
docker·开源·团队管理
程序员老赵1 天前
服务器文件不想 SFTP 上传?Docker 跑个 File Browser,浏览器就能管理
服务器·docker·开源
lichenyang4534 天前
Docker 学习笔记(五):Docker Compose,用一个 YAML 启动前端、后端和 MongoDB
docker
lichenyang4534 天前
Docker 学习笔记(四):Dockerfile,把项目打成自己的镜像
docker·容器
lichenyang4534 天前
Docker 学习笔记(三):Docker 网络、bridge、子网和容器互通
docker·容器
lichenyang4534 天前
Docker 学习笔记(二):docker run 的参数到底在控制什么?
docker·容器
Patrick_Wilson8 天前
从「改个端口」到 502:Next.js on k8s 的容器端口、Service 映射与 env 覆盖
docker·kubernetes·next.js
Suroy9 天前
DockerView-Go:用 Go 写一个终端 Docker 监控工具,顺便做了个 Web 仪表盘
docker