1、配置文件(application.properties)
# rocketmq
rocketmq.nameServer=localhost:9876
rocketmq.producer.group=newProducer
rocketmq.producer.topic=newTopic
# mongodb
spring.data.mongodb.uri=mongodb://localhost:27017/admin
2、修改pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
</dependency>
3、controller(生产者)
@RestController
public class ProducerController {
@Autowired
private RocketMQTemplate rocketMQTemplate;
@RequestMapping("message/send")
public void send(@RequestBody String msg) {
try {
this.rocketMQTemplate.convertAndSend("demo", msg);//使用rocketMQ模板发送信息
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
4、consumer(消费者)
@Component
@RocketMQMessageListener(topic = "demo", consumerGroup = "group1")
public class RocketMQCustomerEight implements RocketMQListener<String> {
@Autowired
private RocketMQTemplate rocketMQTemplate;
@Override
public void onMessage(String message) {
try{
do(message);//业务逻辑处理
}catch (Exception e){
logger.error("消费失败"+message);
}
}
}