首先,在虚拟机上安装rocketmq和rocketMq可视化控制,安装不做描述。
1、pom.xml文件添加依赖
mq的版本与连接的rocketmq版本保持一致
org.apache.rocketmq
rocketmq-remoting
4.4.0
2、yml文件添加rocketmq配置
apache:
rocketmq:
#消费者的配置
consumer:
pushConsumer: myConsumer
#生产者的配置
producer:
producerGroup: myGroup
namesrvAddr: 192.168.233.128:9876
3、生产者类RocketProducer
package com.zp.springbootdemo.rocketmq;
import com.alibaba.fastjson.JSONObject;
import com.sun.org.apache.xpath.internal.objects.XString;
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 org.springframework.util.StopWatch;
import javax.annotation.PostConstruct;
import java.io.UnsupportedEncodingException;
/**
* @Author zp
* @Description rocketmq生产者
* @Date 22:06 2020/5/22
* @Param
* @return
**/
@Component