Maven
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
yml配置
spring:
rabbitmq:
host: aaa
port: aaa
username: aaa
password: aaa
ssl:
enabled: true
algorithm: ttt
second:
host: xxx
port: xxx
username: xxx
password: xxx
下面是主要数据源,使用了SSl
package com.xxx.xxx.xxx.xxx.xxx;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.RabbitConnectionFactoryBean;
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.RabbitProperties;
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;
import java.util.Objects;
@Configuration
public class firstRabbitConfig {
/**
* 创建链接工厂
*
* @return 连接工厂对象
*/
@Bean(name = "firstConnectionFactory")
@Primary
public ConnectionFactory firstConnectionFactory(RabbitProperties rabbitProperties) throws Exception {
//RabbitProperties 通过这个获取配置
RabbitConnectionFactoryBean factory = new RabbitConnectionFactoryBean();
factory.setHost(rabbitProperties.determineHost());
factory.setPort(rabbitProperties.getPort());
factory.setUsername(rabbitProperties.getUsername());
factory.setPassword(rabbitProperties.getPassword());
factory.setUseSSL(rabbitProperties.getSsl().isEnabled());
factory.setSslAlgorithm(rabbitProperties.getSsl().getAlgorithm());
RabbitProperties.Ssl ssl = rabbitProperties.getSsl();
if (ssl.isEnabled()) {
factory.setUseSSL(true);
if (ssl.getAlgorithm() != null) {
factory.setSslAlgorithm(ssl.getAlgorithm());
}
factory.setKeyStore(ssl.getKeyStore());
factory.setKeyStorePassphrase(ssl.getKeyStorePassword());
factory.setTrustStore(ssl.getTrustStore());
factory.setTrustStorePassphrase(ssl.getTrustStorePassword());
}
if (rabbitProperties.getConnectionTimeout() != null) {
factory.setConnectionTimeout(10000);
}
factory.afterPropertiesSet();
CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(factory.getObject());
cachingConnectionFactory.setAddresses(rabbitProperties.getAddresses());
cachingConnectionFactory.setPublisherConfirms(rabbitProperties.isPublisherConfirms());
cachingConnectionFactory.setPublisherReturns(rabbitProperties.isPublisherReturns());
if (rabbitProperties.getCache().getChannel().getSize() != null) {
cachingConnectionFactory.setChannelCacheSize(rabbitProperties.getCache().getChannel().getSize());
}
if (rabbitProperties.getCache().getConnection().getMode() != null) {
cachingConnectionFactory.setCacheMode(rabbitProperties.getCache().getConnection().getMode());
}
if (rabbitProperties.getCache().getConnection().getSize() != null) {
cachingConnectionFactory.setConnectionCacheSize(rabbitProperties.getCache().getConnection().getSize());
}
if (rabbitProperties.getCache().getChannel().getCheckoutTimeout() != null) {
cachingConnectionFactory.setChannelCheckoutTimeout(10000);
}
return cachingConnectionFactory;
}
@Bean(name = "firstRabbitTemplate")
@Primary
public RabbitTemplate firstRabbitTemplate(@Qualifier("firstConnectionFactory") ConnectionFactory connectionFactory) {
return new RabbitTemplate(connectionFactory);
}
/**
* 创建监听容器工厂
*
* @param configurer
* @param connectionFactory
* @return
*/
@Bean(name = "firstFactory")
public SimpleRabbitListenerContainerFactory firstFactory(
SimpleRabbitListenerContainerFactoryConfigurer configurer,
@Qualifier("firstConnectionFactory") ConnectionFactory connectionFactory
) {
//监听容器
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
//设置手动确认,默认自动确认
factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
//将连接工厂放入监听容器中,监听对应连接工厂的队列
configurer.configure(factory, connectionFactory);
return factory;
}
@Bean(value = "firstRabbitAdmin")
public RabbitAdmin firstRabbitAdmin(@Qualifier("firstConnectionFactory") ConnectionFactory connectionFactory) {
return new RabbitAdmin(connectionFactory);
}
}
RabbitProperties
//通过注解获取yml中的属性,为了偷懒,用了自带的实体类
@ConfigurationProperties(
prefix = "spring.rabbitmq"
)
public class RabbitProperties {
private String host = "localhost";
private int port = 5672;
private String username = "guest";
private String password = "guest";
private final RabbitProperties.Ssl ssl = new RabbitProperties.Ssl();
private String virtualHost;
private String addresses;
@DurationUnit(ChronoUnit.SECONDS)
private Duration requestedHeartbeat;
private boolean publisherConfirms;
private boolean publisherReturns;
private Duration connectionTimeout;
private final RabbitProperties.Cache cache = new RabbitProperties.Cache();
private final RabbitProperties.Listener listener = new RabbitProperties.Listener();
private final RabbitProperties.Template template = new RabbitProperties.Template();
private List<RabbitProperties.Address> parsedAddresses;
...
}
第二数据源,没有使用SSL
package com.xxx.xxx.xxx.xxx.xxx;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
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;
@Configuration
//通过注解获取yml中属性
@ConfigurationProperties("spring.rabbitmq.second")
public class secondRabbitConfig {
protected String host;
protected int port;
protected String username;
protected String password;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
/**
* 创建链接工厂
*
* @return 连接工厂对象
*/
@Bean(name = "secondConnectionFactory")
public ConnectionFactory secondConnectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setHost(host);
connectionFactory.setPort(port);
connectionFactory.setUsername(username);
connectionFactory.setPassword(password);
connectionFactory.setPublisherConfirms(true);
connectionFactory.setPublisherReturns(true);
return connectionFactory;
}
@Bean(name = "secondRabbitTemplate")
public RabbitTemplate secondRabbitTemplate(@Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory) {
return new RabbitTemplate(connectionFactory);
}
/**
* 创建监听容器工厂
*
* @param configurer
* @param connectionFactory
* @return
*/
@Bean(name = "secondFactory")
public SimpleRabbitListenerContainerFactory secondFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer,
@Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory) {
//监听容器
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
//设置手动确认,默认自动确认
// factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
//将连接工厂放入监听容器中,监听对应连接工厂的队列
configurer.configure(factory, connectionFactory);
return factory;
}
@Bean(value = "secondRabbitAdmin")
public RabbitAdmin secondRabbitAdmin(@Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory) {
return new RabbitAdmin(connectionFactory);
}
}
使用template
firstRabbitTemplate.convertAndSend(a, b, c);
secondRabbitTemplate.convertAndSend(a, b, c);
@Autowired
@Qualifier(value = "firstRabbitTemplate")
protected RabbitTemplate firstRabbitTemplate;
@Autowired
@Qualifier(value = "secondRabbitTemplate")
protected AmqpTemplate secondRabbitTemplate;