SpringBoot 集成RabbitMQ 其实非常简单,具体步骤如下:
1. pom.xml 引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2 编写配置文件 (我这里只写一种默认的方式direct 其余的方式类同)
## application.yml 配置
spring:
rabbitmq:
host: 154.8.141.51
port: 5672
username: root
password: root
### 配置队列 Queue
@Configuration
public class RabbitConfig {
@Bean
public Queue Queue() {
return new Queue("demo");
}
}
3 编写消息提供方
@Autowired
private AmqpTemplate rabbitTemplate;
/**
* 发送消息
* @author zhangh
* @date 2018年11月6日下午4:15:22
* @param message
* @throws IOException
* @throws TimeoutException
*/
@ApiOperation(value = "MQ 发送接口", notes = "发送消息")
@RequestMapping("/sendMQ")
@ResponseBody
public void sendMessage(String message) throws IOException, TimeoutException {
rabbitTemplate.convertAndSend("demo","hello " + new Date()+ message);
}
4 编写消息消费方
@Component
@RabbitListener(queues = "demo")
public class ReceiveMQ {
@RabbitHandler
public void receive(String str) {
System.out.println("Receiver : " + str);
}
}
5 .启动项目
先执行消息提供方 然后再控制台可以看到有消息输出
以上就是SpringBoot 集成RabbitMQ 的完整步骤,如有疑问请留言