-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
148 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...n/java/spring/turbo/module/jdbc/autoconfiguration/RoutingDataSourceAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,63 @@ | ||
package spring.turbo.module.jdbc.autoconfiguration; | ||
|
||
import com.zaxxer.hikari.HikariDataSource; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.util.Assert; | ||
import spring.turbo.module.jdbc.ds.DataSourceSwitchAdvice; | ||
import spring.turbo.module.jdbc.ds.RoutingDataSource; | ||
import spring.turbo.module.jdbc.ds.RoutingDataSourceProperties; | ||
import spring.turbo.module.jdbc.util.DataSourceFactories; | ||
|
||
import javax.sql.DataSource; | ||
import java.util.HashMap; | ||
|
||
/** | ||
* @author 应卓 | ||
* @since 3.4.1 | ||
*/ | ||
@EnableConfigurationProperties(RoutingDataSourceProperties.class) | ||
@ConditionalOnProperty(prefix = "springturbo.routing-data-source", name = "enabled", havingValue = "true", matchIfMissing = true) | ||
public class RoutingDataSourceAutoConfiguration { | ||
|
||
@Bean | ||
@ConditionalOnMissingBean(RoutingDataSource.class) | ||
public DataSource dataSource(RoutingDataSourceProperties props) { | ||
final var targetDataSources = new HashMap<String, DataSource>(); | ||
|
||
props.getHikariDataSources().forEach((dataSourceName, dataSourceConfig) -> { | ||
var dataSource = (HikariDataSource) DataSourceFactories.createDataSource(dataSourceConfig, HikariDataSource.class); | ||
|
||
dataSource.setPoolName(dataSourceConfig.getPoolName()); | ||
dataSource.setMinimumIdle(dataSourceConfig.getMinimumIdle()); | ||
dataSource.setMaximumPoolSize(dataSourceConfig.getMaximumPoolSize()); | ||
dataSource.setAutoCommit(dataSourceConfig.isAutoCommit()); | ||
dataSource.setIdleTimeout(dataSourceConfig.getIdleTimeout()); | ||
dataSource.setMaxLifetime(dataSourceConfig.getMaxLifetime()); | ||
dataSource.setConnectionTimeout(dataSourceConfig.getConnectionTimeout()); | ||
dataSource.setConnectionTestQuery(dataSourceConfig.getConnectionTestQuery()); | ||
dataSource.setValidationTimeout(dataSourceConfig.getValidationTimeout()); | ||
dataSource.setConnectionInitSql(dataSourceConfig.getConnectionInitSql()); | ||
dataSource.setInitializationFailTimeout(dataSourceConfig.getInitializationFailTimeout()); | ||
|
||
targetDataSources.put(dataSourceName, dataSource); | ||
}); | ||
|
||
var defaultDataSource = targetDataSources.get(props.getDefaultDataSourceName()); | ||
Assert.notNull(defaultDataSource, "unable to find defaultDataSource"); | ||
|
||
return new RoutingDataSource(defaultDataSource, targetDataSources); | ||
} | ||
|
||
@Bean | ||
@ConditionalOnClass(name = "org.aspectj.lang.annotation.Aspect") | ||
@ConditionalOnMissingBean(DataSourceSwitchAdvice.class) | ||
public DataSourceSwitchAdvice dataSourceSwitchAdvice() { | ||
return new DataSourceSwitchAdvice(Ordered.HIGHEST_PRECEDENCE); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...bo-module-jdbc/src/main/java/spring/turbo/module/jdbc/ds/RoutingDataSourceProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package spring.turbo.module.jdbc.ds; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
import java.io.Serializable; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author 应卓 | ||
* @since 3.4.1 | ||
*/ | ||
@Getter | ||
@Setter | ||
@ConfigurationProperties(prefix = "springturbo.routing-data-source") | ||
public class RoutingDataSourceProperties implements Serializable { | ||
|
||
private boolean enabled = true; | ||
private String defaultDataSourceName; | ||
private Map<String, HikariProperties> hikariDataSources; | ||
|
||
} |
6 changes: 0 additions & 6 deletions
6
spring-turbo-module-jdbc/src/main/java/spring/turbo/module/jdbc/ds/hikari/package-info.java
This file was deleted.
Oops, something went wrong.
42 changes: 42 additions & 0 deletions
42
...ng-turbo-module-jdbc/src/main/java/spring/turbo/module/jdbc/util/DataSourceFactories.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package spring.turbo.module.jdbc.util; | ||
|
||
import org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetails; | ||
import org.springframework.boot.jdbc.DataSourceBuilder; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.util.ClassUtils; | ||
|
||
import javax.sql.DataSource; | ||
import java.util.Objects; | ||
|
||
/** | ||
* 数据源创建工具 | ||
* | ||
* @author 应卓 | ||
* @since 3.4.1 | ||
*/ | ||
public final class DataSourceFactories { | ||
|
||
/** | ||
* 私有构造方法 | ||
*/ | ||
private DataSourceFactories() { | ||
} | ||
|
||
public static <T> T createDataSource(JdbcConnectionDetails connectionDetails, Class<? extends DataSource> type) { | ||
return createDataSource(connectionDetails, type, null); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public static <T> T createDataSource(JdbcConnectionDetails connectionDetails, Class<? extends DataSource> type, @Nullable ClassLoader classLoader) { | ||
// @formatter:off | ||
return (T) DataSourceBuilder.create(Objects.requireNonNullElse(classLoader, ClassUtils.getDefaultClassLoader())) | ||
.type(type) | ||
.driverClassName(connectionDetails.getDriverClassName()) | ||
.url(connectionDetails.getJdbcUrl()) | ||
.username(connectionDetails.getUsername()) | ||
.password(connectionDetails.getPassword()) | ||
.build(); | ||
// @formatter:on | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
...ng.turbo.module.jackson/package-info.java → .../turbo/module/jdbc/util/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
@NonNullApi | ||
@NonNullFields | ||
package spring.turbo.module.jackson; | ||
package spring.turbo.module.jdbc.util; | ||
|
||
import org.springframework.lang.NonNullApi; | ||
import org.springframework.lang.NonNullFields; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters