application配置
#fubao
spring.rabbitmq.fubao.host=192.168.x.xx
spring.rabbitmq.fubao.port=5672
spring.rabbitmq.fubao.username=xxx
spring.rabbitmq.fubao.password=xxx
spring.rabbitmq.fubao.virtual-host=xx
#zcnews
spring.rabbitmq.zcnews.host=192.168.x.xxx
spring.rabbitmq.zcnews.port=5672
spring.rabbitmq.zcnews.username=xxx
spring.rabbitmq.zcnews.password=xxxx
spring.rabbitmq.zcnews.virtual-host=xx
配置Rabbitmq config
@Configuration
@EnableRabbit
public class RabbitConfig {
@Value("${spring.rabbitmq.fubao.host}")
private String host1Addresses;
@Value("${spring.rabbitmq.fubao.username}")
private String host1Username;
@Value("${spring.rabbitmq.fubao.password}")
private String host1Password;
@Value("${spring.rabbitmq.fubao.virtual-host}")
private String host1VirtualHost;
@Value("${spring.rabbitmq.zcnews.host}")
private String host2Addresses;
@Value("${spring.rabbitmq.zcnews.username}")
private String host2Username;
@Value("${spring.rabbitmq.zcnews.password}")
private String host2Password;
@Value("${spring.rabbitmq.zcnews.virtual-host}")
private String host2VirtualHost;
@Bean
@Primary
public CachingConnectionFactory fubaoConnectionFactory() {
CachingConnectionFactory factory = new CachingConnectionFactory(host1Addresses, 5672);
factory.setUsername(host1Username);
factory.setPassword(host1Password);
factory.setVirtualHost(host1VirtualHost);
return factory;
}
@Bean(name = "fubaoRabbitListenerContainerFactory")
public RabbitListenerContainerFactory<?> fubaoRabbitListenerContainerFactory( @Qualifier("fubaoConnectionFactory")CachingConnectionFactory fubaoConnectionFactory) {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(fubaoConnectionFactory);
return factory;
}
@Bean(name = "fubaoRabbitTemplate")
public RabbitTemplate fubaoRabbitTemplate() {
return new RabbitTemplate(fubaoConnectionFactory());
}
@Bean
public CachingConnectionFactory zcnewsConnectionFactory() {
CachingConnectionFactory factory = new CachingConnectionFactory(host2Addresses, 5672);
factory.setUsername(host2Username);
factory.setPassword(host2Password);
factory.setVirtualHost(host2VirtualHost);
return factory;
}
@Bean(name = "zcnewsRabbitTemplate")
public RabbitTemplate zcnewsRabbitTemplate() {
return new RabbitTemplate(zcnewsConnectionFactory());
}
@Bean(name = "zcnewsRabbitListenerContainerFactory")
public RabbitListenerContainerFactory<?> zcnewsRabbitListenerContainerFactory(@Qualifier("zcnewsConnectionFactory") CachingConnectionFactory zcnewsConnectionFactory) {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(zcnewsConnectionFactory);
return factory;
}
}
发送消息
@Slf4j
@Component
public class RabbitMQUtil {
@Autowired
private RabbitTemplate fubaoRabbitTemplate;
public void sendMessage(String exchange, String routingKey, Object message) {
try {
fubaoRabbitTemplate.convertAndSend(exchange, routingKey, message);
log.info("消息发送成功: exchange={}, routingKey={}, message={}", exchange, routingKey, message);
} catch (Exception e) {
log.error("消息发送失败: exchange={}, routingKey={}, message={}", exchange, routingKey, message, e);
throw new RuntimeException("消息发送失败", e);
}
}
public void sendMessageWithConfirm(String exchange, String routingKey, Object message) {
fubaoRabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
if (ack) {
log.info("消息发送确认成功: exchange={}, routingKey={}, message={}",
exchange, routingKey, message);
} else {
log.error("消息发送确认失败: exchange={}, routingKey={}, message={}, cause={}",
exchange, routingKey, message, cause);
}
});
fubaoRabbitTemplate.setReturnCallback((msg, replyCode, replyText, tmpExchange, tmpRoutingKey) -> {
log.error("消息发送失败: exchange={}, routingKey={}, replyCode={}, replyText={}",
tmpExchange, tmpRoutingKey, replyCode, replyText);
});
sendMessage(exchange, routingKey, message);
}
}
消费消息
@Slf4j
@Component
public class PullNewsRabbitMQListener implements ChannelAwareMessageListener {
@Resource
private NewsService newsService;
private int retryCount = 0;
@RabbitListener(queues = "q_fubao_news", containerFactory = "fubaoRabbitListenerContainerFactory",ackMode = "MANUAL")
@Override
public void onMessage(Message message, Channel channel) throws Exception {
long deliveryTag = message.getMessageProperties().getDeliveryTag();
log.info("接收到mq信息,处理的articleId为:" + new String(message.getBody()));
try{
String msg = new String(message.getBody());
Integer articleId = Integer.parseInt(msg);
newsService.pullNews(articleId);
channel.basicAck(deliveryTag,false);
log.info("消费mq消息成功,articleId为:{}",articleId);
}catch (Exception e){
retryCount++;
log.error(String.format("articleId:%s,%d次重试失败,异常信息为:%s",new String(message.getBody()),retryCount,e.getMessage()));
throw e;
}
finally {
if (retryCount == 3) {
log.error(String.format("新闻后台数据入库失败,articleId为:%s",new String(message.getBody())));
channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
}
}
}
}