目录

RocketMQ docker-compose部署 4主4从集群

RocketMQ docker-compose 部署 4 主 4 从集群

V 4.8.0
采用4主4从同步模式。HA 实现上采用Master/Slave+Failover组件方式 每台主机运行三个容器,分别为NameServerBrokerMasterSlaveMaster,每个 Master 和 Slave 分别存放在不同的机器上

架构

/images/rocketmq.svg

IP 角色 服务
193.0.40.172 NameServer -
193.0.40.172 BrokerMaster broker-a
193.0.40.172 SlaveMaster broker-d-s
193.0.40.172 BrokerMaster broker-b
193.0.40.172 SlaveMaster broker-a-s
193.0.40.172 BrokerMaster broker-c
193.0.40.172 SlaveMaster broker-b-s
193.0.40.172 BrokerMaster broker-d
193.0.40.172 SlaveMaster broker-c-s

部署

安装 docker-compose

1
2
3
4
5
6
#!/bin/bash
# 下载安装 v2.4.1 docker-compose
curl -L https://get.daocloud.io/docker/compose/releases/download/v2.4.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
docker-compose --version

执行 docker-compose --version 查看是否安装成功

生成配置文件

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/bin/bash
#docker-compose 生成配置文件

mkdir -p /rocketmq/data/namesv1
mkdir -p /rocketmq/logs/namesv1
mkdir -p /rocketmq/data/namesv2
mkdir -p /rocketmq/logs/namesv2
mkdir -p /rocketmq/config/broker-a
mkdir -p /rocketmq/data/broker-a
mkdir -p /rocketmq/logs/broker-a
mkdir -p /rocketmq/config/broker-a-s
mkdir -p /rocketmq/data/broker-a-s
mkdir -p /rocketmq/logs/broker-a-s
mkdir -p /rocketmq/config/broker-b
mkdir -p /rocketmq/data/broker-b
mkdir -p /rocketmq/logs/broker-b
mkdir -p /rocketmq/config/broker-b-s
mkdir -p /rocketmq/data/broker-b-s
mkdir -p /rocketmq/logs/broker-b-s
mkdir -p /rocketmq/config/broker-c
mkdir -p /rocketmq/data/broker-c
mkdir -p /rocketmq/logs/broker-c
mkdir -p /rocketmq/config/broker-c-s
mkdir -p /rocketmq/data/broker-c-s
mkdir -p /rocketmq/logs/broker-c-s
mkdir -p /rocketmq/config/broker-d
mkdir -p /rocketmq/data/broker-d
mkdir -p /rocketmq/logs/broker-d
mkdir -p /rocketmq/config/broker-d-s
mkdir -p /rocketmq/data/broker-d-s
mkdir -p /rocketmq/logs/broker-d-s
cd /rocketmq/config/broker-a
cat > broker-a.conf <<EOF
#集群名称
brokerClusterName=DefaultCluster
#broker名称
brokerName=broker-a
#brokerId master用0 slave用其他
brokerId=0
#清理时机
deleteWhen=4
#文件保留时长 48小时
fileReservedTime=48
#broker角色 -ASYNC_MASTER异步复制 -SYNC_MASTER同步双写 -SLAVE
brokerRole=SYNC_MASTER
#刷盘策略 - ASYNC_FLUSH 异步刷盘 - SYNC_FLUSH 同步刷盘
flushDiskType=SYNC_FLUSH
#主机ip
brokerIP1=193.0.40.172
#对外服务的监听接口,同一台机器上部署多个broker,端口号要不相同
listenPort=10911
#namesvr
namesrvAddr=193.0.40.172:9876;193.0.40.172:9877
#是否能够自动创建topic
autoCreateTopicEnable=true
EOF
# 生成配置文件
cd /rocketmq/config/broker-a-s
cat > broker-a-s.conf <<EOF
#集群名称
brokerClusterName=DefaultCluster
#broker名称
brokerName=broker-a
#brokerId master用0 slave用其他
brokerId=1
#清理时机
deleteWhen=4
#文件保留时长 48小时
fileReservedTime=48
#broker角色 -ASYNC_MASTER异步复制 -SYNC_MASTER同步双写 -SLAVE
brokerRole=SLAVE
#刷盘策略 - ASYNC_FLUSH 异步刷盘 - SYNC_FLUSH 同步刷盘
flushDiskType=SYNC_FLUSH
#主机ip
brokerIP1=193.0.40.172
#对外服务的监听接口,同一台机器上部署多个broker,端口号要不相同
listenPort=11911
#namesrv
namesrvAddr=193.0.40.172:9876;193.0.40.172:9877
#是否能够自动创建topic
autoCreateTopicEnable=true
EOF
cd /rocketmq/config/broker-b
cat > broker-b.conf <<EOF
#集群名称
brokerClusterName=DefaultCluster
#broker名称
brokerName=broker-b
#brokerId master用0 slave用其他
brokerId=0
#清理时机
deleteWhen=4
#文件保留时长 48小时
fileReservedTime=48
#broker角色 -ASYNC_MASTER异步复制 -SYNC_MASTER同步双写 -SLAVE
brokerRole=SYNC_MASTER
#刷盘策略 - ASYNC_FLUSH 异步刷盘 - SYNC_FLUSH 同步刷盘
flushDiskType=SYNC_FLUSH
#主机ip
brokerIP1=193.0.40.172
#对外服务的监听接口,同一台机器上部署多个broker,端口号要不相同
listenPort=12911
#namesrv
namesrvAddr=193.0.40.172:9876;193.0.40.172:9877
#是否能够自动创建topic
autoCreateTopicEnable=true
EOF
cd /rocketmq/config/broker-b-s
cat > broker-b-s.conf <<EOF
#集群名称
brokerClusterName=DefaultCluster
#broker名称
brokerName=broker-b
#brokerId master用0 slave用其他
brokerId=1
#清理时机
deleteWhen=4
#文件保留时长 48小时
fileReservedTime=48
#broker角色 -ASYNC_MASTER异步复制 -SYNC_MASTER同步双写 -SLAVE
brokerRole=SLAVE
#刷盘策略 - ASYNC_FLUSH 异步刷盘 - SYNC_FLUSH 同步刷盘
flushDiskType=SYNC_FLUSH
#主机ip
brokerIP1=193.0.40.172
#对外服务的监听接口,同一台机器上部署多个broker,端口号要不相同
listenPort=13911
#namesrv
namesrvAddr=193.0.40.172:9876;193.0.40.172:9877
#是否能够自动创建topic
autoCreateTopicEnable=true
EOF
cd /rocketmq/config/broker-c
cat > broker-c.conf <<EOF
#集群名称
brokerClusterName=DefaultCluster
#broker名称
brokerName=broker-c
#brokerId master用0 slave用其他
brokerId=0
#清理时机
deleteWhen=4
#文件保留时长 48小时
fileReservedTime=48
#broker角色 -ASYNC_MASTER异步复制 -SYNC_MASTER同步双写 -SLAVE
brokerRole=SYNC_MASTER
#刷盘策略 - ASYNC_FLUSH 异步刷盘 - SYNC_FLUSH 同步刷盘
flushDiskType=SYNC_FLUSH
#主机ip
brokerIP1=193.0.40.172
#对外服务的监听接口,同一台机器上部署多个broker,端口号要不相同
listenPort=14911
#namesrv
namesrvAddr=193.0.40.172:9876;193.0.40.172:9877
#是否能够自动创建topic
autoCreateTopicEnable=true
EOF
cd /rocketmq/config/broker-c-s
cat > broker-c-s.conf <<EOF
#集群名称
brokerClusterName=DefaultCluster
#broker名称
brokerName=broker-c
#brokerId master用0 slave用其他
brokerId=1
#清理时机
deleteWhen=4
#文件保留时长 48小时
fileReservedTime=48
#broker角色 -ASYNC_MASTER异步复制 -SYNC_MASTER同步双写 -SLAVE
brokerRole=SLAVE
#刷盘策略 - ASYNC_FLUSH 异步刷盘 - SYNC_FLUSH 同步刷盘
flushDiskType=SYNC_FLUSH
#主机ip
brokerIP1=193.0.40.172
#对外服务的监听接口,同一台机器上部署多个broker,端口号要不相同
listenPort=15911
#namesrv
namesrvAddr=193.0.40.172:9876;193.0.40.172:9877
#是否能够自动创建topic
autoCreateTopicEnable=true
EOF
cd /rocketmq/config/broker-d
cat > broker-d.conf <<EOF
#集群名称
brokerClusterName=DefaultCluster
#broker名称
brokerName=broker-d
#brokerId master用0 slave用其他
brokerId=0
#清理时机
deleteWhen=4
#文件保留时长 48小时
fileReservedTime=48
#broker角色 -ASYNC_MASTER异步复制 -SYNC_MASTER同步双写 -SLAVE
brokerRole=SYNC_MASTER
#刷盘策略 - ASYNC_FLUSH 异步刷盘 - SYNC_FLUSH 同步刷盘
flushDiskType=SYNC_FLUSH
#主机ip
brokerIP1=193.0.40.172
#对外服务的监听接口,同一台机器上部署多个broker,端口号要不相同
listenPort=16911
#namesrv
namesrvAddr=193.0.40.172:9876;193.0.40.172:9877
#是否能够自动创建topic
autoCreateTopicEnable=true
EOF
cd /rocketmq/config/broker-d-s
cat > broker-d-s.conf <<EOF
#集群名称
brokerClusterName=DefaultCluster
#broker名称
brokerName=broker-d
#brokerId master用0 slave用其他
brokerId=1
#清理时机
deleteWhen=4
#文件保留时长 48小时
fileReservedTime=48
#broker角色 -ASYNC_MASTER异步复制 -SYNC_MASTER同步双写 -SLAVE
brokerRole=SLAVE
#刷盘策略 - ASYNC_FLUSH 异步刷盘 - SYNC_FLUSH 同步刷盘
flushDiskType=SYNC_FLUSH
#主机ip
brokerIP1=193.0.40.172
#对外服务的监听接口,同一台机器上部署多个broker,端口号要不相同
listenPort=17911
#namesrv
namesrvAddr=193.0.40.172:9876;193.0.40.172:9877
#是否能够自动创建topic
autoCreateTopicEnable=true
EOF

