第二章 搭建RocketMQ (单机)

第二章 搭建RocketMQ (单机)

第一章 RocketMQ基础知识
第二章 搭建RocketMQ (单机)
第三章 搭建RocketMQ (集群)


目录

  • 第二章 搭建RocketMQ (单机)
  • 一、安装
    • 1.mqnamesrv:路由管理
    • 2.mqbroker:消息队列
    • 3.console:控制台
  • 二、运行&一些优化配置:
  • 三、Demo开发与测试
  • 四、遇到问题


一、安装

1.mqnamesrv:路由管理

详细步骤:https://rocketmq.apache.org/docs/quick-start/

2.mqbroker:消息队列

详细步骤:https://rocketmq.apache.org/docs/quick-start/

3.console:控制台

详细步骤:https://rocketmq-1.gitbook.io/rocketmq-connector/rocketmq-connect/rocketmq-console/an-zhuang-shi-yong

二、运行&一些优化配置:

   由于broker启动时,需要占用大量内存,测试时,可以修改配置文件,以减小内存的消耗。
  1. 修改bin目录下的runserver.sh文件:
JAVA_OPT="${JAVA_OPT} -server -Xms256m -Xmx256m -Xmn128m -XX:PermSize=128m -XX:MaxPermSize=320m"
  1. 修改runbroker.sh文件:
JAVA_OPT="${JAVA_OPT} -server -Xms256m -Xmx256m -Xmn128m
  1. 修改tools.sh文件:
JAVA_OPT="${JAVA_OPT} -server -Xms256m -Xmx256m -Xmn128m -XX:PermSize=128m -XX:MaxPermSize=128m"
  1. 日志输出
默认地址为:~/logs/rocketmqlogs/***.log

三、Demo开发与测试

代码地址:https://github.com/yawpei/rocketmq-demo.git

  1. 引包
        <dependency>
            <groupId>org.apache.rocketmqgroupId>
            <artifactId>rocketmq-clientartifactId>
            <version>4.9.3version>
        dependency>
  1. 消费者
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");
    }
}
  1. 生产者
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();
    }
}

四、遇到问题

  1. 无法连接路由–已解决
    堆栈信息:
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默认监听端口)

你可能感兴趣的:(RocketMQ,中间件,RocketMQ)