https://github.com/wmeints/spring-multi-tenant-demo 按照这个逻辑,我现在可以解决了。有些版本需要升级,代码也需要升级。 Spring 启动版本已更改。 org.springframework.boot spring boot starter父版本2.1.0.release mysql版本已被删除。 还有一些小的变化 MultitenantConfiguration.java ``` @Configuration public class MultitenantConfiguration {
// Create the final multi-tenant source. // It needs a default database to connect to. // Make sure that the default database is actually an empty tenant database. // Don't use that for a regular tenant if you want things to be safe! MultitenantDataSource dataSource = new MultitenantDataSource(); dataSource.setDefaultTargetDataSource(defaultDataSource()); dataSource.setTargetDataSources(resolvedDataSources);
// Call this to finalize the initialization of the data source. dataSource.afterPropertiesSet();
return dataSource; }
/**
Creates the default data source for the application
2条答案
按热度按时间55ooxyrt1#
你可以用
AbstractRoutingDataSource
为了达到这个目的。AbstractRoutingDataSource
需要信息来知道DataSource
路由到(称为上下文),由determineCurrentLookupKey()
方法。用这里的例子。定义上下文如下:
然后您需要定义将在中使用的上下文持有者
determineCurrentLookupKey()
```public class ClientDatabaseContextHolder {
}
public class ClientDataSourceRouter extends AbstractRoutingDataSource {
}
@Bean
public DataSource clientDatasource() {
Map<Object, Object> targetDataSources = new HashMap<>();
DataSource clientADatasource = clientADatasource();
DataSource clientBDatasource = clientBDatasource();
targetDataSources.put(ClientDatabase.CLIENT_A,
clientADatasource);
targetDataSources.put(ClientDatabase.CLIENT_B,
clientBDatasource);
}
falq053o2#
https://github.com/wmeints/spring-multi-tenant-demo
按照这个逻辑,我现在可以解决了。有些版本需要升级,代码也需要升级。
Spring 启动版本已更改。
org.springframework.boot spring boot starter父版本2.1.0.release
mysql版本已被删除。
还有一些小的变化
MultitenantConfiguration.java
```@Configuration
public class MultitenantConfiguration {
@Autowired
private DataSourceProperties properties;
/**
Defines the data source for the application
@return
*/
@Bean
@ConfigurationProperties(
prefix = "spring.datasource"
)
public DataSource dataSource() {
File[] files = Paths.get("tenants").toFile().listFiles();
Map<Object,Object> resolvedDataSources = new HashMap<>();
if(files != null) {
for (File propertyFile : files) {
Properties tenantProperties = new Properties();
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create(this.getClass().getClassLoader());
}
// Create the final multi-tenant source.
// It needs a default database to connect to.
// Make sure that the default database is actually an empty tenant database.
// Don't use that for a regular tenant if you want things to be safe!
MultitenantDataSource dataSource = new MultitenantDataSource();
dataSource.setDefaultTargetDataSource(defaultDataSource());
dataSource.setTargetDataSources(resolvedDataSources);
// Call this to finalize the initialization of the data source.
dataSource.afterPropertiesSet();
return dataSource;
}
/**
Creates the default data source for the application
@return
*/
private DataSource defaultDataSource() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create(this.getClass().getClassLoader())
.driverClassName(properties.getDriverClassName())
.url(properties.getUrl())
.username(properties.getUsername())
.password(properties.getPassword());
if(properties.getType() != null) {
dataSourceBuilder.type(properties.getType());
}
return dataSourceBuilder.build();
}