SpringBoot中使用RabbitMQ
搭建初始环境
1. 引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2. 配置配置文件
spring:
application:
name: springboot_rabbitmq
rabbitmq:
host: xxxx
port: 5672
username: ems
password: 123
virtual-host: /ems
使用RabbitTemplate
用来简化操作 使用时候直接在项目中注入即可使用
第一种hello world模型使用
1.开发生产者
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void testHello(){
rabbitTemplate.convertAndSend("hello","hello world");
}
2.开发消费者
@Component
@RabbitListener(queuesToDeclare = @Queue("hello"))
public class HelloCustomer