windows安装rocketMq以及springboot调试使用

rocketMq下载地址:https://rocketmq.apache.org/download/,Binary 下载!
windows安装rocketMq以及springboot调试使用_第1张图片
配置环境变量:
NAMESRV_ADDR:localhost:9876
ROCKETMQ_HOME:E:\rocketmq
windows安装rocketMq以及springboot调试使用_第2张图片
点击运行这两个cmd文件
windows安装rocketMq以及springboot调试使用_第3张图片
windows安装rocketMq以及springboot调试使用_第4张图片
springbot部署rocketMq:

pom.xml添加
		<dependency>
            <groupId>org.apache.rocketmq</groupId>
            <artifactId>rocketmq-common</artifactId>
            <version>4.5.1</version>
        </dependency>
配置文件添加
#RocketMQ配置
apache:
  rocketmq:
    #消费者的配置
    consumer:
      pushConsumer: pushConsumer
    #生产者的配置
    producer:
      producerGroup: wangtianyou
    #Nameserver的地址,这里配置你MQ安装的机器上的IP就好,我这里在本机安装的
    namesrvAddr: 127.0.0.1:9876
编写生产者:Producer.java
package com.wty.common.rocketmq;

import cn.hutool.core.date.StopWatch;
import org.apache.rocketmq.client.exception.MQBrokerException;
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;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.UnsupportedEncodingException;

/**
 * Author 王天佑
 * email: [email protected]
 * Date: 2022/10/21
 * Time: 17:14
 */


@Component
public class Producer {

    /**
     * 生产者的组名
     */
    @Value("${apache.rocketmq.producer.producerGroup}")
    private String producerGroup;

    private DefaultMQProducer producer;
    /**
     * NameServer 地址
     */
    @Value("${apache.rocketmq.namesrvAddr}")
    private String namesrvAddr;

    @PostConstruct
    public void defaultMQProducer() {

        //生产者的组名
        producer= new DefaultMQProducer(producerGroup);
        //指定NameServer地址,多个地址以 ; 隔开
        producer.setNamesrvAddr(namesrvAddr);
        producer.setVipChannelEnabled(false);
        try {
            producer.start();
            System.out.println("-------->:producer启动了");
        } catch (MQClientException e) {
            e.printStackTrace();
        }
    }

    public String send(String topic,String tags,String body) throws InterruptedException, RemotingException, MQClientException, MQBrokerException, UnsupportedEncodingException {
        Message message = new Message(topic, tags, body.getBytes(RemotingHelper.DEFAULT_CHARSET));
        StopWatch stop = new StopWatch();
        stop.start();
        SendResult result = producer.send(message);
        System.out.println("发送响应:MsgId:" + result.getMsgId() + ",发送状态:" + result.getSendStatus());
        stop.stop();
        return "{\"MsgId\":\""+result.getMsgId()+"\"}";
    }


}

编写消费者:Consumer.java
package com.wty.common.rocketmq;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.Message;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;

import org.springframework.stereotype.Component;

/**
 * Author 王天佑
 * email: [email protected]
 * Date: 2022/10/21
 * Time: 17:17
 */

@Component
public class Consumer implements CommandLineRunner {

    /**
     * 消费者
     */
    @Value("${apache.rocketmq.consumer.pushConsumer}")
    private String pushConsumer;

    /**
     * NameServer 地址
     */
    @Value("${apache.rocketmq.namesrvAddr}")
    private String namesrvAddr;


    /**
     * 初始化RocketMq的监听信息,渠道信息
     */
    public void messageListener(){

        DefaultMQPushConsumer consumer=new DefaultMQPushConsumer("SpringBootRocketMqGroup");

        consumer.setNamesrvAddr(namesrvAddr);
        try {

            // 订阅PushTopic下Tag为push的消息,都订阅消息
            consumer.subscribe("wangtianyou", "push");
            // 程序第一次启动从消息队列头获取数据
            consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
            //可以修改每次消费消息的数量,默认设置是每次消费一条
            consumer.setConsumeMessageBatchMaxSize(1);

            //在此监听中消费信息,并返回消费的状态信息
            consumer.registerMessageListener((MessageListenerConcurrently) (msgs, context) -> {

                // 会把不同的消息分别放置到不同的队列中
                for(Message msg:msgs){

                    System.out.println("接收到了消息:"+new String(msg.getBody()));
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            });

            consumer.start();

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

    @Override
    public void run(String... args) throws Exception {
        this.messageListener();
    }


}

测试
@RequestMapping(value = "/rocketMqTest",method = RequestMethod.GET)
    public String rocketMqTest(){
        try {
            return producer.send("wangtianyou","push","哈哈哈哈哈哈");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (RemotingException e) {
            e.printStackTrace();
        } catch (MQClientException e) {
            e.printStackTrace();
        } catch (MQBrokerException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "ERROR";
    }

注释:请求有返回:{“MsgId”:“C0A942AC3CF8610644256F0F5A8C005C”}表示成功啦!

windows安装rocketMq以及springboot调试使用_第5张图片
注释:如果刚开始正常运行,过一小会报内存错误,可以尝试这种方法
添加:set “JAVA_OPT=%JAVA_OPT% -Drocketmq.broker.diskSpaceWarningLevelRatio=0.98”
windows安装rocketMq以及springboot调试使用_第6张图片

你可能感兴趣的:(springboot,java,spring,boot,java-rocketmq,windows)