执行rocker-config.sh,确认配置文件是否生成

tree /rocketmq

执行 docker-compose.yaml 文件

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
version: "3"
services:
  rocketmq-namesv1:
    image: apache/rocketmq:4.8.0
    container_name: rocketmq-namesv1
    restart: always
    ports:
      - 9876:9876
    volumes:
      - /rocketmq/logs/namesv1:/home/rocketmq/logs
    environment:
      JAVA_OPT_EXT: -server -Xms256M -Xmx256M -Xmn128m
    command: sh mqnamesrv
    networks:
      rocketmq:
        aliases:
          - rocketmq-namesv1
  rocketmq-namesv2:
    image: apache/rocketmq:4.8.0
    container_name: rocketmq-namesv2
    restart: always
    ports:
      - 9877:9876
    volumes:
      - /rocketmq/logs/namesv2:/home/rocketmq/logs
    environment:
      JAVA_OPT_EXT: -server -Xms256M -Xmx256M -Xmn128m
    command: sh mqnamesrv
    networks:
      rocketmq:
        aliases:
          - rocketmq-namesv2
  broker-a:
    image: apache/rocketmq:4.8.0
    container_name: broker-a
    links:
      - rocketmq-namesv1:rocketmq-namesv1
      - rocketmq-namesv2:rocketmq-namesv2
    ports:
      - 10909:10909
      - 10911:10911
      - 10912:10912
    environment:
      TZ: Asia/Shanghai
      NAMESRV_ADDR: "rocketmq-namesv1:9876"
      JAVA_OPT_EXT: "-server -Xms256M -Xmx256M -Xmn128m"
    volumes:
      - /rocketmq/logs/broker-a:/home/rocketmq/logs
      - /rocketmq/config/broker-a/broker-a.conf:/home/rocketmq/rocketmq-4.8.0/conf/broker.conf
    command: sh mqbroker  -c /home/rocketmq/rocketmq-4.8.0/conf/broker.conf autoCreateTopicEnable=true &
    networks:
      rocketmq:
        aliases:
          - broker-a
  broker-a-s:
    image: apache/rocketmq:4.8.0
    container_name: broker-a-s
    links:
      - rocketmq-namesv1:rocketmq-namesv1
      - rocketmq-namesv2:rocketmq-namesv2
    ports:
      - 11909:10909
      - 11911:11911
      - 11912:10912
    environment:
      TZ: Asia/Shanghai
      NAMESRV_ADDR: "rocketmq-namesv1:9876"
      JAVA_OPT_EXT: "-server -Xms256M -Xmx256M -Xmn128m"
    volumes:
      - /rocketmq/logs/broker-a-s:/home/rocketmq/logs
      - /rocketmq/config/broker-a-s/broker-a-s.conf:/home/rocketmq/rocketmq-4.8.0/conf/broker.conf
    command: sh mqbroker  -c /home/rocketmq/rocketmq-4.8.0/conf/broker.conf autoCreateTopicEnable=true &
    networks:
      rocketmq:
        aliases:
          - broker-a-s
  broker-b:
    image: apache/rocketmq:4.8.0
    container_name: broker-b
    links:
      - rocketmq-namesv1:rocketmq-namesv1
      - rocketmq-namesv2:rocketmq-namesv2
    ports:
      - 12909:10909
      - 12911:12911
      - 12912:10912
    environment:
      TZ: Asia/Shanghai
      NAMESRV_ADDR: "rocketmq-namesv1:9876"
      JAVA_OPT_EXT: "-server -Xms256M -Xmx256M -Xmn128m"
    volumes:
      - /rocketmq/logs/broker-b:/home/rocketmq/logs
      - /rocketmq/config/broker-b/broker-b.conf:/home/rocketmq/rocketmq-4.8.0/conf/broker.conf
    command: sh mqbroker  -c /home/rocketmq/rocketmq-4.8.0/conf/broker.conf autoCreateTopicEnable=true &
    networks:
      rocketmq:
        aliases:
          - broker-b
  broker-b-s:
    image: apache/rocketmq:4.8.0
    container_name: broker-b-s
    links:
      - rocketmq-namesv1:rocketmq-namesv1
      - rocketmq-namesv2:rocketmq-namesv2
    ports:
      - 13909:10909
      - 13911:13911
      - 13912:10912
    environment:
      TZ: Asia/Shanghai
      NAMESRV_ADDR: "rocketmq-namesv1:9876"
      JAVA_OPT_EXT: "-server -Xms256M -Xmx256M -Xmn128m"
    volumes:
      - /rocketmq/logs/broker-b-s:/home/rocketmq/logs
      - /rocketmq/config/broker-b-s/broker-b-s.conf:/home/rocketmq/rocketmq-4.8.0/conf/broker.conf
    command: sh mqbroker  -c /home/rocketmq/rocketmq-4.8.0/conf/broker.conf autoCreateTopicEnable=true &
    networks:
      rocketmq:
        aliases:
          - broker-b-s
  broker-c:
    image: apache/rocketmq:4.8.0
    container_name: broker-c
    links:
      - rocketmq-namesv1:rocketmq-namesv1
      - rocketmq-namesv2:rocketmq-namesv2
    ports:
      - 14909:10909
      - 14911:14911
      - 14912:10912
    environment:
      TZ: Asia/Shanghai
      NAMESRV_ADDR: "rocketmq-namesv1:9876"
      JAVA_OPT_EXT: "-server -Xms256M -Xmx256M -Xmn128m"
    volumes:
      - /rocketmq/logs/broker-c:/home/rocketmq/logs
      - /rocketmq/config/broker-c/broker-c.conf:/home/rocketmq/rocketmq-4.8.0/conf/broker.conf
    command: sh mqbroker  -c /home/rocketmq/rocketmq-4.8.0/conf/broker.conf autoCreateTopicEnable=true &
    networks:
      rocketmq:
        aliases:
          - broker-c
  broker-c-s:
    image: apache/rocketmq:4.8.0
    container_name: broker-c-s
    links:
      - rocketmq-namesv1:rocketmq-namesv1
      - rocketmq-namesv2:rocketmq-namesv2
    ports:
      - 15909:10909
      - 15911:15911
      - 15912:10912
    environment:
      TZ: Asia/Shanghai
      NAMESRV_ADDR: "rocketmq-namesv1:9876"
      JAVA_OPT_EXT: "-server -Xms256M -Xmx256M -Xmn128m"
    volumes:
      - /rocketmq/logs/broker-c-s:/home/rocketmq/logs
      - /rocketmq/config/broker-c-s/broker-c-s.conf:/home/rocketmq/rocketmq-4.8.0/conf/broker.conf
    command: sh mqbroker  -c /home/rocketmq/rocketmq-4.8.0/conf/broker.conf autoCreateTopicEnable=true &
    networks:
      rocketmq:
        aliases:
          - broker-c-s
  broker-d:
    image: apache/rocketmq:4.8.0
    container_name: broker-d
    links:
      - rocketmq-namesv1:rocketmq-namesv1
      - rocketmq-namesv2:rocketmq-namesv2
    ports:
      - 16909:10909
      - 16911:16911
      - 16912:10912
    environment:
      TZ: Asia/Shanghai
      NAMESRV_ADDR: "rocketmq-namesv1:9876"
      JAVA_OPT_EXT: "-server -Xms256M -Xmx256M -Xmn128m"
    volumes:
      - /rocketmq/logs/broker-d:/home/rocketmq/logs
      - /rocketmq/config/broker-d/broker-d.conf:/home/rocketmq/rocketmq-4.8.0/conf/broker.conf
    command: sh mqbroker  -c /home/rocketmq/rocketmq-4.8.0/conf/broker.conf autoCreateTopicEnable=true &
    networks:
      rocketmq:
        aliases:
          - broker-d
  broker-d-s:
    image: apache/rocketmq:4.8.0
    container_name: broker-d-s
    links:
      - rocketmq-namesv1:rocketmq-namesv1
      - rocketmq-namesv2:rocketmq-namesv2
    ports:
      - 17909:10909
      - 17911:17911
      - 17912:10912
    environment:
      TZ: Asia/Shanghai
      NAMESRV_ADDR: "rocketmq-namesv1:9876"
      JAVA_OPT_EXT: "-server -Xms256M -Xmx256M -Xmn128m"
    volumes:
      - /rocketmq/logs/broker-d-s:/home/rocketmq/logs
      - /rocketmq/config/broker-d-s/broker-d-s.conf:/home/rocketmq/rocketmq-4.8.0/conf/broker.conf
    command: sh mqbroker  -c /home/rocketmq/rocketmq-4.8.0/conf/broker.conf autoCreateTopicEnable=true &
    networks:
      rocketmq:
        aliases:
          - broker-d-s
  rocketmq-console:
    image: styletang/rocketmq-console-ng
    container_name: rocketmq-console
    ports:
      - 8090:8080
    environment:
      JAVA_OPTS: -Drocketmq.namesrv.addr=rocketmq-namesv1:9876;rocketmq-namesv2:9877 -Dcom.rocketmq.sendMessageWithVIPChannel=false
    networks:
      rocketmq:
        aliases:
          - rocketmq-console
networks:
  rocketmq:
    name: rocketmq
    driver: bridge

执行 docker-compose 部署

docker-compose up -d