本次我们集成RabbitMQ
1、引入依赖
<!--mq-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2、在配置文件中加入
# mq
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=root
spring.rabbitmq.password=123456
spring.rabbitmq.virtual-host=beck
3、消息的生产者
@Autowired
private AmqpTemplate amqpTemplate;
/**
* 测试ma
* @return
* @throws Exception
*/
@RequestMapping("mq")
public String mqTest() throws Exception {
amqpTemplate.convertAndSend("sendSMS", "manager", "StreamYearMq内容");
return "ok";
}
4、消息的消费者
package com.streamyear.course.consumer;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "eventQueue")
public class EventQueue {
@RabbitHandler
public void process(String manager){
System.out.println("eventQueue收到的消息内容: " + manager);
}
}
5、测试(http://localhost:9090/mq)
备注: