SpringCloud 集成 RocketMQ 示例及配置详解
warning:
这篇文章距离上次修改已过433天,其中的内容可能已经有所变动。
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProducerController {
@Autowired
private RocketMQTemplate rocketMQTemplate;
@GetMapping("/sendMessage")
public String sendMessage(@RequestParam String message) {
rocketMQTemplate.convertAndSend("topicTest:tagTest", message);
return "消息发送成功";
}
}这段代码展示了如何在Spring Cloud项目中使用RocketMQTemplate发送消息。通过@Autowired注入RocketMQTemplate后,可以调用convertAndSend方法发送消息到指定的主题和标签。这里的topicTest:tagTest表示消息将发送到名为topicTest的主题,并带有标签tagTest。消息内容通过HTTP请求的message参数传递。
评论已关闭