一:Ubuntu安装RocketMQ
Ubuntu 安装 Rocketmq_羁客%的博客-CSDN博客
二:添加RocketMQ依赖
<!--rocket mq 依赖-->
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>2.1.1</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</exclusion>
</exclusions>
</dependency>
三:在application中添加RocketMQ配置
#rocketmq 配置
rocketmq:
name-server: 127.0.0.0:9876 #IP根据自己电脑服务器配置的 mqbroker->brokerIP1 修改一下,最好不要使用这个
producer:
group: rocketmq_group
myrocketmq-config:
my-topic: rocketmq_topic
my-consumer-group: rocketmq_group_consumer
四:编写消费者,消息生产者,消息实体类(自定义)
//Rocket消费者(Push 推送型消费者,被动型消费)
@Slf4j
@Component
@RocketMQMessageListener(consumerGroup = "${myrocketmq-config.my-consumer-group}", topic = "${myrocketmq-config.my-topic}")
public class RocketConsumer implements RocketMQListener<RocketmqVo> {
@Override
public void onMessage(RocketmqVo rocketmqVo) {
log.info("收到RocketMQ消息:{}",rocketmqVo);
}
}
//Rocket生产者
@Slf4j
@Component
public class RocketProduce {
@Value("${myrocketmq-config.my-topic}")
private String mqTopic;
@Value("${myrocketmq-config.my-consumer-group}")
private String mqConsumerGroup;
@Autowired
private RocketMQTemplate mqTemplate;
/**
* 同步发送
*
* @param title 发送消息
* @param data 消息内容
*/
public void sync(String title, Object data) {
String time = new SimpleDateFormat("yyyyMMdd HH:mm:ss").format(new Date());
RocketmqVo rocketmqVo = new RocketmqVo(mqConsumerGroup, mqTopic, title, time, data);
SendResult sendResult = mqTemplate.syncSend(mqTopic, rocketmqVo);
log.info("同步发送:{}", rocketmqVo);
log.info("同步发送消息结果:{}", sendResult);
}
/**
* 异步发送
*
* @param title 发送消息
* @param data 消息内容
*/
public void async(String title, Object data) {
String time = new SimpleDateFormat("yyyyMMdd HH:mm:ss").format(new Date());
RocketmqVo rocketmqVo = new RocketmqVo(mqConsumerGroup, mqTopic, title, time, data);
log.info("异步发送:{}", rocketmqVo);
mqTemplate.asyncSend(mqTopic, rocketmqVo, new SendCallback() {
@Override
public void onSuccess(SendResult var1) {
log.info("异步发送成功:{}", var1);
}
@Override
public void onException(Throwable var1) {
log.info("异步发送失败:{}", var1.getMessage());
}
});
}
/**
* 单向发送
*
* @param title 发送消息
*/
public void oneway(String title, Object data) {
String time = new SimpleDateFormat("yyyyMMdd HH:mm:ss").format(new Date());
RocketmqVo rocketmqVo = new RocketmqVo(mqConsumerGroup, mqTopic, title, time, data);
mqTemplate.sendOneWay(mqTopic, rocketmqVo);
log.info("单向发送:{}", rocketmqVo);
}
}
//Rocket消息实体
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class RocketmqVo {
@Value("${myrocketmq-config.my-consumer-group}")
private String group;
@Value("${myrocketmq-config.my-topic}")
private String topic;
private String title;
private String date;
private Object data;
public RocketmqVo(String title, String date, Object data) {
this.title = title;
this.date = date;
this.data = data;
}
}
五:测试Controller
@RestController
@RequestMapping("/mqtest")
public class RocketController {
@Resource
private RocketProduce producer;
@RequestMapping("/sendMessageSync/{msg}")
public String sendMessageSync(@PathVariable("msg") String message) {
producer.sync(message, null);
return "消息发送完成";
}
@RequestMapping("/sendMessageAsync/{msg}")
public String sendMessageAsync(@PathVariable("msg") String message) {
producer.async(message, null);
return "消息发送完成";
}
@RequestMapping("/sendMessageOneway/{msg}")
public String sendMessageOneway(@PathVariable("msg") String message) {
producer.oneway(message, null);
return "消息发送完成";
}
}