数据源之间的多租户动态切换不起作用

时间:2018-11-22 07:32:25

标签: java spring spring-boot multi-tenant spring-config

我正在尝试在Spring Boot应用程序中实现多租户。我指的是“ http://anakiou.blogspot.com/2015/08/multi-tenant-application-with-spring.html”示例。在此示例中,一切正常。但是在我的代码中,没有在数据源之间进行切换。

在我的DataSourceConfig.java中,我遇到了声明3个Bean的问题,所以我正在使用@Primary批注。如果删除@Primary注释,则会出现以下错误

' Parameter 1 of method liquibase in com.config.DatabaseConfiguration required a single bean, but 3 were found:
- dataSource: defined by method 'dataSource1' in class path resource [com/config/DataSourceConfig.class]
- dataSource2: defined by method 'dataSource2' in class path resource [com/config/DataSourceConfig.class]
- dataSource3: defined by method 'dataSource3' in class path resource [com/config/DataSourceConfig.class] ' 

但是,如果我使用@Primary批注而不考虑租户ID,它会将数据存储在以@Primary批注的DataSource中。因此,不会发生数据源的切换。

这是我的DataSourceConfig类

package com.config;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
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 com.netflix.governator.annotations.binding.Secondary;

@Configuration
public class DataSourceConfig {

@Autowired
private MultitenancyProperties multitenancyProperties;

@Primary
@Bean(name =  {"dataSource" , "dataSource1"})
@ConfigurationProperties(prefix = "spring.datasource.datasource1")
public DataSource dataSource1() {
    DataSourceBuilder factory = DataSourceBuilder
            .create(this.multitenancyProperties.getDatasource1().getClassLoader())
            //.driverClassName(this.multitenancyProperties.getDatasource1().getDriverClassName())
            .username(this.multitenancyProperties.getDatasource1().getUsername())
            .password(this.multitenancyProperties.getDatasource1().getPassword())
            .url(this.multitenancyProperties.getDatasource1().getUrl());
    return factory.build();
}


@Bean(name = {"dataSource2"})
@ConfigurationProperties(prefix = "spring.datasource.datasource2")
public DataSource dataSource2(@Qualifier("dataSource1") DataSource dataSource1) {
    System.out.println("Entered to 2");
    DataSourceBuilder factory = DataSourceBuilder
            .create(this.multitenancyProperties.getDatasource2().getClassLoader())
            //.driverClassName(this.multitenancyProperties.getDatasource2().getDriverClassName())
            .username(this.multitenancyProperties.getDatasource2().getUsername())
            .password(this.multitenancyProperties.getDatasource2().getPassword())
            .url(this.multitenancyProperties.getDatasource2().getUrl());
    return factory.build();
}


@Bean(name = {"dataSource3"})
@ConfigurationProperties(prefix = "spring.datasource.datasource3")
public DataSource dataSource3() {
    DataSourceBuilder factory = DataSourceBuilder
            .create(this.multitenancyProperties.getDatasource3().getClassLoader())
            //.driverClassName(this.multitenancyProperties.getDatasource3().getDriverClassName())
            .username(this.multitenancyProperties.getDatasource3().getUsername())
            .password(this.multitenancyProperties.getDatasource3().getPassword())
            .url(this.multitenancyProperties.getDatasource3().getUrl());
    return factory.build();
}}

1 个答案:

答案 0 :(得分:1)

禁用Spring的DataSourceAutoConfiguration并提供我们的多租户DataSourceConfig。这将从MultitenancyProperties中加载属性,该属性又由application.properties配置,并相应地配置我们的数据源。要排除DataSourceAutoConfiguration,请使用@EnableAutoConfiguration

@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})