我想在我当前的项目中使用RabbitMQ和Spring Boot实现多租户应用程序。目前,该应用程序以单租户的方式运行。方法是为每个租户创建单独的虚拟主机。我们面临的问题是如何在单个spring启动应用程序中读取来自不同虚拟主机的消息。
我们使用的是spring boot spring-boot-starter-amqp。
你能让我知道这是否可以做到(如果可以的话,如何做到)?任何高级代码都是有用的吗?
发布于 2021-03-11 23:05:53
在这里,configTemplateRabbitMQ只是一个包含用户名、密码和地址的对象。
通过使用cachingConnectionFactory,您可以设置所有属性。
您可以动态传递信息。
pom.xml
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>2.2.10.RELEASE</version>
</dependency>代码
public void publish(String message, ConfigTemplateRabbitMQ configTemplateRabbitMQ) {
logger.info("publishing message: {}", message);
if (message != null) {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setUsername(configTemplateRabbitMQ.getUsername());
connectionFactory.setPassword(configTemplateRabbitMQ.getPassword());
connectionFactory.setAddresses(configTemplateRabbitMQ.getAddresses());
AmqpTemplate dynamicRabbitTemplate = new RabbitTemplate(connectionFactory);
dynamicRabbitTemplate.convertAndSend(configTemplateRabbitMQ.getExchangeName(), configTemplateRabbitMQ.getPrefix(), message);
}
else
logger.error("message is empty!");
}发布于 2021-03-11 21:44:41
启动自动配置只能配置一个连接工厂。
您需要自己注册基础架构bean(连接工厂、模板、容器工厂)。
发布于 2021-03-12 03:52:24
您可以使用相应的AmqpAdmin声明多个连接工厂。在声明AmqpAdmin之后,您可以将它们绑定到您使用的交换/队列。
下面是一些示例@Configuration类代码,用于声明两个不同虚拟主机上的两个交换。
@Configuration
public class RabbitConfiguration {
@Bean
public ConnectionFactory firstVhostConnectionFactory(final RabbitProperties properties, @Value("${application.vhost1}") String firstVhostName) {
var connFactory = new CachingConnectionFactory();
connFactory.setHost(properties.getHost());
connFactory.setPort(properties.getPort());
connFactory.setUsername(properties.getUsername());
connFactory.setPassword(properties.getPassword());
connFactory.setVirtualHost(firstVhostName);
return connFactory;
}
@Bean
ConnectionFactory secondVhostConnectionFactory(final RabbitProperties properties, @Value("${application.vhost2}") String secondVhostName) {
var connectionFactory = new CachingConnectionFactory();
connFactory.setHost(properties.getHost());
connFactory.setPort(properties.getPort());
connectionFactory.setVirtualHost(secondVhostName);
connFactory.setUsername(properties.getUsername());
connFactory.setPassword(properties.getPassword());
return connectionFactory;
}
@Bean
AmqpAdmin firstVhostAdmin() {
var admin = new RabbitAdmin(firstVhostConnectionFactory());
admin.afterPropertiesSet();
return admin;
}
@Bean
AmqpAdmin secondVhostAdmin() {
var admin = new RabbitAdmin(secondVhostConnectionFactory());
admin.afterPropertiesSet();
return admin;
}
@Bean
FanoutExchange orderStatusRequestExchange() {
var exchange = new FanoutExchange("secondVhostExchange", true, false);
// This exchange will be declared on first vhost
exchange.setAdminsThatShouldDeclare(firstVhostAdmin());
return exchange;
}
@Bean
FanoutExchange secondVhostExchange() {
var exchange = new FanoutExchange("secondVhostExchange", true, false);
// This exchange will be declared on second vhost
exchange.setAdminsThatShouldDeclare(secondVhostAdmin());
return exchange;
}
@Bean
Queue orderStatusRequestExchange() {
var exchange = new FanoutExchange("secondVhostExchange", true, false);
// This exchange will be declared on first vhost
exchange.setAdminsThatShouldDeclare(firstVhostAdmin());
return exchange;
}
@Bean
Queue secondVhostExchange() {
var exchange = new FanoutExchange("secondVhostExchange", true, false);
// This exchange will be declared on second vhost
exchange.setAdminsThatShouldDeclare(secondVhostAdmin());
return exchange;
}
@Bean
Queue firstVhostQueue() {
var queue = new AnonymousQueue();
queue.setAdminsThatShouldDeclare(firstVhostAdmin());
return queue;
}
@Bean
Queue secondVhostQueue() {
var queue = new AnonymousQueue();
queue.setAdminsThatShouldDeclare(secondVhostAdmin());
return queue;
}
@Bean
Binding firstVhostBinding(FanoutExchange firstVhostExchange, Queue firstVhostQueue) {
return BindingBuilder.bind(firstVhostQueue).to(firstVhostExchange);
}
@Bean
Binding secondVhostBinding(FanoutExchange secondVhostExchange, Queue secondVhostQueue) {
return BindingBuilder.bind(secondVhostQueue).to(secondVhostExchange);
}
}在此之后,您可以在@RabbitListener中使用声明的队列,并从两个vhost获取数据。
https://stackoverflow.com/questions/66577590
复制相似问题