1. 下载最新的RocketMQ
2. 将下载软件解压到指定目录下
本人解压到: /Users/Joker/Documents/software/rocketmq-all-4.7.0下
3. 编辑配置文件
修改配置文件前,需要新建logs文件夹,我是建在rocketmq包下面的,即 /Users/Joker/Documents/software/rocketmq-all-4.7.0/logs
位置:conf/2m-noslave/broker-a.properties
内容:
#所属集群名字
brokerClusterName=DefaultCluster
#broker名字,注意此处不同的配置文件填写的不一样
brokerName=broker-a
#0 表示 Master,>0 表示 Slave
brokerId=0
#nameServer地址,分号分割
namesrvAddr=127.0.0.1:9876
#关键
brokerIP1=127.0.0.1
#在发送消息时,自动创建服务器不存在的topic,默认创建的队列数
defaultTopicQueueNums=4
#是否允许 Broker 自动创建Topic,建议线下开启,线上关闭
autoCreateTopicEnable=true
#是否允许 Broker 自动创建订阅组,建议线下开启,线上关闭
autoCreateSubscriptionGroup=true
#Broker 对外服务的监听端口
listenPort=10911
#删除文件时间点,默认凌晨 4点
deleteWhen=04
#文件保留时间,默认 48 小时
fileReservedTime=48
#commitLog每个文件的大小默认1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue每个文件默认存30W条,根据业务情况调整
mapedFileSizeConsumeQueue=300000
#destroyMapedFileIntervalForcibly=120000
#redeleteHangedFileInterval=120000
#检测物理文件磁盘空间
diskMaxUsedSpaceRatio=88
#日志配置
#存储路径
storePathRootDir=/Users/Joker/Documents/software/rocketmq-all-4.7.0/logs
#commitLog 存储路径
storePathCommitLog=/Users/Joker/Documents/software/rocketmq-all-4.7.0/logs/commitlog
#消费队列存储路径存储路径
storePathConsumeQueue=/Users/Joker/Documents/software/rocketmq-all-4.7.0/consumequeue
#消息索引存储路径
storePathIndex=/Users/Joker/Documents/software/rocketmq-all-4.7.0/logs/index
#checkpoint 文件存储路径
storeCheckpoint=/Users/Joker/Documents/software/rocketmq-all-4.7.0/logs/checkpoint
#abort 文件存储路径
abortFile=/Users/Joker/Documents/software/rocketmq-all-4.7.0/logs/abort
#限制的消息大小
maxMessageSize=65536
#flushCommitLogLeastPages=4
#flushConsumeQueueLeastPages=2
#flushCommitLogThoroughInterval=10000
#flushConsumeQueueThoroughInterval=60000
#Broker 的角色
#- ASYNC_MASTER 异步复制Master
#- SYNC_MASTER 同步双写Master
#- SLAVE
brokerRole=ASYNC_MASTER
#刷盘方式
#- ASYNC_FLUSH 异步刷盘
#- SYNC_FLUSH 同步刷盘
flushDiskType=ASYNC_FLUSH
#checkTransactionMessageEnable=false
#发消息线程池数量
#sendMessageThreadPoolNums=128
#拉消息线程池数量
#pullMessageThreadPoolNums=128
4. 修改profile文件
需要配置jdk
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home
export ROCKETMQ_HOME=/Users/Joker/Documents/software/rocketmq-all-4.7.0
export MAVEN_HOME=/Users/Joker/Documents/software/maven-3.6.3
export GRADLE_HOME=/Users/Joker/Documents/software/gradle-6.4
export PATH=$PATH:$JAVA_HOME:$MAVEN_HOME/bin:$GRADLE_HOME/bin:$ROCKETMQ_HOME/bin:$PATH
source ~/.profile
4. 执行启动命令
nohup sh /Users/Joker/Documents/software/rocketmq-all-4.7.0/bin/mqnamesrv > /Users/Joker/Documents/software/rocketmq-all-4.7.0/logs/mqnamesrv.log 2>&1 &
nohup sh /Users/Joker/Documents/software/rocketmq-all-4.7.0/bin/mqbroker -n 127.0.0.1:9876 -c /Users/Joker/Documents/software/rocketmq-all-4.7.0/conf/2m-noslave/broker-a.properties > /Users/Joker/Documents/software/rocketmq-all-4.7.0/logs/mqbroker.log 2>&1 &
关于 autoCreateTopicEnable=true的说明:
RocketMQ的4.4.0版本默认开启自动创建Topic,所以启动broker的时候不需要此属性,低版本则需要添加此属性, 在地址后面加 autoCreateTopicEnable=true
5. 通过jps命令查看是否启动成功
(1) 出现下面的图片内容,则启动成功
(2) 检查下nameserver中是否成功注册了broker
使用命令:sh mqadmin clusterList -n 127.0.0.1:9876 出现下面图片即成功注册
6. 添加pom文件
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>4.7.0</version>
</dependency>
7. 创建MQProducer类,同步消息发送
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
public class MQProducer {
public static void main(String[] args) throws Exception {
// 实例化消息生产者Producer
DefaultMQProducer producer = new DefaultMQProducer("DefaultCluster");
// 设置NameServer的地址
producer.setNamesrvAddr("127.0.0.1:9876");
producer.setSendMsgTimeout(10000);
// 启动Producer实例
producer.start();
for (int i = 0; i < 100; i++) {
// 创建消息,并指定Topic,Tag和消息体
Message msg = new Message("TopicTest" /* Topic */,
"TagA" /* Tag */,
("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */
);
// 发送消息到一个Broker
SendResult sendResult = producer.send(msg);
// 通过sendResult返回消息是否成功送达
System.out.printf("%s%n", sendResult);
}
// 如果不再发送消息,关闭Producer实例。
producer.shutdown();
}
}
8. 创建MQConsumer类
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.common.message.MessageExt;
import java.util.List;
public class MQConsumer {
public static void main(String[] args) throws Exception {
// 实例化消费者
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("DefaultCluster");
// 设置NameServer的地址
consumer.setNamesrvAddr("127.0.0.1:9876");
consumer.setConsumeTimeout(10000);
// 订阅一个或者多个Topic,以及Tag来过滤需要消费的消息
consumer.subscribe("TopicTest", "*");
// 注册回调实现类来处理从broker拉取回来的消息
consumer.registerMessageListener(new MessageListenerConcurrently() {
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
System.out.printf("%s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs);
// 标记该消息已经被成功消费
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
// 启动消费者实例
consumer.start();
System.out.printf("Consumer Started.%n");
}
}
9. 启动生产者和消费者
可以从控制台看到相应输出