上一篇:SpringBoot怎样整合ActiveMQ? | 带你读《SpringBoot实战教程》之三十五
下一篇:Kafka概述及安装 | 带你读《SpringBoot实战教程》之三十七
本文来自于千锋教育在阿里云开发者社区学习中心上线课程《SpringBoot实战教程》,主讲人杨红艳,点击查看视频内容。
SpringBoot整合RabbitMQ
添加相应依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
全局配置:
spring.application.name=sbrabbitmq
spring.rabbitmq.addresses=192.168.25.129:5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin
spring.rabbitmq.publisher-confirms=true
新建消息发送接收包:com.qianfeng.mq
@Component
public class MySender {
@Autowired
private AmqpTemplate rabbitTemplate;
public void send() {
this.rabbitTemplate.convertAndSend("hel", "你好!!!");
return "发送成功";
}
}
@Component
public class MyReceiver {
@RabbitHandler
@RabbitListener(queues="hel")
public void receive(String text) {
System.out.println("收到消息:"+text);
}
}
新建com.qianfeng.controller:
@Controller
public class TestController {
@Autowired
private MySender mySender;
@RequestMapping("/send")
@ResponseBody
public String test() {
mySender.send();
return "ok";
}
}
在启动类中添加所有需要扫描的包:
@SpringBootApplication(scanBasePackages="com.qianfeng")
执行结果: