RocketMQ集群搭建-4.2.0版本

6,119 阅读12分钟

首发于我的博客: www.liutf.com/
首发链接:www.liutf.com/posts/14196…

背景

由于公司内部用的RocketMQ消息中间件是个单点实例,随着业务发展,越来越多的应用接入了同一个中间件,耦合性太强。也觉得越来越不安心,不稳心。怕哪天宕了后,所有业务也跟着受到牵连影响。为了组件高可用,决定新搭建一套集群,业务后续逐步迁移过来。

生产中目前所用版本为:v3.2,当时的版本还是归属于阿里。

新搭建一套用官网最新版 v4.2.0 ,现已归属于Apache

集群网络架构图

NameServer简介

NameServer顾名思义,在系统中肯定是做命名服务,服务治理方面的工作,功能应该是和zookeeper差不多,据说RocketMq的早期版本确实是使用的zookeeper,后来改为了自己实现的NameServer。

NameServer在RocketMQ中的两个主要作用:

  • NameServer维护了一份Broker的地址列表和,broker在启动的时候会去NameServer进行注册,会维护Broker的存活状态。

  • NameServer维护了一份Topic和Topic对应队列的地址列表,broker每次发送心跳过来的时候都会把Topic信息带上。

NameServer的稳定性非常高。原因有二:

  1. NameServer互相独立,彼此没有通信关系,单台NameServer挂掉,不影响其他NameServer,即使全部挂掉,也不影响业务系统使用。无状态。
  2. NameServer不会有频繁的读写,所以性能开销非常小,稳定性很高。

Broker简介

broker是消息接收处理,落地的核心模块。这个模块用于接收producer发送的消息以及consumer。

与NameServer关系

  • 连接

    单个broker和所有nameServer保持长连接

  • 心跳

    • 心跳间隔:每隔30秒(此时间无法更改)向所有nameserver发送心跳,心跳包含了自身的topic配置信息。
    • 心跳超时:nameserver每隔10秒钟(此时间无法更改),扫描所有还存活的broker连接,若某个连接2分钟内(当前时间与最后更新时间差值超过2分钟,此时间无法更改)没有发送心跳数据,则断开连接。
  • 断开

  • 时机:broker挂掉;心跳超时导致nameserver主动关闭连接

  • 动作:一旦连接断开,nameserver会立即感知,更新topc与队列的对应关系,但不会通知生产者和消费者

负载均衡

  • 一个topic分布在多个broker上,一个broker可以配置多个topic,它们是多对多的关系。
  • 如果某个topic消息量很大,应该给它多配置几个队列,并且尽量多分布在不同broker上,减轻某个broker的压力。
  • topic消息量都比较均匀的情况下,如果某个broker上的队列越多,则该broker压力越大。

集群介绍

  1. 单个Master

    这种方式风险较大,一旦Broker重启或者宕机时,会导致整个服务不可用,不建议线上环境使用。

  2. 多 Master 模式

    一个集群无 Slave,全是 Master,例如 2 个 Master 或者 3 个 Master。

    优点:

    配置简单,单个Master 宕机或重启维护对应用无影响,在磁盘配置为

    RAID10 时,即使机器宕机不可恢复情况下,由与 RAID10

    磁盘非常可靠,消息也不会丢(异步刷盘丢失少量消息,同步刷盘一条不丢)。性能最高。

    缺点:

    单台机器宕机期间,这台机器上未被消费的消息在机器恢复之前不可订阅,消息实时性会受到受到影响。

    启动步骤:

    先启动 NameServer

    在机器 A,启动第一个 Master

    在机器 B,启动第二个 Master

  3. 多 Master 多 Slave 模式,同步刷盘

    每个 Master 配置一个 Slave,有多对Master-Slave,HA采用同步双写方式,主备都写成功,向应用返回成功。

    优点:

    数据与服务都无单点,Master宕机情况下,消息无延迟,服务可用性与数据可用性都非常高。

    缺点:

    性能比异步复制模式略低,大约低 10%左右,发送单个消息的 RT会略高。目前主宕机后,备机不能自动切换为主机,后续会支持自动切换功能。

    启动步骤:

    先启动 NameServer

    在机器 A,启动第一个 Master

    在机器 B,启动第二个 Master

    在机器 C,启动第一个 Slave

    在机器 D,启动第二个 Slave

    以上 Broker 与 Slave 配对是通过指定相同的brokerName 参数来配对,Master的 BrokerId 必须是 0,Slave 的BrokerId 必须是大与 0 的数。另外一个 Master下面可以挂载多个 Slave,同一 Master 下的多个 Slave通过指定不同的 BrokerId来区分。

  4. 多 Master 多 Slave 模式,异步刷盘

    每个 Master 配置一个 Slave,有多对Master-Slave,HA采用异步复制方式,主备有短暂消息延迟,毫秒级。

    优点:

    即使磁盘损坏,消息丢失的非常少,且消息实时性不会受影响,因为

    Master 宕机后,消费者仍然可以从 Slave消费,此过程对应用透明。不需要人工干预。性能同多 Master 模式几乎一样。

    缺点:

    Master 宕机,磁盘损坏情况,会丢失少量消息。

    启动步骤:

    先启动 NameServer

    在机器 A,启动第一个 Master

    在机器 B,启动第二个 Master

    在机器 C,启动第一个 Slave

    在机器 D,启动第二个 Slave

