第一步:pom+ 配置
<!--RabbitMQ-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
//在spring下添加RabbitMQ的配置
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
第二步,写个消息的发送者
@Component
public class MqSenderTest{
@Autowired
private AmqpTemplate amqpTemplate;
@Test
public void send(){
// 消息发送到myQueue队列
amqpTemplate.convertAndSend("myQueue","now"+ new Date());
}
}
第三步,写个消息的接收者
@Slf4j
@Component
public class MqReceiver {
// 1 @RabbitListener(queues = "myQueue") 还需要去手动创建队列
//2 @RabbitListener(queuesToDeclare = @Queue("myQueue"))//自动创建队列
@RabbitListener(bindings = @QueueBinding(
value= @Queue("myQueue"),
exchange = @Exchange("myExchange")
))//自动创建队列并且和Exchange绑定
public void process(String message){
log.info("MqReceiver:{}",message);
}
}
到此,RabbitMQ的简单使用完成。