单机安装启动步骤如下:
1:下载,并解压安装包,window截图如下:
2:配置JAVA ,ROCKETMQ 环境变量,因为很简单,我直接贴进来两个链接,按照步骤操作即可
3:启动NameServer
window : 切换到ROCKETMQ 的bin目录下执行命令: start start mqnamesrv.cmd
弹出如下图所示证明nameServer启动成功
也可以打开日志查看nameServer的启动日志,日志路径地址: ${user}/logs/rocketmqlogs/namesrv.log
如图:
如需要修改日志路径,通过修改%ROCKETMQ_HOME%conf/logback_namesrv.xml 中的路径
4:启动Broker
window : 切换到ROCKETMQ 的bin目录下执行命令: start mqbroker.cmd
弹出如下图所示证明Broker启动成功
同样也可以打开日志查看Broker的启动日志,日志路径地址: ${user}/logs/rocketmqlogs/broker.log
如图:
如需要修改日志路径,通过修改%ROCKETMQ_HOME%conf/logback_broker.xml 中的路径。
附件为本人修改日志路径为ROCKETMQ根目录下配置文件, 启动时需要切换到bin目录下
logback_broker.xmllogback_namesrv.xml
此时最简单的服务端单机ROCKETMQ已经搭建并启动成功。接下来就是producer 和consumer收发消息示例
5:生产者发送消息最简配置代码如下(因为是入门案例,我们删除一切可以删去的配置项,做到最简,后面慢慢不断完善):
package com.dsx.standalone;
import com.alibaba.fastjson.JSONObject;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
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.exception.RemotingException;
/**
* @author : tianwenqing
* @version : 1.0
* @date : 2020-06-01 14:59
* @description :
**/
public class ProducerTest {
public static void main(String[] args) throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
DefaultMQProducer producer = new DefaultMQProducer();
//必须设置生产者组,否则会抛出异常:
//Exception in thread "main" org.apache.rocketmq.client.exception.MQClientException: producerGroup can not equal DEFAULT_PRODUCER, please specify another one.
//这个校验在 at org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl.checkConfig(DefaultMQProducerImpl.java:242)
producer.setProducerGroup("test-2");
//TODO
producer.setNamesrvAddr("192.168.0.105:9876");
//创建启动生产者
producer.start();
Message message = new Message();
//必须指定发送到哪个topic,否则会抛出异常
//Exception in thread "main" org.apache.rocketmq.client.exception.MQClientException: The specified topic is blank
//这个校验在 at org.apache.rocketmq.client.Validators.checkTopic(Validators.java:106)
message.setTopic("firsTopic2");
//必须指定消息体,否则抛出异常
//Exception in thread "main" org.apache.rocketmq.client.exception.MQClientException: CODE: 13 DESC: the message body is null
//这个校验在 at org.apache.rocketmq.client.Validators.checkMessage(Validators.java:91)
message.setBody("你好".getBytes());
//发送消息
SendResult send = producer.send(message);
System.out.println(JSONObject.toJSONString(send));
//关闭发送者
producer.shutdown();
}
}
附源码地址:https://gitee.com/disanxian/home_rocketmq_test.git
6:消费者接受消息示例代码:
package com.dsx.standalone;
import com.alibaba.fastjson.JSONObject;
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.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;
import java.util.List;
/**
* @author : tianwenqing
* @version : 1.0
* @date : 2020-06-02 11:02
* @description :
**/
public class ConsumerTest {
public static void main(String[] args) throws MQClientException {
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer();
//必须设置消费组,否则抛出异常:
//Exception in thread "main" org.apache.rocketmq.client.exception.MQClientException: consumerGroup can not equal DEFAULT_CONSUMER, please specify another one.
//这个校验再 at org.apache.rocketmq.client.impl.consumer.DefaultMQPushConsumerImpl.checkConfig(DefaultMQPushConsumerImpl.java:670)
consumer.setConsumerGroup("consumer_group_3");
//必须设置namesrv,如发送者抛出异常一致:
//Exception in thread "main" java.lang.IllegalStateException: org.apache.rocketmq.remoting.exception.RemotingConnectException: connect to null failed
consumer.setNamesrvAddr("rocket-ns-2.yunzong:9876");
consumer.subscribe("parking-gateway-2","*");
//必须设置消息监听器,否则抛出异常:
//Exception in thread "main" org.apache.rocketmq.client.exception.MQClientException: messageListener is null
//这个校验再 at org.apache.rocketmq.client.impl.consumer.DefaultMQPushConsumerImpl.checkConfig(DefaultMQPushConsumerImpl.java:716)
//consumer.setMessageListener((MessageListenerConcurrently) ConsumerTest::consumeMessage);
consumer.registerMessageListener(new MessageListenerConcurrently() {
@Override
public ConsumeConcurrentlyStatus consumeMessage(List msgs, ConsumeConcurrentlyContext context) {
System.out.println(msgs);
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
consumer.start();
System.out.println("启动成功");
}
private static ConsumeConcurrentlyStatus consumeMessage(List msgs, ConsumeConcurrentlyContext context) {
System.out.println(JSONObject.toJSONString(msgs));
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
}
附源码地址:https://gitee.com/disanxian/home_rocketmq_test.git
7:单机环境配置参数:
配置参数分为三部分介绍:
1:NameServer ,Broker运行的虚拟机环境
运行时环境是指对NameServer, Broker 运行时JVM参数配置,配置路径在:
%ROCKETMQ_HOME/bin/runserver.cmd
%ROCKETMQ_HOME/bin/runbroker.cmd
%ROCKETMQ_HOME/bin/tools.cmd
分别根据实际运行情况修改文件示例代码,修改JVM参数
set "JAVA_OPT=%JAVA_OPT% -server -Xms1g -Xmx1g -Xmn256m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m
2:NameServer,Broker本身的配置
配置文件配置地址:%ROCKETMQ_HOME/conf/broker.conf
变量名 |
默认值 |
含义 |
备注 |
listenPort |
10911 |
客户端监听boke的端口 |
|
namesrvAddr |
|
nameServer地址,带端口号,默认端口号为:9876 |
当多个的时候,用逗号(,)隔开 |
brokerIP1 |
|
指定bokerIP地址 |
当物理机含有多个ip时,如果使用私有ip发布,则存在客户端不能访问的问题 |
brokerId | 0 |
当为master的时候,id为0 |
|
brokerRole | ASYNC_MASTER |
Broker角色,有同步主,异步主,从 三种角色 |
|
3:输出日志配置
日志输出配置也在%ROCKETMQ_HOME/conf 下,上文中单机启动NameServer ,Broker时已经介绍查看和修改方法。
FAQ
1:通过运维命令查看集群信息 :mqadmin clusterList -n 127.0.0.1
输出结果如图
表示并没有集群信息展示,怀疑是没有读到namespace信息,重新启动broker,手动指定配置文件地址(配置文件中配置namesrvAddr 参数):
start mqbroker.cmd -c D:\software\work\rocketmq-all-4.7.0-bin-release\conf\broker.conf
重新查看集群信息:
这时已经启动成功,可以正常发送消息,所以此处的问题是单机启动的时候broker并没有读取到conf/broker.conf 配置文件,如何能读取到,具体的规则还未知。
2:发送消息,报错内容如下:
RocketMQLog:WARN No appenders could be found for logger (io.netty.util.internal.PlatformDependent0).
RocketMQLog:WARN Please initialize the logger system properly.
Exception in thread "main" org.apache.rocketmq.client.exception.MQClientException: No route info of this topic: firsTopic
See http://rocketmq.apache.org/docs/faq/ for further details.
at org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl.sendDefaultImpl(DefaultMQProducerImpl.java:684)
at org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl.send(DefaultMQProducerImpl.java:1342)
at org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl.send(DefaultMQProducerImpl.java:1288)
at org.apache.rocketmq.client.producer.DefaultMQProducer.send(DefaultMQProducer.java:324)
at com.dsx.standalone.ProducerTest.main(ProducerTest.java:44)
没有topic路由,我们这里通过运维开启自动创建topic来解决。在broker.conf中添加如下配置,自动创建topic,默认开启
autoCreateTopicEnable=true