Skip to content

Commit

Permalink
2025-01-04 16:11:18
Browse files Browse the repository at this point in the history
  • Loading branch information
yingzhuo committed Jan 4, 2025
1 parent 887cb11 commit d7ca043
Show file tree
Hide file tree
Showing 19 changed files with 148 additions and 28 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.yingzhuo</groupId>
<artifactId>spring-turbo-modules</artifactId>
<version>3.4.1-3</version>
<version>3.4.1-3-SNAPSHOT</version>
<modules>
<module>spring-turbo-module-configuration</module>
<module>spring-turbo-module-jackson</module>
Expand Down
2 changes: 1 addition & 1 deletion spring-turbo-module-configuration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.github.yingzhuo</groupId>
<artifactId>spring-turbo-modules</artifactId>
<version>3.4.1-3</version>
<version>3.4.1-3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-turbo-module-configuration</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion spring-turbo-module-jackson/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.github.yingzhuo</groupId>
<artifactId>spring-turbo-modules</artifactId>
<version>3.4.1-3</version>
<version>3.4.1-3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-turbo-module-jackson</artifactId>
Expand Down
Empty file.
2 changes: 1 addition & 1 deletion spring-turbo-module-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.github.yingzhuo</groupId>
<artifactId>spring-turbo-modules</artifactId>
<version>3.4.1-3</version>
<version>3.4.1-3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-turbo-module-jdbc</artifactId>
Expand Down
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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.Ordered;

/**
* @author 应卓
* @see DataSourceSwitch
* @since 3.4.1
*/
@Aspect
public record DataSourceSwitchAdvice(int order) implements Ordered {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package spring.turbo.module.jdbc.ds.hikari;
package spring.turbo.module.jdbc.ds;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetails;

import java.io.Serializable;

/**
* @author 应卓
* @since 3.4.1
*/
@Getter
@Setter
public class HikariProperties implements JdbcConnectionDetails, Serializable {
Expand All @@ -22,7 +26,9 @@ public class HikariProperties implements JdbcConnectionDetails, Serializable {
private long idleTimeout = 30000L;
private long maxLifetime = 900000L;
private long connectionTimeout = 10000;
private String connectionTestQuery = "SELECT 1 FROM DUAL";
private String connectionTestQuery = null;
private long validationTimeout = 1000L;
private String connectionInitSql = null;
private long initializationFailTimeout = 1000L;

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,6 @@
*/
public class RoutingDataSource extends AbstractRoutingDataSource implements DataSource, InitializingBean {

public RoutingDataSource(String defaultDataSourceName, Map<String, DataSource> targetDataSources) {
Assert.hasText(defaultDataSourceName, "defaultDataSourceName is required");
Assert.notEmpty(targetDataSources, "targetDataSources is null or empty");

super.setDefaultTargetDataSource(defaultDataSourceName);
super.setTargetDataSources(new HashMap<>(targetDataSources));
super.setDataSourceLookup(new MapDataSourceLookup());
}

public RoutingDataSource(DataSource defaultDataSource, Map<String, DataSource> targetDataSources) {
Assert.notNull(defaultDataSource, "defaultDataSource is required");
Assert.notEmpty(targetDataSources, "targetDataSources is null or empty");
Expand Down
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;

}

This file was deleted.

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
}

}
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;
2 changes: 1 addition & 1 deletion spring-turbo-module-jwt/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.github.yingzhuo</groupId>
<artifactId>spring-turbo-modules</artifactId>
<version>3.4.1-3</version>
<version>3.4.1-3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-turbo-module-jwt</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion spring-turbo-module-misc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.github.yingzhuo</groupId>
<artifactId>spring-turbo-modules</artifactId>
<version>3.4.1-3</version>
<version>3.4.1-3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-turbo-module-misc</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion spring-turbo-module-redis/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.github.yingzhuo</groupId>
<artifactId>spring-turbo-modules</artifactId>
<version>3.4.1-3</version>
<version>3.4.1-3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-turbo-module-redis</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion spring-turbo-module-security/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.github.yingzhuo</groupId>
<artifactId>spring-turbo-modules</artifactId>
<version>3.4.1-3</version>
<version>3.4.1-3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-turbo-module-security</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion spring-turbo-module-webcli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.github.yingzhuo</groupId>
<artifactId>spring-turbo-modules</artifactId>
<version>3.4.1-3</version>
<version>3.4.1-3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-turbo-module-webcli</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion spring-turbo-module-webmvc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.github.yingzhuo</groupId>
<artifactId>spring-turbo-modules</artifactId>
<version>3.4.1-3</version>
<version>3.4.1-3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-turbo-module-webmvc</artifactId>
Expand Down

0 comments on commit d7ca043

Please sign in to comment.