首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我可以使用RabbitMQ和spring boot开发多租户应用程序吗?

我可以使用RabbitMQ和spring boot开发多租户应用程序吗?
EN

Stack Overflow用户
提问于 2021-03-11 14:40:50
回答 3查看 181关注 0票数 1

我想在我当前的项目中使用RabbitMQ和Spring Boot实现多租户应用程序。目前,该应用程序以单租户的方式运行。方法是为每个租户创建单独的虚拟主机。我们面临的问题是如何在单个spring启动应用程序中读取来自不同虚拟主机的消息。

我们使用的是spring boot spring-boot-starter-amqp。

你能让我知道这是否可以做到(如果可以的话,如何做到)?任何高级代码都是有用的吗?

EN

回答 3

Stack Overflow用户

发布于 2021-03-11 23:05:53

在这里,configTemplateRabbitMQ只是一个包含用户名、密码和地址的对象。

通过使用cachingConnectionFactory,您可以设置所有属性。

您可以动态传递信息。

pom.xml

代码语言:javascript
复制
<dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit</artifactId>
            <version>2.2.10.RELEASE</version>
</dependency>

代码

代码语言:javascript
复制
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!");
    }
票数 1
EN

Stack Overflow用户

发布于 2021-03-11 21:44:41

启动自动配置只能配置一个连接工厂。

您需要自己注册基础架构bean(连接工厂、模板、容器工厂)。

票数 0
EN

Stack Overflow用户

发布于 2021-03-12 03:52:24

您可以使用相应的AmqpAdmin声明多个连接工厂。在声明AmqpAdmin之后,您可以将它们绑定到您使用的交换/队列。

下面是一些示例@Configuration类代码,用于声明两个不同虚拟主机上的两个交换。

代码语言:javascript
复制
@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获取数据。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66577590

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档