RocketMQ: Kafka: RocketMQ: Kafka: RocketMQ: Kafka: RocketMQ: Kafka: RocketMQ: Kafka: RocketMQ: Kafka: RocketMQ: Kafka: RocketMQ: Kafka: RocketMQ: Kafka: RocketMQ: Kafka: RocketMQ: Kafka MQ application scenarios, which let us know when MQ can be used and what problems can be solved, are introduced later: RocketMQ cluster deployment configuration; This article picks up where we left off with a quick RocketMQ primer.

How to use

1. Introduce RocketMQ-client

<dependency> <groupId>org.apache.rocketmq</groupId> <artifactId>rocketmq-client</artifactId> < version > 4.1.0 - incubating < / version > < / dependency >Copy the code

2. Writing Producer

 DefaultMQProducer producer = new DefaultMQProducer("producer_demo");
       // Specify the NameServer address
        producer.setNamesrvAddr("192.168.116.115:9876; 192.168.116.116:9876"); // Change it to your own

        /** * The Producer object must be initialized with the start method before being used, which can only be initialized once. Note that the start method cannot be called every time a message is sent
        producer.start();

        for (int i = 0; i < 997892; i++) {
            try {
                // Build the message
                Message msg = new Message("TopicTest" /* Topic */."TagA" /* Tag */,
                    ("Test RocketMQ" + i).getBytes(RemotingHelper.DEFAULT_CHARSET)
                );

                // Send a synchronization message
                SendResult sendResult = producer.send(msg);

                System.out.printf("%s%n", sendResult);
            } catch (Exception e) {
                e.printStackTrace();
                Thread.sleep(1000);
            }
        }


producer.shutdown();
Copy the code

3. Write Consumer

/** ** Consumer Group, a very important concept, will be added later */
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("consumer_demo");
// Specify the NameServer address. separated
consumer.setNamesrvAddr("192.168.116.115:9876; 192.168.116.116:9876"); // Change it to your own

/** * Sets whether the first Consumer starts at the head of the queue or at the end of the queue */
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);

consumer.subscribe("TopicTest"."*");

consumer.registerMessageListener(new MessageListenerConcurrently() {

    @Override
    public ConsumeConcurrentlyStatus consumeMessage(List
       
         msgs, ConsumeConcurrentlyContext context)
        {
	try {
	    for(MessageExt msg:msgs){
		String msgbody = new String(msg.getBody(), "utf-8");
		System.out.println(" MessageBody: "+ msgbody);// Outputs the message content}}catch (Exception e) {
	    e.printStackTrace();
	    return ConsumeConcurrentlyStatus.RECONSUME_LATER; // Try again later
	}
	return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; // Successful consumption}}); consumer.start(); System.out.printf("Consumer Started.%n");
Copy the code

4, description,

Modify the value of NamesrvAddr according to your environment. For my cluster, please refer to RocketMQ Cluster Deployment Configuration. The RocketMQ console will display the multi-master, multi-slave and asynchronous replication cluster modes.

5. Use the RocketMQ console

The rocketmq-console-ng command is rocketmq-console-ng, and the MAVNE command is used to compile the JAR.

mvn clean package -Dmaven.test.skip=true
java -jar target/rocketmq-console-ng-1.0.0.jar
Copy the code

Rocketmq-console-ng-1.0.0. jar\ boot-INF \classes\application.properties According to your own NamesrvAddr modified rocketmq. Config. NamesrvAddr values.

Direct start:

java -jar rocketmq-console-ng-1.0.0.jar
Copy the code

6. Operation and observation

A good habit is to run Consumer first, then run Producer, and then watch from the RocketMQ-console-ng console

After running, it is true that the amount of data in broker-A plus the amount of data in Broker-B equals the amount of data we sent, and the number of slaves is the same as the number of masters, as follows:

Check the disk status of the two machines that sent these data as follows:Rocketmq1 occupies disk space

At this point, the RocketMQ Quickstart is over and to be continued…

If you find it rewarding, please click “like” and follow it.


Personal public account, welcome to pay attention to, consult more wonderful history!!