如果对大家有用,请点赞收藏支持,如果无用请多多包含!!!!
非常感谢!!!
先上代码配置文件:
spring:
rabbitmq:
yjdpservice:
host: 192.168.1.130
port: 5672
username: admin123
password: admin123
virtualhost: /exceptionless
yjservice:
host: 192.168.1.131
port: 5672
username: admin123
password: admin123
virtualhost: /exceptionless
属性类:
/**
* MQ配置读类
*
*/
@Data
public abstract class AbstractRabbitConfiguration {
protected String host;
protected int port;
protected String username;
protected String password;
protected String virtualhost;
protected ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setHost(host);
connectionFactory.setPort(port);
connectionFactory.setUsername(username);
connectionFactory.setPassword(password);
connectionFactory.setVirtualHost(virtualhost);
return connectionFactory;
}
}
配置类,我拆分了两个,大家可以使用一个类写即可:
package com.itdu.common.config;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
@Configuration
@ConfigurationProperties("spring.rabbitmq.yjdpservice")
public class YjdpRabbitConfiguration extends AbstractRabbitConfiguration {
@Bean(name = "yjdpServiceConnectionFactory")
@Primary
public ConnectionFactory contractConnectionFactory() {
return super.connectionFactory();
}
@Bean(name = "yjdpServiceRabbitTemplate")
@Primary
public RabbitTemplate contractRabbitTemplate(@Qualifier("yjdpServiceConnectionFactory") ConnectionFactory connectionFactory) {
return new RabbitTemplate(connectionFactory);
}
@Bean(name = "yjdpServiceFactory")
public SimpleRabbitListenerContainerFactory contractFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer,
@Qualifier("yjdpServiceConnectionFactory") ConnectionFactory connectionFactory) {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
configurer.configure(factory, connectionFactory);
return factory;
}
@Bean(value = "yjdpServiceRabbitAdmin")
public RabbitAdmin contractRabbitAdmin(@Qualifier("yjdpServiceConnectionFactory") ConnectionFactory connectionFactory) {
return new RabbitAdmin(connectionFactory);
}
}
模板类:
/**
* 多个MQ服务费配置
* @author ty-intasect04
*
*/
//@Component
public abstract class MultiRabbitTemplate {
@Autowired
@Qualifier(value = "yjdpServiceRabbitTemplate")
public AmqpTemplate yjdpServiceRabbitTemplate;
@Autowired
@Qualifier(value = "yjServiceRabbitTemplate")
public AmqpTemplate yjServiceRabbitTemplate;
}
消息监听类:
@RabbitListener(queues = "yj_actived",containerFactory = "yjFactory")
public void reciveContractActive(Message message) throws IOException {
try {
String result = new String(message.getBody());
logger.info("消息:{}", new String(message.getBody()))
} catch (Exception e) {
logger.error("失败消息:{}", new String(message.getBody()), e);
}
}
请注意 @RabbitListener(queues = "yj_actived",containerFactory = "yjFactory")
这个注解是监听yjFactory对应的队列yj_actived的。
ok了,在项目中使用过,目前来说还是稳定的,没有出过其他问题。
也可以如下写法:
@Configuration
public class RabbitConfig {
@Bean(name="yjdpConnectionFactory")
@Primary
public ConnectionFactory YjdpConnectionFactory(
@Value("${spring.rabbitmq.yjdpservice.host}") String host,
@Value("${spring.rabbitmq.yjdpservice.port}") int port,
@Value("${spring.rabbitmq.yjdpservice.username}") String username,
@Value("${spring.rabbitmq.yjdpservice.password}") String password,
@Value("${spring.rabbitmq.yjdpservice.virtualhost}") String virtualhost
){
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setHost(host);
connectionFactory.setPort(port);
connectionFactory.setUsername(username);
connectionFactory.setPassword(password);
connectionFactory.setVirtualHost(virtualhost);
return connectionFactory;
}
@Bean(name="yjConnectionFactory")
public ConnectionFactory yjConnectionFactory(
@Value("${spring.rabbitmq.yjservice.host}") String host,
@Value("${spring.rabbitmq.yjservice.port}") int port,
@Value("${spring.rabbitmq.yjservice.username}") String username,
@Value("${spring.rabbitmq.yjservice.password}") String password,
@Value("${spring.rabbitmq.yjservice.virtualhost}") String virtualhost
){
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setHost(host);
connectionFactory.setPort(port);
connectionFactory.setUsername(username);
connectionFactory.setPassword(password);
connectionFactory.setVirtualHost(virtualhost);
return connectionFactory;
}
@Bean(name="yjdpRabbitTemplate")
@Primary
public RabbitTemplate yjdpRabbitTemplate(
@Qualifier("yjdpConnectionFactory") ConnectionFactory connectionFactory
){
RabbitTemplate yjdpRabbitTemplate = new RabbitTemplate(connectionFactory);
return yjdpRabbitTemplate;
}
@Bean(name="yjRabbitTemplate")
public RabbitTemplate yjRabbitTemplate(
@Qualifier("yjConnectionFactory") ConnectionFactory connectionFactory
){
RabbitTemplate yjRabbitTemplate = new RabbitTemplate(connectionFactory);
return yjRabbitTemplate;
}
@Bean(name="yjdpFactory")
public SimpleRabbitListenerContainerFactory yjdpFactory(
SimpleRabbitListenerContainerFactoryConfigurer configurer,
@Qualifier("yjdpConnectionFactory") ConnectionFactory connectionFactory
) {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
configurer.configure(factory, connectionFactory);
return factory;
}
@Bean(name="yjFactory")
public SimpleRabbitListenerContainerFactory yjFactory(
SimpleRabbitListenerContainerFactoryConfigurer configurer,
@Qualifier("yjConnectionFactory") ConnectionFactory connectionFactory
) {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
configurer.configure(factory, connectionFactory);
return factory;
}
}
使用方法如上
@RabbitListener(queues = "yjdp_actived",containerFactory = "yjdpFactory")
@RabbitListener(queues = "yjdp_actived",containerFactory = "yjFactory")
分别使用两个Factory