初识RocketMQ

目录

  • 一、简介
  • 二、准备
    • 2.1 下载包
    • 2.2 启动 Namesrv Broker 服务
    • 2.3 创建 Topic
  • 三、使用
    • 3.1 创建 SpringBoot 项目
    • 3.2 创建 Producer
    • 3.3 创建 Consumer
  • 四、验证
    • 4.1 验证 Producer,能否推送消息到 broker
    • 4.2 验证 Consumer,是否能消费消息
  • 五、更多demo参考

一、简介

RocketMQ是一个纯Java、分布式、队列模型的开源消息中间件

官网:http://rocketmq.apache.org/docs/quick-start/

二、准备

2.1 下载包

  • 下载 RocketMQ Binary 包,下载地址
    初识RocketMQ_第1张图片
    初识RocketMQ_第2张图片
  • 修改Namesrv和Broker的JVM启动参数(可选)
    初识RocketMQ_第3张图片
  • 配置环境变量 ROCKETMQ_HOME D:\Software\RocketMQ
    初识RocketMQ_第4张图片

2.2 启动 Namesrv Broker 服务

  • bin目录下打开CMD,右键打开CMD
  • 启动 Namesrv 服务:start mqnamesrv.cmd
    初识RocketMQ_第5张图片
  • 建议使用 jdk1.8 否则会报:OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.
  • 启动 Broker 服务并自动创建topic:start mqbroker.cmd -n localhost:9876 autoCreateTopicEnable=true
    初识RocketMQ_第6张图片

2.3 创建 Topic

mqadmin.cmd updateTopic -n localhost:9876 -b ${IPv4 地址}:10911 -t demo
初识RocketMQ_第7张图片

三、使用

3.1 创建 SpringBoot 项目

添加 rocketmq 依赖
pom.xml

<dependency>
   <groupId>org.apache.rocketmqgroupId>
   <artifactId>rocketmq-clientartifactId>
   <version>4.4.0version>
dependency>

3.2 创建 Producer

ProducerService.java

package com.example.demo.producer;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Service
public class ProducerService {
    private DefaultMQProducer producer = null;

    @PostConstruct
    public void initMQProducer() {
        producer = new DefaultMQProducer("defaultGroup");
        producer.setNamesrvAddr("localhost:9876");
        producer.setRetryTimesWhenSendFailed(3);

        try {
            producer.start();
        } catch (MQClientException e) {
            e.printStackTrace();
        }
    }

    public boolean send(String topic, String tags, String content) {
        Message msg = new Message(topic, tags, "", content.getBytes());
        try {
            producer.send(msg);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @PreDestroy
    public void shutDownProducer() {
        if (producer != null) {
            producer.shutdown();
        }
    }
}

3.3 创建 Consumer

ConsumerService.java

package com.example.demo.consumer;

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.MessageExt;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.List;

@Service
public class ConsumerService {
    private DefaultMQPushConsumer consumer = null;

    @PostConstruct
    public void initMQConsumer() {
        consumer = new DefaultMQPushConsumer("defaultGroup");
        consumer.setNamesrvAddr("localhost:9876");
        try {
            consumer.subscribe("testTopic", "*");
            consumer.registerMessageListener(new MessageListenerConcurrently() {

                @Override
                public ConsumeConcurrentlyStatus consumeMessage(
                        List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
                    for (MessageExt msg : msgs) {
                        System.out.println("Message Received: " + new String(msg.getBody()));

                    }
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                }
            });
            consumer.start();
        } catch (MQClientException e) {
            e.printStackTrace();
        }
    }

    @PreDestroy
    public void shutDownConsumer() {
        if (consumer != null) {
            consumer.shutdown();
        }
    }
}

四、验证

4.1 验证 Producer,能否推送消息到 broker

创建 ProducerService LLT

package com.example.demo.producer;

import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
class ProducerServiceTest {

    @Autowired
    private ProducerService producerService;

    @Test
    void send() {
        boolean res = producerService.send("testTopic", "tag1", "Hello World!");
        Assert.assertTrue(res);
    }
}

初识RocketMQ_第8张图片

4.2 验证 Consumer,是否能消费消息

  • 启动项目,consumer打印消息
    初识RocketMQ_第9张图片
  • 运行 LLT 产生一条消息,可以看到 Consumer 又消费一条消息
    初识RocketMQ_第10张图片

五、更多demo参考

http://rocketmq.apache.org/docs/simple-example/

你可能感兴趣的:(RocketMQ)