<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
#修改 application.properites文件
# 配置集群的VIP # 192.168.174.150:5672
spring.rabbitmq.addresses=192.168.174.150:5672
# 配置真实IP也可以
#spring.rabbitmq.addresses=192.168.174.140:5672,192.168.174.141:5672
# 配置账号
spring.rabbitmq.username=admin
spring.rabbitmq.password=123456
spring.rabbitmq.connection-timeout=15000
@Configuration
public class RabbitConfig {
private Logger log = LoggerFactory.getLogger(RabbitConfig.class);
@Bean
public RabbitTemplate rabbitTemplate(CachingConnectionFactory connectionFactory) {
connectionFactory.setPublisherConfirms(true);
connectionFactory.setPublisherReturns(true);
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
rabbitTemplate.setMandatory(true);
rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
log.info("消息回调id:" + correlationData);
if (ack) {
log.info("消息成功消费");
} else {
log.info("消息消费失败:" + cause);
}
}
rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey