第一章 RocketMQ基础知识
第二章 搭建RocketMQ (单机)
第三章 搭建RocketMQ (集群)
详细步骤:https://rocketmq.apache.org/docs/quick-start/
详细步骤:https://rocketmq.apache.org/docs/quick-start/
详细步骤:https://rocketmq-1.gitbook.io/rocketmq-connector/rocketmq-connect/rocketmq-console/an-zhuang-shi-yong
由于broker启动时,需要占用大量内存,测试时,可以修改配置文件,以减小内存的消耗。
JAVA_OPT="${JAVA_OPT} -server -Xms256m -Xmx256m -Xmn128m -XX:PermSize=128m -XX:MaxPermSize=320m"
JAVA_OPT="${JAVA_OPT} -server -Xms256m -Xmx256m -Xmn128m
JAVA_OPT="${JAVA_OPT} -server -Xms256m -Xmx256m -Xmn128m -XX:PermSize=128m -XX:MaxPermSize=128m"
默认地址为:~/logs/rocketmqlogs/***.log
代码地址:https://github.com/yawpei/rocketmq-demo.git
<dependency>
<groupId>org.apache.rocketmqgroupId>
<artifactId>rocketmq-clientartifactId>
<version>4.9.3version>
dependency>
import java.util.List;
/**
* 消费者
*/
public class Consumer {
public static void main(String[] args) throws InterruptedException, MQClientException {
// 消费者组名
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("consumer-group");
// Specify name server addresses.
consumer.setNamesrvAddr("127.0.0.1:9876");
// Subscribe one more more topics to consume.
consumer.subscribe("TopicTest", "*");
// Register callback to execute on arrival of messages fetched from brokers.
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;
}
});
//Launch the consumer instance.
consumer.start();
System.out.printf("Consumer Started.%n");
}
}
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 SyncProducer {
public static void main(String[] args) throws Exception {
//生产者组
DefaultMQProducer producer = new
DefaultMQProducer("sync-producer-group");
// 指定路由地址
producer.setNamesrvAddr("127.0.0.1:9876");
producer.start();
for (int i = 0; i < 100; i++) {
//创建消息实例,指定主题、标签和消息体
Message msg = new Message("TopicTest" ,
"TagA" ,
("Hello RocketMQ " +
i).getBytes(RemotingHelper.DEFAULT_CHARSET)
);
//发送消息
SendResult sendResult = producer.send(msg);
System.out.println(sendResult);
}
//关闭生产者
producer.shutdown();
}
}
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
/**
* 生产者,异步
*/
public class AsyncProducer {
public static void main(String[] args) throws Exception {
//生产者组
DefaultMQProducer producer = new DefaultMQProducer("async-producer-group");
// 指定路由地址
producer.setNamesrvAddr("127.0.0.1:9876");
producer.start();
//设置发送异步失败时的重试次数
producer.setRetryTimesWhenSendAsyncFailed(0);
int messageCount = 100;
final CountDownLatch countDownLatch = new CountDownLatch(messageCount);
for (int i = 0; i < messageCount; i++) {
try {
final int index = i;
Message msg = new Message("TopicTest",
"TagB",
"async01",
"Hello world".getBytes(RemotingHelper.DEFAULT_CHARSET));
producer.send(msg, new SendCallback() {
@Override
public void onSuccess(SendResult sendResult) {
countDownLatch.countDown();
System.out.printf("%-10d OK %s %n", index, sendResult.getMsgId());
}
@Override
public void onException(Throwable e) {
countDownLatch.countDown();
System.out.printf("%-10d Exception %s %n", index, e);
e.printStackTrace();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
countDownLatch.await(5, TimeUnit.SECONDS);
producer.shutdown();
}
}
at org.apache.rocketmq.remoting.exception.RemotingTooMuchRequestException: sendDefaultImpl call timeout
at org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl.sendDefaultImpl(DefaultMQProducerImpl.java:588)
at org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl.send(DefaultMQProducerImpl.java:1223)
at org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl.send(DefaultMQProducerImpl.java:1173)
at org.apache.rocketmq.client.producer.DefaultMQProducer.send(DefaultMQProducer.java:214)
at com.lczq.rocketmq.demo.producer.SyncProducer.main(SyncProducer.java:27)
解决办法:配置conf/broker.propeties,添加brokerIP1=127.0.0.1,放开对外端口:10911(broker默认监听端口)