阿里rocketmq正式成为apache项目,也于年前刚发布4.0.0版本,本文初试服务端和客户端
1 服务端安装
1.0 安装准备
CentOS release 6.8 (Final)
- 64bit OS, Linux/Unix/Mac is recommended;
- 64bit JDK 1.7+;
- Maven 3.2.x
- Git
1.1 编译
-
> git clone https://github.com/apache/incubator-rocketmq.git
-
> cd incubator-rocketmq
-
> mvn clean package install -Prelease-all assembly:assembly -U
-
> cd target/apache-rocketmq-all/
1.2 启动NameServer
-
> nohup sh bin/mqnamesrv &
-
> tail -f ~/logs/rocketmqlogs/namesrv.log
-
The Name Server boot success...
1.3 启动Broker
-
> nohup sh bin/mqbroker -n localhost:9876 &
-
> tail -f ~/logs/rocketmqlogs/broker.log
-
The broker[%s, 172.30.30.233:10911] boot success...
2 客户端
2.1 客户示例客户端
运行生产者producer
-
> export NAMESRV_ADDR=localhost:9876
-
> sh bin/tools.sh org.apache.rocketmq.example.quickstart.Producer
-
SendResult [sendStatus=SEND_OK, msgId= ...
运行消费者
-
> sh bin/tools.sh org.apache.rocketmq.example.quickstart.Consumer
-
ConsumeMessageThread_%d Receive New Messages: [MessageExt...
2.2 java客户端编写
pom.xmlsg
<rocketmq.version>4.0.0-incubating</rocketmq.version>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>${rocketmq.version}</version>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-all</artifactId>
<version>${rocketmq.version}</version>
</dependency>
生产者
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 java.util.concurrent.TimeUnit;
public class Producer {
public static void main(String[] args) throws MQClientException,
InterruptedException {
/**
* 一个应用创建一个Producer,由应用来维护此对象,可以设置为全局对象或者单例<br>
* 注意:ProducerGroupName需要由应用来保证唯一<br>
* ProducerGroup这个概念发送普通的消息时,作用不大,但是发送分布式事务消息时,比较关键,
* 因为服务器会回查这个Group下的任意一个Producer
*/
DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName");
producer.setNamesrvAddr("192.168.56.101:9876");
producer.setInstanceName("Producer");
producer.setVipChannelEnabled(false);
/**
* Producer对象在使用之前必须要调用start初始化,初始化一次即可<br>
* 注意:切记不可以在每次发送消息时,都调用start方法
*/
producer.start();
/**
* 下面这段代码表明一个Producer对象可以发送多个topic,多个tag的消息。
* 注意:send方法是同步调用,只要不抛异常就标识成功。但是发送成功也可会有多种状态,<br>
* 例如消息写入Master成功,但是Slave不成功,这种情况消息属于成功,但是对于个别应用如果对消息可靠性要求极高,<br>
* 需要对这种情况做处理。另外,消息可能会存在发送失败的情况,失败重试由应用来处理。
*/
for (int i = 0; i < 1; i++) {
try {
{
Message msg = new Message("TopicTest1",// topic
"TagA",// tag
"OrderID001",// key
("Hello MetaQ").getBytes());// body
SendResult sendResult = producer.send(msg);
System.out.println(sendResult);
}
{
Message msg = new Message("TopicTest2",// topic
"TagB",// tag
"OrderID0034",// key
("Hello MetaQ").getBytes());// body
SendResult sendResult = producer.send(msg);
System.out.println(sendResult);
}
{
Message msg = new Message("TopicTest3",// topic
"TagC",// tag
"OrderID061",// key
("Hello MetaQ").getBytes());// body
SendResult sendResult = producer.send(msg);
System.out.println(sendResult);
}
} catch (Exception e) {
e.printStackTrace();
}
TimeUnit.MILLISECONDS.sleep(1000);
}
/**
* 应用退出时,要调用shutdown来清理资源,关闭网络连接,从MetaQ服务器上注销自己
* 注意:我们建议应用在JBOSS、Tomcat等容器的退出钩子里调用shutdown方法
*/
producer.shutdown();
}
}
消费者
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;
public class PushConsumer {
/**
* 当前例子是PushConsumer用法,使用方式给用户感觉是消息从RocketMQ服务器推到了应用客户端。<br>
* 但是实际PushConsumer内部是使用长轮询Pull方式从MetaQ服务器拉消息,然后再回调用户Listener方法<br>
*/
public static void main(String[] args) throws InterruptedException,
MQClientException {
/**
* 一个应用创建一个Consumer,由应用来维护此对象,可以设置为全局对象或者单例<br>
* 注意:ConsumerGroupName需要由应用来保证唯一
*/
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(
"ConsumerGroupName");
consumer.setNamesrvAddr("192.168.56.101:9876");
consumer.setInstanceName("Consumber");
/**
* 订阅指定topic下tags分别等于TagA或TagC或TagD
*/
consumer.subscribe("TopicTest1", "TagA || TagC || TagD");
/**
* 订阅指定topic下所有消息<br>
* 注意:一个consumer对象可以订阅多个topic
*/
consumer.subscribe("TopicTest2", "*");
consumer.registerMessageListener(new MessageListenerConcurrently() {
/**
* 默认msgs里只有一条消息,可以通过设置consumeMessageBatchMaxSize参数来批量接收消息
*/
@Override
public ConsumeConcurrentlyStatus consumeMessage(
List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
System.out.println(Thread.currentThread().getName()
+ " Receive New Messages: " + msgs.size());
MessageExt msg = msgs.get(0);
if (msg.getTopic().equals("TopicTest1")) {
// 执行TopicTest1的消费逻辑
if (msg.getTags() != null && msg.getTags().equals("TagA")) {
// 执行TagA的消费
System.out.println(new String(msg.getBody()));
} else if (msg.getTags() != null
&& msg.getTags().equals("TagC")) {
// 执行TagC的消费
} else if (msg.getTags() != null
&& msg.getTags().equals("TagD")) {
// 执行TagD的消费
}
} else if (msg.getTopic().equals("TopicTest2")) {
System.out.println(new String(msg.getBody()));
}
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
/**
* Consumer对象在使用之前必须要调用start初始化,初始化一次即可<br>
*/
consumer.start();
System.out.println("Consumer Started.");
}
}
安装中问题汇总:
> nohup sh bin/mqnamesrv &
先不使用以上命令,如果出错,不会有显示
使用
sh bin/mqnamesrv
出现以下问题
-
#
-
# There is insufficient memory for the Java Runtime Environment to continue.
-
# Native memory allocation (mmap) failed to map 805306368 bytes for committing reserved memory.
-
# Possible reasons:
-
# The system is out of physical RAM or swap space
-
# In 32 bit mode, the process size limit was hit
-
# Possible solutions:
-
# Reduce memory load on the system
-
# Increase physical memory or swap space
-
# Check if swap backing store is full
-
# Use 64 bit Java on a 64 bit OS
-
# Decrease Java heap size (-Xmx/-Xms)
-
# Decrease number of Java threads
-
# Decrease Java thread stack sizes (-Xss)
-
# Set larger code cache with -XX:ReservedCodeCacheSize=
-
# This output file may be truncated or incomplete.
-
#
-
# Out of Memory Error (os_linux.cpp:2627), pid=1864, tid=0x00007f94d8319700
原因:rocketmq默认jvm配置较高,导致内存不足
解决:
runserver.sh
改成
JAVA_OPT="${JAVA_OPT} -server -Xms256m -Xmx256m -Xmn128m -XX:PermSize=128m -XX:MaxPermSize=320m"
同理,运行其他命令也建议先使用sh来运行,解决方法如下
runbroker.sh
JAVA_OPT="${JAVA_OPT} -server -Xms8g -Xmx8g -Xmn4g"
改成:
JAVA_OPT="${JAVA_OPT} -server -Xms256m -Xmx256m -Xmn128m
tools.sh
JAVA_OPT="${JAVA_OPT} -server -Xms1g -Xmx1g -Xmn256m -XX:PermSize=128m -XX:MaxPermSize=128m"
改成
JAVA_OPT="${JAVA_OPT} -server -Xms256m -Xmx256m -Xmn128m -XX:PermSize=128m -XX:MaxPermSize=128m"
调试时问题汇总:
运行java客户端的Producer.java,出现
Caused by: org.apache.rocketmq.remoting.exception.RemotingConnectException: connect to <*****:10911> failed |
经调试,
org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl
String brokerAddr = this.mQClientFactory.findBrokerAddressInPublish(mq.getBrokerName());
通过broderName获取到的ip不正确
解决:临时解决方案是断点在该代码下方,将brokerAddr设置为正确ip
在安装时,我们会出现nohup.out日志文件的困惑。
解答:这里其实就跟我们log4j一样,有一个catalina.out的文件一样,这个表示把日志文件放到nohup.out的文件流中。我们
查看:tail -100f nohup.out
相关文件:
https://rocketmq.incubator.apache.org/docs/quick-start/
http://www.tuicool.com/articles/mEBVneB