https://www.cnblogs.com/bigdataZJ/p/springboot-rabbitmq.html
RabbitMQ是一种我们经常使用的消息中间件,通过RabbitMQ可以帮助我们实现异步、削峰的目的。
今天这篇,我们来看看Spring Boot是如何集成RabbitMQ,发送消息和消费消息的。同时我们介绍下死信队列。
集成RabbitMQ
集成RabbitMQ只需要如下几步即可
1、添加maven依赖
<!--rabbitmq-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2、添加配置文件application.yaml
在application.yaml添加配置内容如下:
spring:
# profiles: test
rabbitmq:
host: 192.168.3.161
port: 5672
username: guest
password: guest
cache:
channel:
size: 10
listener:
type: simple
simple:
acknowledge-mode: auto
concurrency: 5
default-requeue-rejected: true
max-concurrency: 100
retry:
enabled: true
# initial-interval: 1000ms
max-attempts: 3
# max-interval: 1000ms
multiplier: 1
stateless: true
# publisher-confirms: true
注意:
这里最基本的配置只需要配置host
,port
,username
和password
四个属性即可。
其他属性都有各自的含义,比如retry
是用于配置重试策略的,acknowledge-mode
是配置消息接收确认机制的。