RocketMQ Initial Trampling Notes

This article mainly talks about installing RocketMQ in Centos and doing simple examples. If you install 100% according to this article, you can succeed. If you follow the official instructions of Ali, it can only be heed.

install

The official address is https://rocketmq.apache.org/docs/quick-start./
I installed the following:

//Download the latest rocketmq
wget http://apache-mirror.8birdsvideo.com/rocketmq/4.4.0/rocketmq-all-4.4.0-bin-release.zip

//decompression
unzip rocketmq-all-4.4.0-bin-release.zip

//Switch to mq directory
cd rocketmq-all-4.4.0-bin-release

//name server startup
nohup ./bin/mqnamesrv -n 111.231.XX.XX:9876 &

//- c conf/broker.conf autoCreateTopicEnable=true parameter needs to be brought, otherwise topic needs to be created manually
nohup sh bin/mqbroker -n 111.231.XX.XX:9876 -c conf/broker.conf autoCreateTopicEnable=true &

Configuration, switch to the bin directory of mq

cd rocketmq-all-4.4.0-bin-release/bin

rocketmq defaults to a minimum memory of 4g. If the machine memory is not enough, find runserver.sh and runbroker.sh to edit as follows:

JAVA_OPT="${JAVA_OPT} -server -Xms256m -Xmx256m -Xmn125m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"

Function

Running the official demo, the following errors were found:

21:20:22.249 [NettyClientSelector_1] INFO  RocketmqRemoting - closeChannel: close the connection to remote address[] result: true
org.apache.rocketmq.remoting.exception.RemotingTooMuchRequestException: sendDefaultImpl call timeout
    at org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl.sendDefaultImpl(DefaultMQProducerImpl.java:640)
    at org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl.send(DefaultMQProducerImpl.java:1310)
    at org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl.send(DefaultMQProducerImpl.java:1256)
    at org.apache.rocketmq.client.producer.DefaultMQProducer.send(DefaultMQProducer.java:339)
    at org.apache.rocketmq.example.simple.Producer.main(Producer.java:40)

Run the following command to view the broker configuration and write to the remote ip address:

//View broker configuration
sh ./bin/mqbroker -m

//Close broker
sh bin/mqshutdown broker

//Write native remote ip to configuration file
echo 'brokerIP1=111.231.XX.XX' > conf/broker.properties 

//Restart broker
nohup sh bin/mqbroker -n 111.231.XX.XX:9876 -c conf/broker.conf autoCreateTopicEnable=true &

Management Console Installation

Git address: https://github.com/apache/rocketmq-externals/tree/master/rocketmq-console

git clone git@github.com:apache/rocketmq-externals.git
cd  rocketmq-external/rocketmq-console/
mvn clean package -Dmaven.test.skip=true

After typing the package, run the following commands

java -jar rocketmq-console-ng-1.0.1.jar --server.port=12181 --rocketmq.config.namesrvAddr=111.231.XX.XX:9876

Open http://localhost:12181 to access the console as follows

The following exceptions occur when querying the Procuder page:

java.lang.RuntimeException: org.apache.rocketmq.client.exception.MQBrokerException: CODE: 1  DESC: the producer group[] not exist
For more information, please visit the url, http://rocketmq.apache.org/docs/faq/
        at com.google.common.base.Throwables.propagate(Throwables.java:160)
        at org.apache.rocketmq.console.service.impl.ProducerServiceImpl.getProducerConnection(ProducerServiceImpl.java:38)
        at org.apache.rocketmq.console.controller.ProducerController.producerConnection(ProducerController.java:39)

Please comment out the word producer.shutdown() in the code and add it to the production environment.

 //producer.shutdown();

Code examples (official)

Producer

package org.apache.rocketmq.example.simple;

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.common.RemotingHelper;

public class Producer {
    public static void main(String[] args) throws MQClientException, InterruptedException {

        DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName");

        producer.setNamesrvAddr("111.231.XX.XX:9876");
        producer.start();

        for (int i = 0; i < 10; i++)
            try {
                {
                    Message msg = new Message("TopicTest",
                        "TagA",
                        "OrderID188",
                        "Hello world".getBytes(RemotingHelper.DEFAULT_CHARSET));
                    SendResult sendResult = producer.send(msg);
                    System.out.printf("%s%n", sendResult);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        //producer.shutdown();
    }
}

Consumer

package org.apache.rocketmq.example.simple;

import java.util.List;
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.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.MessageExt;

public class PushConsumer {

    public static void main(String[] args) throws InterruptedException, MQClientException {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("CID_JODIE_1");
        consumer.subscribe("TopicTest", "*");
        consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
        //wrong time format 2017_0422_221800
        //consumer.setConsumeTimestamp("20181109221800");
        consumer.setNamesrvAddr("111.231.XX.XX:9876");
        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");
    }
}

There are more articles, please pay attention to them and send them to us for interviews.

Keywords: Java Apache git github

Added by edup_pt on Tue, 08 Oct 2019 04:14:49 +0300