Linux installation and deployment RocketMq with console

Today I learned about Ali’s awesome messaging middleware RocketMq, installed and practiced it according to the official website, and encountered some pits, but in the end it was resolved. Now I will make a summary.

Official website address: http://rocketmq.apache.org/

The download is the latest version: 4.4.0
rocketmq-all-4.4.0-source-release.zip

First unzip, through the command unzip
unzip rocketmq-all-4.4.0-source-release.zip

Then enter the rocketmq-all-4.4.0 directory to compile, using maven
mvn -Prelease-all -DskipTests clean install -U
after success, enter the apache-rocketmq directory

cd distribution/target/apache-rocketmq

Start the Name Server through the background startup mode
nohup sh bin/mqnamesrv &

See the following instructions to start successfully
Insert picture description here
and then start Broker
nohup sh bin/mqbroker -n localhost:9876 &
Insert picture description here
start Ok

Then you can send and receive messages.
Sending class:

package com.demo.service.business;

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;

/**
 * @author huangdi
 * @Description: TODO
 * @date 2019/2/25 11:46
 */
public class SyncProducer {
    public static void main(String[] args) throws Exception {
        //Instantiate with a producer group name.
        DefaultMQProducer producer = new
                DefaultMQProducer("TEST_PRODUCER_GROUP");
        // Specify name server addresses.
        producer.setNamesrvAddr("192.168.x.xx:9876");
        //Launch the instance.
        producer.start();
        for (int i = 0; i < 1; i++) {
            //Create a message instance, specifying topic, tag and message body.
            Message msg = new Message("CONSUMER-CONTACT-TOPIC" ,
                    null ,
                    ("Hello RocketMQ ").getBytes(RemotingHelper.DEFAULT_CHARSET)
            );
            //Call send message to deliver message to one of brokers.
            SendResult sendResult = producer.send(msg);
            System.out.printf("%s%n", sendResult);
        }
        //Shut down once the producer instance is not longer in use.
        producer.shutdown();
    }
}

consumer:

package com.demo.service.business;

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.Message;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * @author huangdi
 * @Description: TODO
 * @date 2019/2/25 12:56
 */
public class Consume {

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

        // Instantiate with specified consumer group name.
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("rename_unique_group_name");

        // Specify name server addresses.
        consumer.setNamesrvAddr("192.168.x.xx:9876");

        // Subscribe one more more topics to consume.
        consumer.subscribe("CONSUMER-CONTACT-TOPIC", "*");
        // 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);
                for (Message msg : msgs) {
                    String jsonStr = new String(msg.getBody());
                    System.out.println("receive is " + jsonStr);
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });

        //Launch the consumer instance.
        consumer.start();

        System.out.printf("Consumer Started.%n");
    }
}

I thought it was that simple, but I still made a mistake. The first mistake:
Insert picture description here
TOPIC will not be created automatically. It is said on the Internet that it is not turned on: autoCreateTopicEnable=true. You can add this parameter through the startup command. I didn't try it here. It might succeed. My approach is to add this TOPIC directly in the console

The RocketMq console configuration process is also very simple, just a jar package, just start it, the steps are as follows:
https://github.com/apache/rocketmq-externals Download from github

Enter rocketmq-console under the folder of the console project, you can modify the configuration of the console, I modified the management background access context path, the default is empty
$ vi src/main/resources/application.properties
# management background access context path , The default is empty, if you fill in, you must add "/" before it, don't add it after it, otherwise it will start to report an error
server.contextPath=/rocketmq #Access
port
server.port=80

Type the project into a jar package and run the jar file.

$ mvn clean package -Dmaven.test.skip=true

$ java -jar target/rocketmq-console-ng-1.0.0.jar --rocketmq.config.namesrvAddr=‘192.168.x.xx:9876’

After the startup is successful, visit the address http://192.168.x.xx/rocketmq to enter the management background operation.

Insert picture description here
Add CONSUMER-CONTACT-TOPIC
and run it again. This time it should be no problem. I went to,,, and reported an error again. This time it was another error:

Insert picture description here
So I checked the information on the Internet, and it was said that it was because of the firewall problem. I tried many ways to no avail. . . I'm wondering why there are problems with Anzhao's official website. Could it be that I missed something? I looked for it carefully and didn't find the difference, which is really puzzling. . . . . .

Huang Tian paid off, and finally let me find the information, which was provided by the great god on the Internet. He wrote this:
The startup command on the official website is very pitted. When it is started, the broker will be started via a private ip, which will cause the client to be unable to remotely. Connect, so we need to modify the configuration file before starting. . .
So I changed it again according to his introduction, mainly to change the broker.conf file,
Insert picture description here
change the nameserver to the IP of the machine, and the broker to the host IP.

Start the command again:
nohup sh bin/mqbroker -n localhost:9876 autoCreateTopicEnable=true -c conf/broker.conf &

To find the location of the above broker.conf, I am in the current conf directory.
Insert picture description here

You can see the specific IP this time, and send the message again.
Insert picture description here
Successfully sent, and was also consumed

Insert picture description here

Guess you like

Origin blog.csdn.net/huangdi1309/article/details/87924943