集群搭建

综上几种集群方式,我选取的是多Master多Slave,异步刷盘 的方案。

环境准备

  • 主机信息
IP 备注
10.10.10.31 slave-b&namesrv
10.10.10.43 slave-a&namesrv
10.10.10.44 master-a&namesrv
10.10.10.45 master-b&namesrv

4台主机都启动了namesrv服务,做namesrv集群。

44和45两台机分别为master-a和master-b。

43和31两台机分别为slave-a和slave-b。

配置要求

硬件:

12G+内存(broker默认分配8G,namesrv默认分配4G,可自行调整)

软件:

  1. 64bit OS, Linux/Unix/Mac is recommended;
  2. 64bit JDK 1.8+;
  3. Maven 3.2.x(非必要)
  4. Git(非必须)

正式搭建

  1. 先搭建单机环境,参考单机Quick Start官方教程 。让各单机能跑起来,以及熟悉基本的指令操作。

  2. 各单机搭建成功后, 然后我们开始做集群配置。集群的核心也就是在于各配置文件配置了。

  3. 进入配置文件目录/home/rocket/apache-rocketmq/conf 。这里我以其中一台机器rocket-master-b 示例。

    目录介绍

    • 2m-noslave: 多Master模式
    • 2m-2s-sync: 多Master多Slave模式,同步双写
    • 2m-2s-async:多Master多Slave模式,异步复制

    我选择的是多Master多Slave,异步刷盘 方式,进入2m-2s-async 目录做配置。

    [rocket@rocket-master-b conf]$ cd 2m-2s-async/
    [rocket@rocket-master-b 2m-2s-async]$ ls
    broker-a.properties  broker-a-s.properties  broker-b.properties  broker-b-s.properties
    

    这里可以看到默认有4份文件,这也就是我们的重点内容了。

  4. 修改各配置文件

    • broker-a.properties

      # Licensed to the Apache Software Foundation (ASF) under one or more
      # contributor license agreements.  See the NOTICE file distributed with
      # this work for additional information regarding copyright ownership.
      # The ASF licenses this file to You under the Apache License, Version 2.0
      # (the "License"); you may not use this file except in compliance with
      # the License.  You may obtain a copy of the License at
      #
      #     http://www.apache.org/licenses/LICENSE-2.0
      #
      # Unless required by applicable law or agreed to in writing, software
      # distributed under the License is distributed on an "AS IS" BASIS,
      # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      # See the License for the specific language governing permissions and
      # limitations under the License.
      
      # 配置参考官方链接:http://rocketmq.apache.org/docs/rmq-deployment/
      
      # 所属集群名字
      brokerClusterName=rocketmq-cluster
      
      # broker名字,注意此处不同的配置文件填写的不一样
      brokerName=broker-a
      
      # 0 表示 Master,>0 表示 Slave
      brokerId=0
      
      # 删除文件时间点,默认凌晨4点。24小时制,单位小时
      deleteWhen=04
      
      # 文件保留时间,默认 72 小时。根据业务情况调整
      fileReservedTime=168
      
      # Broker 对外服务的监听端口
      listenPort=10911
      
      # nameServer地址,分号分割
      namesrvAddr=10.10.10.44:9876;10.10.10.45:9876;10.10.10.46:9876;10.10.10.31:9876
      
      # Details:Should be configured if having multiple addresses; Default value:InetAddress for network interface
      # 本机ip地址,默认系统自动识别,但是某些多网卡机器会存在识别错误的情况,这种情况下可以人工配置。
      brokerIP1=10.10.10.45
      
      # commitLog 存储路径
      storePathCommitLog=/home/rocket/app/rocketmq/store/commitlog
      
      # 消费队列存储路径存储路径
      storePathConsumerQueue=/home/rocket/app/rocketmq/store/consumequeue
      
      # commitLog每个文件的大小默认1G
      mapedFileSizeCommitLog=1073741824
      
      # Broker 的角色
      # - ASYNC_MASTER 异步复制Master
      # - SYNC_MASTER 同步双写Master
      # - SLAVE
      brokerRole=ASYNC_MASTER
      
      # 刷盘方式
      # - ASYNC_FLUSH 异步刷盘
      # - SYNC_FLUSH 同步刷盘
      flushDiskType=ASYNC_FLUSH
      
      

    • broker-a-s.properties

      # Licensed to the Apache Software Foundation (ASF) under one or more
      # contributor license agreements.  See the NOTICE file distributed with
      # this work for additional information regarding copyright ownership.
      # The ASF licenses this file to You under the Apache License, Version 2.0
      # (the "License"); you may not use this file except in compliance with
      # the License.  You may obtain a copy of the License at
      #
      #     http://www.apache.org/licenses/LICENSE-2.0
      #
      # Unless required by applicable law or agreed to in writing, software
      # distributed under the License is distributed on an "AS IS" BASIS,
      # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      # See the License for the specific language governing permissions and
      # limitations under the License.
      
      # 配置参考官方链接:http://rocketmq.apache.org/docs/rmq-deployment/
      
      # 所属集群名字
      brokerClusterName=rocketmq-cluster
      
      # broker名字,注意此处不同的配置文件填写的不一样
      brokerName=broker-a
      
      # 0 表示 Master,>0 表示 Slave
      brokerId=1
      
      # 删除文件时间点,默认凌晨4点。24小时制,单位小时
      deleteWhen=04
      
      # 文件保留时间,默认 72 小时。根据业务情况调整
      fileReservedTime=168
      
      # Broker 对外服务的监听端口
      listenPort=10911
      
      # nameServer地址,分号分割
      namesrvAddr=10.10.10.44:9876;10.10.10.45:9876;10.10.10.46:9876;10.10.10.31:9876
      
      # Details:Should be configured if having multiple addresses; Default value:InetAddress for network interface
      # 本机ip地址,默认系统自动识别,但是某些多网卡机器会存在识别错误的情况,这种情况下可以人工配置。
      brokerIP1=10.10.10.44
      
      # commitLog 存储路径
      storePathCommitLog=/home/rocket/app/rocketmq-slave/store/commitlog
      
      # 消费队列存储路径存储路径
      storePathConsumerQueue=/home/rocket/app/rocketmq-slave/store/consumequeue
      
      # commitLog每个文件的大小默认1G
      mapedFileSizeCommitLog=1073741824
      
      # Broker 的角色
      # - ASYNC_MASTER 异步复制Master
      # - SYNC_MASTER 同步双写Master
      # - SLAVE
      brokerRole=SLAVE
      
      # 刷盘方式
      # - ASYNC_FLUSH 异步刷盘
      # - SYNC_FLUSH 同步刷盘
      flushDiskType=ASYNC_FLUSH
      
      

    • broker-b.properties

      # Licensed to the Apache Software Foundation (ASF) under one or more
      # contributor license agreements.  See the NOTICE file distributed with
      # this work for additional information regarding copyright ownership.
      # The ASF licenses this file to You under the Apache License, Version 2.0
      # (the "License"); you may not use this file except in compliance with
      # the License.  You may obtain a copy of the License at
      #
      #     http://www.apache.org/licenses/LICENSE-2.0
      #
      # Unless required by applicable law or agreed to in writing, software
      # distributed under the License is distributed on an "AS IS" BASIS,
      # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      # See the License for the specific language governing permissions and
      # limitations under the License.
      
      # 配置参考官方链接:http://rocketmq.apache.org/docs/rmq-deployment/
      
      # 所属集群名字
      brokerClusterName=rocketmq-cluster
      
      # broker名字,注意此处不同的配置文件填写的不一样
      brokerName=broker-b
      
      # 0 表示 Master,>0 表示 Slave
      brokerId=0
      
      # 删除文件时间点,默认凌晨4点。24小时制,单位小时
      deleteWhen=04
      
      # 文件保留时间,默认 72 小时。根据业务情况调整
      fileReservedTime=168
      
      # Broker 对外服务的监听端口
      listenPort=10921
      
      # nameServer地址,分号分割
      namesrvAddr=10.10.10.44:9876;10.10.10.45:9876;10.10.10.46:9876;10.10.10.31:9876
      
      # Details:Should be configured if having multiple addresses; Default value:InetAddress for network interface
      # 本机ip地址,默认系统自动识别,但是某些多网卡机器会存在识别错误的情况,这种情况下可以人工配置。
      brokerIP1=10.10.10.46
      
      # commitLog 存储路径
      storePathCommitLog=/home/rocket/app/rocketmq/store/commitlog
      
      # 消费队列存储路径存储路径
      storePathConsumerQueue=/home/rocket/app/rocketmq/store/consumequeue
      
      # commitLog每个文件的大小默认1G
      mapedFileSizeCommitLog=1073741824
      
      # Broker 的角色
      # - ASYNC_MASTER 异步复制Master
      # - SYNC_MASTER 同步双写Master
      # - SLAVE
      brokerRole=ASYNC_MASTER
      
      # 刷盘方式
      # - ASYNC_FLUSH 异步刷盘
      # - SYNC_FLUSH 同步刷盘
      flushDiskType=ASYNC_FLUSH
      
      

    • broker-b-s.properties

      # Licensed to the Apache Software Foundation (ASF) under one or more
      # contributor license agreements.  See the NOTICE file distributed with
      # this work for additional information regarding copyright ownership.
      # The ASF licenses this file to You under the Apache License, Version 2.0
      # (the "License"); you may not use this file except in compliance with
      # the License.  You may obtain a copy of the License at
      #
      #     http://www.apache.org/licenses/LICENSE-2.0
      #
      # Unless required by applicable law or agreed to in writing, software
      # distributed under the License is distributed on an "AS IS" BASIS,
      # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      # See the License for the specific language governing permissions and
      # limitations under the License.
      
      # 配置参考官方链接:http://rocketmq.apache.org/docs/rmq-deployment/
      
      # 所属集群名字
      brokerClusterName=rocketmq-cluster
      
      # broker名字,注意此处不同的配置文件填写的不一样
      brokerName=broker-b
      
      # 0 表示 Master,>0 表示 Slave
      brokerId=1
      
      # 删除文件时间点,默认凌晨4点。24小时制,单位小时
      deleteWhen=04
      
      # 文件保留时间,默认 72 小时。根据业务情况调整
      fileReservedTime=168
      
      # Broker 对外服务的监听端口
      listenPort=10921
      
      # nameServer地址,分号分割
      namesrvAddr=10.10.10.44:9876;10.10.10.45:9876;10.10.10.46:9876;10.10.10.31:9876
      
      # Details:Should be configured if having multiple addresses; Default value:InetAddress for network interface
      # 本机ip地址,默认系统自动识别,但是某些多网卡机器会存在识别错误的情况,这种情况下可以人工配置。
      brokerIP1=10.10.10.31
      
      # commitLog 存储路径
      storePathCommitLog=/home/persiancat/rocketmq-slave-p/app/rocketmq-slave/store/commitlog
      
      # 消费队列存储路径存储路径
      storePathConsumerQueue=/home/persiancat/rocketmq-slave-p/app/rocketmq-slave/store/consumequeue
      
      # commitLog每个文件的大小默认1G
      mapedFileSizeCommitLog=1073741824
      
      # Broker 的角色
      # - ASYNC_MASTER 异步复制Master
      # - SYNC_MASTER 同步双写Master
      # - SLAVE
      brokerRole=SLAVE
      
      # 刷盘方式
      # - ASYNC_FLUSH 异步刷盘
      # - SYNC_FLUSH 同步刷盘
      flushDiskType=ASYNC_FLUSH
      
      

    重点配置说明

    • brokerClusterName:同一个集群中,brokerClusterName需一致
    • brokerId:0 表示 Master,>0 表示 Slave
    • namesrvAddr:配置多个用分号分隔
    • brokerIP1:默认系统自动识别,但是某些多网卡机器会存在识别错误的情况,建议都手工指定
    • brokerRole:选择Broker的角色
    • flushDiskType:选择刷盘方式

