[英]Can I develop multi tenancy application by using RabbitMQ and spring boot?
我想在我當前的項目中使用 RabbitMQ 和 Spring Boot 來實現多租戶應用程序。 目前該應用程序以單租戶方式運行。 該方法是為每個租戶創建單獨的虛擬主機。 我們面臨的問題是如何在單個 spring 啟動應用程序中讀取來自不同虛擬主機的消息。
我們正在使用 spring 啟動 spring-boot-starter-amqp。
你能否讓我知道這是否可以做到(如果可以的話)? 任何高級代碼都會有用嗎?
開機自動配置只能配置一個連接工廠。
您需要自己注冊基礎設施 bean(連接工廠、模板、容器工廠)。
這里的configTemplateRabbitMQ
是一個包含用戶名、密碼和地址的 object。
通過使用cachingConnectionFactory
,您可以設置所有屬性。
您可以動態傳遞信息。
<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!");
}
您可以使用相應的AmqpAdmin
聲明多個連接工廠。 聲明AmqpAdmin
后,您可以將它們綁定到您使用的交換/隊列。
這是一些示例@Configuration
class 代碼,聲明了兩個不同虛擬主機上的兩個交換。
@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
中使用聲明的隊列並從兩個虛擬主機中獲取數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.