1.添加pom文件并添加配置文件
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>2.2.1</version>
</dependency>
rocketmq:
# NameServer
name-server: xxx:9876
producer:
# 发送消息超时时间,默认3000
send-message-timeout: 30000
# 生产者组
group: groupTest
# 发送消息失败重试次数,默认2
retryTimesWhenSendFailed: 2
# 异步消息重试此处,默认2
retryTimesWhenSendAsyncFailed: 2
2.发送消息(方式一)
package com.zy.sdt.convert.login.controller;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class MyProducer {
@Autowired
private RocketMQTemplate rocketMQTemplate;
public void sendMessage(String topic,String message){
rocketMQTemplate.convertAndSend(topic,message);
}
}
package com.zy.sdt.convert.login.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/myProducer")
@RestController
public class MyProducerController {
@Autowired
private MyProducer myProducer;
@GetMapping("/cc")
public void cc() {
myProducer.sendMessage("my-topic","hello world");
System.out.println("消息发送成功!");
}
}
3.发送消息(方式二)
package com.zy.sdt.convert.login.controller;
import org.apache.rocketmq.spring.annotation.ExtRocketMQTemplateConfiguration;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
@ExtRocketMQTemplateConfiguration(nameServer = "${rocketmq.name-server}")
public class ExtRocketMQTemplate extends RocketMQTemplate {
}
package com.zy.sdt.convert.login.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/myProducer")
@RestController
public class MyProducerController {
@Autowired
private ExtRocketMQTemplate extRocketMQTemplate;
@GetMapping("/yy")
public void yy() {
extRocketMQTemplate.sendOneWay("my-topic","hello world2");
System.out.println("消息发送成功!");
}
}
4.检查消息是否正常送达
5.