springboot+rocketmq+mongodb 小咪咪 2022-12-27 06:24 53阅读 0赞 application.properties # rocketmq rocketmq.nameServer=localhost:9876 rocketmq.producer.group=newProducer rocketmq.producer.topic=newTopic # mongodb spring.data.mongodb.uri=mongodb://localhost:27017/admin 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> 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(); } } } 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); } } }
还没有评论,来说两句吧...