docker-compose安装rocketmq

1. 创建RocketMq文件目录

sh 复制代码
mkdir rocketmq
mkdir -p rocketmq/brokerconf  rocketmq/logs rocketmq/store

2.创建broker.conf配置文件

sh 复制代码
vim  brokerconf/broker.conf
conf 复制代码
# 集群名称
brokerClusterName = DefaultCluster
# broker 名字
brokerName = broker-a
# 0表示master,>0 表示slave
brokerId = 0
# 删除文件的时间点
deleteWhen = 04
# 文件保留时间
fileReservedTime = 48
# Broker 的角色
# # - ASYNC_MASTER 异步复制Master
# # - SYNC_MASTER 同步双写Master
# # - SLAVE
brokerRole = ASYNC_MASTER
# 刷盘方式
# # - ASYNC_FLUSH 异步刷盘
# # - SYNC_FLUSH 同步刷盘
flushDiskType = ASYNC_FLUSH

# nameserver地址
namesrvAddr=60.204.149.224:9876
brokerIP1=60.204.149.224

3.创建docker-compose.yml文件

yml 复制代码
version: '3.8'
services:
  namesrv:
    image: apache/rocketmq:5.3.1
    container_name: rmqnamesrv
    ports:
      - 9876:9876
    volumes:
      - /root/docker/rocketmq/logs:/opt/logs
      - /root/docker/rocketmq//store:/opt/store
    networks:
      - rocketmq
    command: sh mqnamesrv
  broker:
    image: apache/rocketmq:5.3.1
    container_name: rmqbroker
    ports:
      - 10909:10909
      - 10911:10911
      - 10912:10912
    volumes:
      - /root/docker/rocketmq/logs:/opt/logs
      - /root/docker/rocketmq/store:/opt/store
      - /root/docker/rocketmq/brokerconf/broker.conf:/etc/rocketmq/broker.conf
    environment:
      - NAMESRV_ADDR=rmqnamesrv:9876
      - JAVA_OPTS=" -Duser.home=/opt"
    depends_on:
      - namesrv
    networks:
      - rocketmq
    command: sh mqbroker -c /etc/rocketmq/broker.conf
  proxy:
    image: apache/rocketmq:5.3.1
    container_name: rmqproxy
    networks:
      - rocketmq
    depends_on:
      - broker
      - namesrv
    ports:
      - 8080:8080
      - 8081:8081
    restart: on-failure
    environment:
      - NAMESRV_ADDR=rmqnamesrv:9876
    command: sh mqproxy
networks:
  rocketmq:
    driver: bridge

4.启动rocketmq

sh 复制代码
 docker-compose -f docker-compose.yml up -d

5.关闭rocketmq

sh 复制代码
docker-compose down

6.测试

6.1 消息生产者
java 复制代码
import static com.doudou.mq.MqConfig.NAME_SRV_ADDR;
import static com.doudou.mq.MqConfig.topic;

import java.nio.charset.StandardCharsets;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

public class Producer {

    public static void main(String[] args) throws MQClientException, InterruptedException {
        // 初始化消息生产者
        DefaultMQProducer producer = new DefaultMQProducer("DemoProducer");
        // 设置超时时间
        producer.setSendMsgTimeout(10000);
        // 指定nameserver地址
        producer.setNamesrvAddr(NAME_SRV_ADDR);
        // 启动消息生产者服务
        producer.start();

        for (int i = 0; i < 10; i++) {
            try {
                Message message = new Message(topic, ("Hello World" + i).getBytes(StandardCharsets.UTF_8));
                SendResult result = producer.send(message);
                System.out.printf("%s%n", result);
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
                Thread.sleep(1000);
            }
        }
        producer.shutdown();
    }
}
6.2 消息消费者
java 复制代码
import static com.doudou.mq.MqConfig.NAME_SRV_ADDR;
import static com.doudou.mq.MqConfig.topic;

import java.nio.charset.StandardCharsets;
import java.util.List;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.MessageExt;

public class Consumer {

    public static void main(String[] args) throws MQClientException {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("DemoConsumer");
        consumer.setNamesrvAddr(NAME_SRV_ADDR);
        consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
        consumer.subscribe(topic, "*");
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list,
                    ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                for (MessageExt messageExt : list) {
                    System.out.println(new String(messageExt.getBody(), StandardCharsets.UTF_8));
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        consumer.start();
        System.out.println("Consumer Started");
    }

}
6.3 依赖
xml 复制代码
<dependency>
    <groupId>org.apache.rocketmq</groupId>
     <artifactId>rocketmq-client</artifactId>
     <version>5.3.0</version>
 </dependency>
相关推荐
TsengOnce24 分钟前
Docker 安装 sentinel
docker·容器·sentinel
XMYX-028 分钟前
使用 Docker Compose 部署 Redis 主从与 Sentinel 高可用集群
redis·docker·sentinel
社畜码农且逊1 小时前
Docker Compose实战三:轻松部署PHP
docker·容器·php
weixin_SAG1 小时前
21天掌握javaweb-->第13天:Docker容器化部署与微服务简介
docker·微服务·容器
懒阳羊7 小时前
Docker(一)
docker·云原生·eureka
漫天转悠8 小时前
Docker保存镜像和导入镜像文件(图文详解)
ubuntu·docker
fen_fen9 小时前
Docker如何运行一个python脚本Hello World
运维·docker·容器
檀越剑指大厂9 小时前
【Docker系列】Docker 构建多平台镜像:arm64 架构的实践
docker·容器·架构
阿moments11 小时前
Docker - 速成
运维·docker·云原生·容器
落寞书生11 小时前
docker安装mysql 实现主从同步
运维·mysql·docker·主从同步·docker 安装mysql