安装之前需要安装并配置 Java 环境,版本需要 jdk1.8
官网地址:http://rocketmq.apache.org/dowloading/releases/
官方说明文档:https://github.com/apache/rocketmq/tree/master/docs/cn
cd /usr/software
wget https://mirrors.tuna.tsinghua.edu.cn/apache/rocketmq/4.8.0/rocketmq-all-4.8.0-bin-release.zip
unzip rocketmq-all-4.8.0-bin-release.zip -d /usr/local
配置环境变量
vim /etc/profile
export ROCKETMQ_HOME=/usr/local/rocketmq-all-4.8.0-bin-release
export PATH=$JAVA_HOME/bin:$ROCKETMQ_HOME/bin:$PATH
source /etc/profile
修改配置文件
配置介绍:https://developer.aliyun.com/article/238351
vim /usr/local/rocketmq-all-4.8.0-bin-release/conf/broker.conf
# Ezrealer record this config for rocketmq 4.8 on centos7.9
# 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 = DefaultCluster
brokerName = broker-a
brokerId = 0
deleteWhen = 04
fileReservedTime = 48
brokerRole = ASYNC_MASTER
flushDiskType = ASYNC_FLUSH
listenPort=10911
namesrvAddr=ip:port
brokerIP1=ip
#存储路径
storePathRootDir=/data/rocketmq/store
#commitLog存储路径
storePathCommitLog=/data/rocketmq/store/commitlog
#消费队列存储路径
storePathConsumeQueue=/data/rocketmq/store/consumequeue
# 消息索引存储路径
storePathIndex=/data/rocketmq/store/index
# checkpoint 文件存储路径
storeCheckpoint=/data/rocketmq/store/checkpoint
#abort 文件存储路径
abortFile=/data/rocketmq/store/abort
maxTransferCountOnMessageInMemory=1000
mkdir -p /data/rocketmq/store/
cd /data/rocketmq/store/
mkdir abort
mkdir commitlog
mkdir index
mkdir consumequeue
启动
# 进入目录
cd /usr/local/rocketmq/bin
# 启动mqnamesrv
nohup sh mqnamesrv &
tail -f ~/logs/rocketmqlogs/namesrv.log
# 停止命令
sh mqshutdown namesrv
# 启动broker
nohup sh mqbroker -c ../conf/broker.conf -n ip:9876 &
tail -f ~/logs/rocketmqlogs/broker.log
# 停止命令
sh mqshutdown broker
配置内存使用
# 可以修改如下两个文件的 Xms,Xmx,Xmn
vim bin/runbroker.sh
vim bin/runServer.sh
rocketmq-console 的安装配置与使用
安装与配置 maven
cd /usr/software
wget https://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
tar -zxvf apache-maven-3.6.3-bin.tar.gz -C /usr/local/
mv /usr/local/apache-maven-3.6.3/ maven3.6
vim /etc/profile
# 增加一行
export PATH="/usr/local/maven3.6/bin:$PATH"
# 保存
source /etc/profile
# 验证是否安装成功
which mvn
mvn -v
# 配置阿里云的 maven 源
cd /usr/local/maven3.6/conf
vim settings.xml
# 在 mirrors 节点下添加
<mirror>
<id>aliyun-maven</id>
<mirrorOf>*</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
# 保存
安装与配置rocketmq-console
# 下载与解压 rocketmq-externals
cd /usr/software/
wget https://github.com/apache/rocketmq-externals/archive/master.zip
unzip rocketmq-externals-master.zip -d /usr/local
# 配置 rocket-console
cd /usr/local/rocketmq-externals-master/rocketmq-console/src/main/resources
vim application.properties
server.port=8876
rocketmq.config.namesrvAddr=ip:port
# 保存
# maven 编译
cd /usr/local/rocketmq-externals-master/rocketmq-console/ # 回退到有pom.xml的目录即可
mvn clean package -Dmaven.test.skip=true
# 因为编译的时候会遇到报错:checkstyle 的错误,所以 编辑 pom,把 artifactId 为 maven-checkstyle-plugin 所属的
# <plugin> 注释掉
<!--
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.17</version>
<executions>
<execution>
<id>validate</id>
<phase>validate</phase>
<configuration>
<excludes>src/main/resources</excludes>
<configLocation>style/rmq_checkstyle.xml</configLocation>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
</configuration>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
-->
# 保存后再次编译
mvn clean package -Dmaven.test.skip=true
# 启动 rocket-console,并输入日志到文件
cd /usr/local/rocketmq-externals-master/rocketmq-console/target
nohua java -jar rocketmq-console-ng-2.0.0.jar > rocketmq-console.log &
# 访问 ip:port 即可
RocketMQ 相关
Message消息对象
topic:表示消息要到的发送主题,必填
flag:选填,消息的标记,完全由应用设置,RocketMQ不做任何处理,类似于memcached中flag的作用。
properties:消息属性,主要存储一些消息的元数据信息
body:消息的内容,这是一个字节数组,序列化方式由应用决定,例如你可以将一个json转为字节数组,也可以通过protol buffer、hessian编码转为字节数组。
tags:表示消息的标签,消费者在消费时,可以根据标签进行过滤,需要注意的是,一个生产者,只能指定一个tag
keys:用于建立索引,之后可以通过命令工具/API/或者管理平台查询key,可以为一个消息设置多个key,用空格""进行分割
消息查询
RocketMQ提供了3种消息查询方式:
按照Message Key 查询:消息的key是业务开发同学在发送消息之前自行指定的,通常会把具有业务含义,区分度高的字段作为消息的key,如用户id,订单id等。
按照Unique Key查询:除了业务开发同学明确的指定消息中的key,RocketMQ生产者客户端在发送发送消息之前,会自动生成一个UNIQ_KEY,设置到消息的属性中,从逻辑上唯一代表一条消息。
按照Message Id 查询:Message Id 是消息发送后,在Broker端生成的,其包含了Broker的地址,和在CommitLog中的偏移信息,并会将Message Id作为发送结果的一部分进行返回。Message Id中属于精确匹配,可以唯一定位一条消息,不需要使用哈希索引机制,查询效率更高。
SendResult [
sendStatus=SEND_OK,
msgId=C0A801030D4B18B4AAC247DE4A0D0000,
offsetMsgId=C0A8010300002A9F000000000007BEE9,
messageQueue=MessageQueue [topic=TopicA, brokerName=broker-a, queueId=0],
queueOffset=0]
msgId 实际是 Unique Key,
offsetMsgId:Broker返回的Message ID
messageQueue:消息发送到了哪个的队列,如上图显示发送到broker-a的第0个的队列
queueOffset:消息在队列中的偏移量,每次发送到一个队列时,offset+1
最佳实践
https://github.com/apache/rocketmq/blob/master/docs/cn/best_practice.md
1、Topic
一个应用尽可能用一个Topic,消息子类型用tags来标识,tags可以由应用自由设置。只有发送消息设置了tags,消费方在订阅消息时,才可以利用tags 在broker做消息过滤。
2、key
每个消息在业务层面的唯一标识码,要设置到 keys 字段,方便将来定位消息丢失问题。服务器会为每个消息创建索引(哈希索引),应用可以通过 topic,key来查询这条消息内容,以及消息被谁消费。由于是哈希索引,请务必保证key 尽可能唯一,这样可以避免潜在的哈希冲突。
//订单Id
String orderId= “20034568923546”;
message.setKeys(orderId);
3、日志
消息发送成功或者失败,要打印消息日志,务必要打印 send result 和key 字段。
4、send
send消息方法,只要不抛异常,就代表发送成功。但是发送成功会有多个状态,在sendResult里定义。
SEND_OK:消息发送成功
FLUSH_DISK_TIMEOUT:消息发送成功,但是服务器刷盘超时,消息已经进入服务器队列,只有此时服务器宕机,消息才会丢失
FLUSH_SLAVE_TIMEOUT:消息发送成功,但是服务器同步到Slave时超时,消息已经进入服务器队列,只有此时服务器宕机,消息才会丢失
SLAVE_NOT_AVAILABLE:消息发送成功,但是此时slave不可用,消息已经进入服务器队列,只有此时服务器宕机,消息才会丢失
Python 使用 RocketMQ
Producer
from rocketmq.client import Producer, Message
import json
producer = Producer('PID-test')
producer.set_namesrv_addr('ip:port') #rocketmq队列接口地址(服务器ip:port)
producer.start()
msg_body = {"id":"001","name":"test_mq","message":"abcdefg"}
ss = json.dumps(msg_body).encode('utf-8')
msg = Message('topic_name') #topic名称
msg.set_keys('set_key')
msg.set_tags('set_tag')
msg.set_body(ss) #message body
retmq = producer.send_sync(msg)
print(retmq.status, retmq.msg_id, retmq.offset)
producer.shutdown()
Consumer
from rocketmq.client import PullConsumer
consumer = PullConsumer('CID_test')
consumer.set_namesrv_addr('ip:port')
consumer.start()
for msg in consumer.pull('topic_name'):
print(msg.id, msg.body)
consumer.shutdown()
官方资料
https://github.com/apache/rocketmq/blob/master/docs/cn/RocketMQ_Example.md
https://github.com/apache/rocketmq-externals/blob/master/rocketmq-console/doc/1_0_0/UserGuide_CN.md
https://guozh.net/rocketmqzhiconsolejiankongpingtaishiyongxiangjiesan/
http://www.tianshouzhi.com/api/tutorials/rocketmq
https://github.com/apache/rocketmq/blob/master/docs/cn/best_practice.md
http://rocketmq.apache.org/docs/quick-start/