目录
一、RocketMQ介绍
二、名词解释
三、安装
安装方式
准备工作
传统方式搭建RocketMQ集群
四、整合Springboot
一、RocketMQ介绍
????????RocketMQ是一款分布式消息中间件,最初是由阿里巴巴消息中间件团队研发并大规模应用于生产系统,满足线上海量消息堆积的需求, 在2016年底捐赠给Apache开源基金会成为孵化项目,经过不到一年时间正式成为了Apache顶级项目;早期阿里曾经基于ActiveMQ研发消息系统, 随着业务消息的规模增大,瓶颈逐渐显现,后来也考虑过Kafka,但因为在低延迟和高可靠性方面没有选择,最后才自主研发了RocketMQ, 各方面的性能都比目前已有的消息队列要好,RocketMQ和Kafka在概念和原理上都非常相似,所以也经常被拿来对比;RocketMQ默认采用长轮询的拉模式, 单机支持千万级别的消息堆积,可以非常好的应用在海量消息系统中。
二、名词解释
消息模型:RocketMQ主要由 Producer、Broker、Consumer 三部分组成,其中Producer 负责生产消息,Consumer 负责消费消息,Broker 负责存储消息。Broker 在实际部署过程中对应一台服务器,每个 Broker 可以存储多个Topic的消息,每个Topic的消息也可以分片存储于不同的 Broker。Message Queue 用于存储消息的物理地址,每个Topic中的消息地址存储于多个 Message Queue 中。ConsumerGroup 由多个Consumer 实例构成。
消息生产者(Producer):负责生产消息,一般由业务系统负责生产消息。一个消息生产者会把业务应用系统里产生的消息发送到broker服务器。RocketMQ提供多种发送方式,同步发送、异步发送、顺序发送、单向发送。同步和异步方式均需要Broker返回确认信息,单向发送不需要。
消息消费者(Consumer):负责消费消息,一般是后台系统负责异步消费。一个消息消费者会从Broker服务器拉取消息、并将其提供给应用程序。从用户应用的角度而言提供了两种消费形式:拉取式消费、推动式消费。
主题(Topic):表示一类消息的集合,每个主题包含若干条消息,每条消息只能属于一个主题,是RocketMQ进行消息订阅的基本单位。
代理服务器(Broker Server):消息中转角色,负责存储消息、转发消息。代理服务器在RocketMQ系统中负责接收从生产者发送来的消息并存储、同时为消费者的拉取请求作准备。代理服务器也存储消息相关的元数据,包括消费者组、消费进度偏移和主题和队列消息等。
名字服务(Name Server):名称服务充当路由消息的提供者。生产者或消费者能够通过名字服务查找各主题相应的Broker IP列表。多个Namesrv实例组成集群,但相互独立,没有信息交换。
拉取式消费(Pull Consumer):Consumer消费的一种类型,应用通常主动调用Consumer的拉消息方法从Broker服务器拉消息、主动权由应用控制。一旦获取了批量消息,应用就会启动消费过程。
推动式消费(Push Consumer):Consumer消费的一种类型,该模式下Broker收到数据后会主动推送给消费端,该消费模式一般实时性较高。
生产者组(Producer Group):同一类Producer的集合,这类Producer发送同一类消息且发送逻辑一致。如果发送的是事务消息且原始生产者在发送之后崩溃,则Broker服务器会联系同一生产者组的其他生产者实例以提交或回溯消费。
?消费者组(Consumer Group):同一类Consumer的集合,这类Consumer通常消费同一类消息且消费逻辑一致。消费者组使得在消息消费方面,实现负载均衡和容错的目标变得非常容易。要注意的是,消费者组的消费者实例必须订阅完全相同的Topic。RocketMQ 支持两种消息模式:集群消费(Clustering)和广播消费(Broadcasting)。
?集群消费(Clustering):集群消费模式下,相同Consumer Group的每个Consumer实例平均分摊消息。
广播消费(Broadcasting):广播消费模式下,相同Consumer Group的每个Consumer实例都接收全量的消息。
普通顺序消息(Normal Ordered Message):普通顺序消费模式下,消费者通过同一个消息队列( Topic 分区,称作 Message Queue) 收到的消息是有顺序的,不同消息队列收到的消息则可能是无顺序的。
严格顺序消息(Strictly Ordered Message):严格顺序消息模式下,消费者收到的所有消息均是有顺序的。
?消息(Message):消息系统所传输信息的物理载体,生产和消费数据的最小单位,每条消息必须属于一个主题。RocketMQ中每个消息拥有唯一的Message ID,且可以携带具有业务标识的Key。系统提供了通过Message ID和Key查询消息的功能。
?标签(Tag):为消息设置的标志,用于同一主题下区分不同类型的消息。来自同一业务单元的消息,可以根据不同业务目的在同一主题下设置不同标签。标签能够有效地保持代码的清晰度和连贯性,并优化RocketMQ提供的查询系统。消费者可以根据Tag实现对不同子主题的不同消费逻辑,实现更好的扩展性。
摘自官网?https://github.com/apache/rocketmq/blob/master/docs/cn/concept.md
三、安装
安装方式
单Master模式:这种方式风险较大,一旦Broker重启或者宕机时,会导致整个服务不可用。
多Master模式:
一个集群无Slave,全是Master。
多Master多Slave模式-异步复制:
每个Master配置一个Slave,有多对Master-Slave,HA采用异步复制方式,主备有短暂消息延迟(毫秒级)。
多Master多Slave模式-同步双写:
每个Master配置一个Slave,有多对Master-Slave,HA(双机集群)采用同步双写方式,即只有主备都写成功,才向应用返回成功。:
本文以多Master多Slave模式-异步复制这种方式安装
准备工作
1、由于rocketmq是使用java语言编写,所以首先需要安装jdk环境,本文使用jdk17 安装参考?Linux系统下安装jdk17&jdk8安装_熟透的蜗牛的博客-CSDN博客_linux安装jdk17
2、集群示意图
两台服务器互为主从
传统方式搭建RocketMQ集群
1、下载安装包 版本4.9.2
Apache Downloads
2、上传到服务器,并解压,如果没有安装unzipan,安装指令
[root@localhost ~]# yum install unzip zip
3、解压文件
#解压
[root@localhost ~]# unzip rocketmq-all-4.9.2-bin-release.zip
#移动到/usr/local 并重命名为rocketmq
[root@localhost ~]# mv rocketmq-4.9.2 /usr/local/rocketmq
4、修改启动脚本?
注意:因为我的jdk是17,所以需要修改启动脚本,jdk8不需要修改,修改的地方为垃圾回收器,和启动参数(-Xms256m -Xmx256m -Xmn128m)
runbroker.sh
#!/bin/sh
# 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.
#===========================================================================================
# Java Environment Setting
#===========================================================================================
error_exit ()
{
echo "ERROR: $1 !!"
exit 1
}
[ ! -e "$JAVA_HOME/bin/java" ] && JAVA_HOME=$HOME/jdk/java
[ ! -e "$JAVA_HOME/bin/java" ] && JAVA_HOME=/usr/java
[ ! -e "$JAVA_HOME/bin/java" ] && error_exit "Please set the JAVA_HOMEvariable in your environment, We need java(x64)!"
export JAVA_HOME
export JAVA="$JAVA_HOME/bin/java"
export BASE_DIR=$(dirname $0)/..
export CLASSPATH=.${JAVA_HOME}/jre/lib/ext:${BASE_DIR}/lib/*:${BASE_DIR}/conf:${CLASSPATH}
#export CLASSPATH=${BASE_DIR}/lib/rocketmq-broker-4.5.0.jar:${BASE_DIR}/lib/*:${BASE_DIR}/conf:${CLASSPATH}
#===========================================================================================
# JVM Configuration
#===========================================================================================
JAVA_OPT="${JAVA_OPT} -server -Xms256m -Xmx256m -Xmn128m"
JAVA_OPT="${JAVA_OPT} -XX:+UseG1GC -XX:G1HeapRegionSize=16m -XX:G1ReservePercent=25 -XX:InitiatingHeapOccupancyPercent=30 -XX:SoftRefLRUPolicyMSPerMB=0"
JAVA_OPT="${JAVA_OPT} -verbose:gc -Xlog:gc:/dev/shm/mq_gc_%p.log -XX:+PrintGCDetails"
JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow"
JAVA_OPT="${JAVA_OPT} -XX:+AlwaysPreTouch"
JAVA_OPT="${JAVA_OPT} -XX:MaxDirectMemorySize=15g"
JAVA_OPT="${JAVA_OPT} -XX:-UseLargePages -XX:-UseBiasedLocking"
#JAVA_OPT="${JAVA_OPT} -Xdebug -Xrunjdwp:transport=dt_socket,address=9555,server=y,suspend=n"
JAVA_OPT="${JAVA_OPT} ${JAVA_OPT_EXT}"
JAVA_OPT="${JAVA_OPT} -cp ${CLASSPATH}"
numactl --interleave=all pwd > /dev/null 2>&1
if [ $? -eq 0 ]
then
if [ -z "$RMQ_NUMA_NODE" ] ; then
numactl --interleave=all $JAVA ${JAVA_OPT} $@
else
numactl --cpunodebind=$RMQ_NUMA_NODE --membind=$RMQ_NUMA_NODE $JAVA${JAVA_OPT} $@
fi
else
$JAVA ${JAVA_OPT} --add-exports=java.base/jdk.internal.ref=ALL-UNNAMED $@
fi
runserver.sh
#!/bin/sh
# 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.
#===========================================================================================
# Java Environment Setting
#===========================================================================================
error_exit ()
{
echo "ERROR: $1 !!"
exit 1
}
[ ! -e "$JAVA_HOME/bin/java" ] && JAVA_HOME=$HOME/jdk/java
[ ! -e "$JAVA_HOME/bin/java" ] && JAVA_HOME=/usr/java
[ ! -e "$JAVA_HOME/bin/java" ] && error_exit "Please set the JAVA_HOMEvariable in your environment, We need java(x64)!"
export JAVA_HOME
export JAVA="$JAVA_HOME/bin/java"
export BASE_DIR=$(dirname $0)/..
export CLASSPATH=.:${BASE_DIR}/conf:${JAVA_HOME}/jre/lib/ext:${BASE_DIR}/lib/*
#===========================================================================================
# JVM Configuration
#===========================================================================================
JAVA_OPT="${JAVA_OPT} -server -Xms256m -Xmx256m -Xmn128m -XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=160m"
# jdk17 可能丢弃了CMS垃圾回收器,需要使用G1收集器
JAVA_OPT="${JAVA_OPT} -XX:+UseG1GC -XX:G1HeapRegionSize=16m -XX:G1ReservePercent=25 -XX:InitiatingHeapOccupancyPercent=30 -XX:SoftRefLRUPolicyMSPerMB=0"
# JAVA_OPT="${JAVA_OPT} -XX:CMSInitiatingOccupancyFraction=70 -XX:+CMSParallelRemarkEnabled -XX:SoftRefLRUPolicyMSPerMB=0 -XX:+CMSClassUnloadingEnabled -XX:SurvivorRatio=8"
JAVA_OPT="${JAVA_OPT} -verbose:gc -Xlog:gc:/dev/shm/rmq_srv_gc.log -XX:+PrintGCDetails"
JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow"
JAVA_OPT="${JAVA_OPT} -XX:-UseLargePages"
# JAVA_OPT="${JAVA_OPT} -Djava.ext.dirs=${JAVA_HOME}/jre/lib/ext:${BASE_DIR}/lib"
#JAVA_OPT="${JAVA_OPT} -Xdebug -Xrunjdwp:transport=dt_socket,address=9555,server=y,suspend=n"
JAVA_OPT="${JAVA_OPT} ${JAVA_OPT_EXT}"
JAVA_OPT="${JAVA_OPT} -cp ${CLASSPATH}"
$JAVA ${JAVA_OPT} $@
tools.sh?
#!/bin/sh
# 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.
#===========================================================================================
# Java Environment Setting
#===========================================================================================
error_exit ()
{
echo "ERROR: $1 !!"
exit 1
}
[ ! -e "$JAVA_HOME/bin/java" ] && JAVA_HOME=$HOME/jdk/java
[ ! -e "$JAVA_HOME/bin/java" ] && JAVA_HOME=/usr/java
[ ! -e "$JAVA_HOME/bin/java" ] && error_exit "Please set the JAVA_HOME variable in your environment, We need java(x64)!"
export JAVA_HOME
export JAVA="$JAVA_HOME/bin/java"
export BASE_DIR=$(dirname $0)/..
export CLASSPATH=${BASE_DIR}/lib/*:${BASE_DIR}/conf:.:${CLASSPATH}
export CLASSPATH=.${JAVA_HOME}/jre/lib/ext:${BASE_DIR}/lib/*:${BASE_DIR}/conf:${CLASSPATH}
#export CLASSPATH=.:${BASE_DIR}/conf:${CLASSPATH}
#echo "BASE_DIR:$BASE_DIR"
#echo "CLASSPATH:$CLASSPATH"
#===========================================================================================
# JVM Configuration
#===========================================================================================
JAVA_OPT="${JAVA_OPT} -server -Xms256m -Xmx256m -Xmn256m -XX:PermSize=128m-XX:MaxPermSize=128m"
JAVA_OPT="${JAVA_OPT} -cp ${CLASSPATH}"
$JAVA ${JAVA_OPT} $@
?5、修改服务器1上的配置文件
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.
#所属集群名字,同一个集群名字相同
brokerClusterName=rocketmq-cluster
#broker名字
brokerName=broker-a
#0表示master >0 表示slave
brokerId=0
#删除文件的时间点,凌晨4点
deleteWhen=04
#文件保留时间 默认是48小时
fileReservedTime=168
#异步复制Master
brokerRole=ASYNC_MASTER
#刷盘方式,ASYNC_FLUSH=异步刷盘,SYNC_FLUSH=同步刷盘
flushDiskType=ASYNC_FLUSH
#Broker 对外服务的监听端口
listenPort=10911
#nameServer地址,这里nameserver是单台,如果nameserver是多台集群的话,就用分号分割(即namesrvAddr=ip1:port1;ip2:port2;ip3:port3)
namesrvAddr=192.168.6.145:9876;192.168.6.146:9876
#每个topic对应队列的数量,默认为4,实际应参考consumer实例的数量,值过小不利于consumer负载均衡
defaultTopicQueueNums=8
#是否允许 Broker 自动创建Topic,生产建议关闭
autoCreateTopicEnable=true
#是否允许 Broker 自动创建订阅组,生产建议关闭
autoCreateSubscriptionGroup=true
#设置BrokerIP 服务器1的ip地址
brokerIP1=192.168.6.145
#存储路径
storePathRootDir=/data/rocketmq/store-a
#commitLog 存储路径
storePathCommitLog=/data/rocketmq/store-a/commitlog
#消费队列存储路径存储路径
storePathConsumerQueue=/data/rocketmq/store-a/consumequeue
#消息索引存储路径
storePathIndex=/data/rocketmq/store-a/index
#checkpoint 文件存储路径
storeCheckpoint=/data/rocketmq/store-a/checkpoint
#abort 文件存储路径
abortFile=/data/rocketmq/store-a/abort
#commitLog每个文件的大小默认1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue每个文件默认存30W条,根据业务情况调整
mapedFileSizeConsumeQueue=300000
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.
brokerClusterName=rocketmq-cluster
brokerName=broker-b
#slave
brokerId=1
deleteWhen=04
fileReservedTime=168
brokerRole=SLAVE
flushDiskType=ASYNC_FLUSH
#Broker 对外服务的监听端口
listenPort=11011
#nameServer地址,这里nameserver是单台,如果nameserver是多台集群的话,就用分号分割(即namesrvAddr=ip1:port1;ip2:port2;ip3:port3)
namesrvAddr=192.168.6.145:9876;192.168.6.146:9876
#每个topic对应队列的数量,默认为4,实际应参考consumer实例的数量,值过小不利于consumer负载均衡
defaultTopicQueueNums=8
#是否允许 Broker 自动创建Topic,生产建议关闭
autoCreateTopicEnable=true
#是否允许 Broker 自动创建订阅组,生产建议关闭
autoCreateSubscriptionGroup=true
#设置BrokerIP
brokerIP1=192.168.6.145
#存储路径
storePathRootDir=/data/rocketmq/store-b
#commitLog 存储路径
storePathCommitLog=/data/rocketmq/store-b/commitlog
#消费队列存储路径存储路径
storePathConsumerQueue=/data/rocketmq/store-b/consumequeue
#消息索引存储路径
storePathIndex=/data/rocketmq/store-b/index
#checkpoint 文件存储路径
storeCheckpoint=/data/rocketmq/store-b/checkpoint
#abort 文件存储路径
abortFile=/data/rocketmq/store-b/abort
#commitLog每个文件的大小默认1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue每个文件默认存30W条,根据业务情况调整
mapedFileSizeConsumeQueue=300000
6、修改服务器2上的配置文件
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.
#所属集群名字
brokerClusterName=rocketmq-cluster
#broker名字,注意此处不同的配置文件填写的不一样 例如:在a.properties 文件中写 broker-a 在b.properties 文件中写 broker-b
brokerName=broker-b
#0 表示 Master,>0 表示 Slave
brokerId=0
#删除文件时间点,默认凌晨 4点
deleteWhen=04
#文件保留时间,默认 48 小时
fileReservedTime=168
#Broker 的角色,ASYNC_MASTER=异步复制Master,SYNC_MASTER=同步双写Master,SLAVE=slave节点
brokerRole=ASYNC_MASTER
#刷盘方式,ASYNC_FLUSH=异步刷盘,SYNC_FLUSH=同步刷盘
flushDiskType=SYNC_FLUSH
#Broker 对外服务的监听端口
listenPort=10911
#nameServer地址,这里nameserver是单台,如果nameserver是多台集群的话,就用分号分割(即namesrvAddr=ip1:port1;ip2:port2;ip3:port3)
namesrvAddr=192.168.6.145:9876;192.168.6.146:9876
#每个topic对应队列的数量,默认为4,实际应参考consumer实例的数量,值过小不利于consumer负载均衡
defaultTopicQueueNums=8
#是否允许 Broker 自动创建Topic,生产建议关闭
autoCreateTopicEnable=true
#是否允许 Broker 自动创建订阅组,生产建议关闭
autoCreateSubscriptionGroup=true
#设置BrokerIP
brokerIP1=192.168.6.146
#存储路径
storePathRootDir=/data/rocketmq/store-b
#commitLog 存储路径
storePathCommitLog=/data/rocketmq/store-b/commitlog
#消费队列存储路径存储路径
storePathConsumerQueue=/data/rocketmq/store-b/consumequeue
#消息索引存储路径
storePathIndex=/data/rocketmq/store-b/index
#checkpoint 文件存储路径
storeCheckpoint=/data/rocketmq/store-b/checkpoint
#abort 文件存储路径
abortFile=/data/rocketmq/store-b/abort
#commitLog每个文件的大小默认1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue每个文件默认存30W条,根据业务情况调整
mapedFileSizeConsumeQueue=300000
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.
#所属集群名字
brokerClusterName=rocketmq-cluster
#broker名字,注意此处不同的配置文件填写的不一样 例如:在a.properties 文件中写 broker-a 在b.properties 文件中写 broker-b
brokerName=broker-a
#0 表示 Master,>0 表示 Slave
brokerId=1
#删除文件时间点,默认凌晨 4点
deleteWhen=04
#文件保留时间,默认 48 小时
fileReservedTime=168
#Broker 的角色,ASYNC_MASTER=异步复制Master,SYNC_MASTER=同步双写Master,SLAVE=slave节点
brokerRole=SLAVE
#刷盘方式,ASYNC_FLUSH=异步刷盘,SYNC_FLUSH=同步刷盘
flushDiskType=SYNC_FLUSH
#Broker 对外服务的监听端口
listenPort=11011
#nameServer地址,这里nameserver是单台,如果nameserver是多台集群的话,就用分号分割(即namesrvAddr=ip1:port1;ip2:port2;ip3:port3)
namesrvAddr=192.168.6.145:9876;192.168.6.146:9876
#每个topic对应队列的数量,默认为4,实际应参考consumer实例的数量,值过小不利于consumer负载均衡
defaultTopicQueueNums=8
#是否允许 Broker 自动创建Topic,生产建议关闭
autoCreateTopicEnable=true
#是否允许 Broker 自动创建订阅组,生产建议关闭
autoCreateSubscriptionGroup=true
#设置BrokerIP
brokerIP1=192.168.6.146
#存储路径
storePathRootDir=/data/rocketmq/store-a
#commitLog 存储路径
storePathCommitLog=/data/rocketmq/store-a/commitlog
#消费队列存储路径存储路径
storePathConsumerQueue=/data/rocketmq/store-a/consumequeue
#消息索引存储路径
storePathIndex=/data/rocketmq/store-a/index
#checkpoint 文件存储路径
storeCheckpoint=/data/rocketmq/store-a/checkpoint
#abort 文件存储路径
abortFile=/data/rocketmq/store-a/abort
#commitLog每个文件的大小默认1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue每个文件默认存30W条,根据业务情况调整
mapedFileSizeConsumeQueue=300000
7、启动nameserver
进入 [root@localhost ~]# cd /usr/local/rocketmq/bin/ 目录?
启动 nohup sh mqnamesrv &
检验是否安装成功
tail -f ~/logs/rocketmqlogs/namesrv.log
如果此时报错
/usr/local/rocketmq/bin/runserver.sh: line 19: syntax error near unexpected token `$'\r'' 'usr/local/rocketmq/bin/runserver.sh: line 19: `error_exit () 解决方法,拷贝的文件格式错乱使用下面命令乎看到文件后面多了^M [root@bogon bin]# vim -b runserver.sh?
去掉^M 再重新启动 [root@bogon bin]# sed -i 's/\r//g' runserver.sh ?
8、启动broker
启动 服务器1上的master
???????[root@bogon rocketmq]# nohup sh bin/mqbroker -c conf/2m-2s-async/broker-a.properties &
启动服务器2上的master
[root@localhost rocketmq]# nohup sh bin/mqbroker -c conf/2m-2s-async/broker-b.properties &
?启动服务器1上的slave
[root@bogon rocketmq]# nohup sh bin/mqbroker -c conf/2m-2s-async/broker-b-s.properties &
启动服务器2上的slave
[root@localhost rocketmq]# nohup sh bin/mqbroker -c conf/2m-2s-async/broker-a-s.properties &
9、搭建可视化管理界面
maven打包安装不细说
mvn clean package -Dmaven.test.skip=true
java -jar target/rocketmq-console-ng-1.0.1.jar
下载安装包? Rocketmq可视化工具-Web服务器文档类资源-CSDN下载
???????
四、整合Springboot
maven依赖
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>2.2.1</version>
</dependency>
application.yml
server:
port: 8090
rocketmq:
name-server: 192.168.6.145:9876;192.168.6.146:9876
producer:
group: rocketmq-producer
?生产者
package com.xiaojie.rocket.rocket.producer;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @Description: 发送消息:同步消息、异步消息和单向消息。其中前两种消息是可靠的,因为会有发送是否成功的应答
* @author: yan
* @date: 2021.11.08
*/
@Service
@Slf4j
public class MqProducer {
@Autowired
private RocketMQTemplate rocketMQTemplate;
/**
* @description: 这种方式主要用在不特别关心发送结果的场景,例如日志发送。
* @param:
* @return: void
* @author xiaojie
* @date: 2021/11/9 23:39
*/
public void sendMq() {
for (int i = 0; i < 10; i++) {
rocketMQTemplate.convertAndSend("xiaojie-test", "测试发送消息》》》》》》》》》" + i);
}
}
/**
* @description: 这种可靠性同步地发送方式使用的比较广泛,比如:重要的消息通知,短信通知。
* @param:
* @return: void
* @author xiaojie
* @date: 2021/11/10 22:25
*/
public void sync() {
SendResult sendResult = rocketMQTemplate.syncSend("xiaojie-test", "sync发送消息。。。。。。。。。。");
log.info("发送结果{}", sendResult);
}
/**
* @description: 异步消息通常用在对响应时间敏感的业务场景,即发送端不能容忍长时间地等待Broker的响应。
* @param:
* @return: void
* @author xiaojie
* @date: 2021/11/10 22:29
*/
public void async() {
String msg = "异步发送消息。。。。。。。。。。";
log.info(">msg:<<" + msg);
rocketMQTemplate.asyncSend("xiaojie-test", msg, new SendCallback() {
@Override
public void onSuccess(SendResult var1) {
log.info("异步发送成功{}", var1);
}
@Override
public void onException(Throwable var1) {
//发送失败可以执行重试
log.info("异步发送失败{}", var1);
}
});
}
}
消费者
package com.xiaojie.rocket.rocket.consumer;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;
/**
* @Description:
* @author: yan
* @date: 2021.11.08
*/
@RocketMQMessageListener(consumerGroup = "test-group", topic = "xiaojie-test")
@Slf4j
@Component
public class MqConsumer implements RocketMQListener<String> {
@Override
public void onMessage(String message) {
log.info("接收到的数据是:{}", message);
}
}
参考:https://blog.csdn.net/javahongxi/article/details/84931747
完整代码参考:?spring-boot: Springboot整合redis、消息中间件等相关代码
|