使用Spingboot

时间:2018-12-04 10:53:01

标签: java spring-mvc spring-boot spring-data multi-tenant

我正在尝试在我的项目中实现多租户架构。我指的是“ http://anakiou.blogspot.com/2015/08/multi-tenant-application-with-spring.html”示例。在此示例中,一切正常。但是此示例中的问题是,每当要添加新的数据源时,我们都应该手动添加它。但是我想要诸如在不重新启动应用程序的情况下动态添加数据源之类的东西。

这是我的application.properties

spring.jpa.hibernate.ddl-auto=none
spring.thymeleaf.cache=false

spring.datasource.datasource1.url=jdbc:mysql://localhost:3306/tenant_1
spring.datasource.datasource1.username=root
spring.datasource.datasource1.password=root
spring.datasource.datasource1.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.datasource2.url=jdbc:mysql://localhost:3306/tenant_2
spring.datasource.datasource2.username=root
spring.datasource.datasource2.password=root
spring.datasource.datasource2.driver-class-name=com.mysql.jdbc.Driver

这是DataSourceConfig.java

package com.anakiou.mt.config;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
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;

@Configuration
public class DataSourceConfig {

@Autowired
private MultitenancyProperties multitenancyProperties;

@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();
}

@Primary
@Bean(name = "dataSource2")
@ConfigurationProperties(prefix = "spring.datasource.datasource2")
public DataSource dataSource2() {
    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();
}
}

MultitenancyProperties.java

package com.anakiou.mt.config;

import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

@ConfigurationProperties("spring.datasource")
public class MultitenancyProperties {

@NestedConfigurationProperty
private DataSourceProperties datasource1;

@NestedConfigurationProperty
private DataSourceProperties datasource2;


public DataSourceProperties getDatasource1() {
    return datasource1;
}

public void setDatasource1(DataSourceProperties datasource1) {
    this.datasource1 = datasource1;
}

public DataSourceProperties getDatasource2() {
    return datasource2;
}

public void setDatasource2(DataSourceProperties datasource2) {
    this.datasource2 = datasource2;
}

}

DataSourceBasedMultiTenantConnectionProviderImpl.java

package com.anakiou.mt.util;

import org.hibernate.engine.jdbc.connections.spi.AbstractDataSourceBasedMultiTenantConnectionProviderImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Component
public class DataSourceBasedMultiTenantConnectionProviderImpl extends 
AbstractDataSourceBasedMultiTenantConnectionProviderImpl {

private static final long serialVersionUID = 8168907057647334460L;
private static final String DEFAULT_TENANT_ID = "tenant_1";

@Autowired
private DataSource dataSource1;

@Autowired
private DataSource dataSource2;

private Map<String, DataSource> map;

@PostConstruct
public void load() {
    map = new HashMap<>();
    map.put("tenant_1", dataSource1);
    map.put("tenant_2", dataSource2);
}

@Override
protected DataSource selectAnyDataSource() {
    return map.get(DEFAULT_TENANT_ID);
}

@Override
protected DataSource selectDataSource(String tenantIdentifier) {
    return map.get(tenantIdentifier);
}
}

0 个答案:

没有答案