启动

配置文件配置完成后,我们开始启动。

# 进入rocketmq根目录
cd /home/rocket/apache-rocketmq

# 后台执行bin目录文件夹下mqnamesrv服务
nohup sh bin/mqnamesrv &

# broker-a机器下执行broker-a.properties文件启动
nohup sh bin/mqbroker -c conf/2m-2s-async/broker-a.properties -n "10.10.10.44:9876;10.10.10.45:9876;10.10.10.46:9876;10.10.10.31:9876" &

# broker-b机器下执行broker-b.properties文件启动
nohup sh bin/mqbroker -c conf/2m-2s-async/broker-b.properties -n "10.10.10.44:9876;10.10.10.45:9876;10.10.10.46:9876;10.10.10.31:9876" &

# broker-a-s机器下执行broker-a-s.properties文件启动
nohup sh bin/mqbroker -c conf/2m-2s-async/broker-a-s.properties -n "10.10.10.44:9876;10.10.10.45:9876;10.10.10.46:9876;10.10.10.31:9876" &

# broker-b-s机器下执行broker-b-s.properties文件启动
nohup sh bin/mqbroker -c conf/2m-2s-async/broker-b-s.properties -n "10.10.10.44:9876;10.10.10.45:9876;10.10.10.46:9876;10.10.10.31:9876" &

注意事项

  • 注意防火墙,刚开始搭建可以先关闭掉防火墙。搞定后再开防火墙,配置对应端口。

RocketMQ控制台

  • 官方下载:https://github.com/apache/rocketmq-externals/tree/master/rocketmq-console