Skip to content

Commit

Permalink
2025-01-04 13:54:30
Browse files Browse the repository at this point in the history
  • Loading branch information
yingzhuo committed Jan 4, 2025
1 parent bc5e949 commit 887cb11
Show file tree
Hide file tree
Showing 26 changed files with 273 additions and 10 deletions.
3 changes: 2 additions & 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-2</version>
<version>3.4.1-3</version>
<modules>
<module>spring-turbo-module-configuration</module>
<module>spring-turbo-module-jackson</module>
Expand All @@ -16,6 +16,7 @@
<module>spring-turbo-module-webcli</module>
<module>spring-turbo-module-webmvc</module>
<module>spring-turbo-module-redis</module>
<module>spring-turbo-module-jdbc</module>
</modules>
<packaging>pom</packaging>

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-2</version>
<version>3.4.1-3</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-2</version>
<version>3.4.1-3</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-turbo-module-jackson</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@NonNullApi
@NonNullFields
package spring.turbo.module.jackson;

import org.springframework.lang.NonNullApi;
import org.springframework.lang.NonNullFields;
53 changes: 53 additions & 0 deletions spring-turbo-module-jdbc/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<parent>
<groupId>com.github.yingzhuo</groupId>
<artifactId>spring-turbo-modules</artifactId>
<version>3.4.1-3</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-turbo-module-jdbc</artifactId>

<dependencies>
<!-- spring-turbo & modules -->
<dependency>
<groupId>com.github.yingzhuo</groupId>
<artifactId>spring-turbo</artifactId>
</dependency>

<!-- spring-boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<scope>provided</scope>
</dependency>

<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package spring.turbo.module.jdbc.autoconfiguration;

public class RoutingDataSourceAutoConfiguration {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@NonNullApi
@NonNullFields
package spring.turbo.module.jdbc.autoconfiguration;

import org.springframework.lang.NonNullApi;
import org.springframework.lang.NonNullFields;
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package spring.turbo.module.jdbc.ds;

import java.lang.annotation.*;

/**
* 数据源切换
*
* @author 应卓
* @since 3.4.1
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface DataSourceSwitch {

/**
* 数据源名称
*
* @return 数据源名称
*/
public String value();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package spring.turbo.module.jdbc.ds;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.Ordered;

@Aspect
public record DataSourceSwitchAdvice(int order) implements Ordered {

@Around("@annotation(spring.turbo.module.jdbc.ds.DataSourceSwitch)")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
var signature = joinPoint.getSignature();
if (signature instanceof MethodSignature methodSignature) {
var annotation = methodSignature.getMethod().getAnnotation(DataSourceSwitch.class);
var dataSourceName = annotation.value();
RoutingDataSourceLookup.set(dataSourceName);
try {
return joinPoint.proceed();
} finally {
RoutingDataSourceLookup.remove();
}
} else {
return joinPoint.proceed();
}
}

@Override
public int getOrder() {
return order;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package spring.turbo.module.jdbc.ds;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import org.springframework.jdbc.datasource.lookup.MapDataSourceLookup;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

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

/**
* 路由数据源
*
* @author 应卓
* @see AbstractRoutingDataSource
* @see RoutingDataSourceLookup
* @since 3.4.1
*/
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");

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

@Override
@Nullable
protected Object determineCurrentLookupKey() {
return RoutingDataSourceLookup.get();
}

@Override
public void afterPropertiesSet() {
super.afterPropertiesSet();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package spring.turbo.module.jdbc.ds;

import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

/**
* 路由数据源查找策略
*
* @author 应卓
* @since 3.4.1
*/
public final class RoutingDataSourceLookup {

private static final ThreadLocal<String> DS_NAME_HOLDER = ThreadLocal.withInitial(() -> null);

/**
* 私有构造方法
*/
private RoutingDataSourceLookup() {
super();
}

public static void set(String dataSourceName) {
Assert.hasText(dataSourceName, "dataSourceName is required");
DS_NAME_HOLDER.set(dataSourceName);
}

@Nullable
public static String get() {
return DS_NAME_HOLDER.get();
}

public static void remove() {
DS_NAME_HOLDER.remove();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package spring.turbo.module.jdbc.ds.hikari;

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

import java.io.Serializable;

@Getter
@Setter
public class HikariProperties implements JdbcConnectionDetails, Serializable {

private String jdbcUrl;
private String username;
private String password;
private String driverClassName;

private String poolName = null;
private int minimumIdle = 10;
private int maximumPoolSize = 30;
private boolean autoCommit = true;
private long idleTimeout = 30000L;
private long maxLifetime = 900000L;
private long connectionTimeout = 10000;
private String connectionTestQuery = "SELECT 1 FROM DUAL";
private long validationTimeout = 1000L;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@NonNullApi
@NonNullFields
package spring.turbo.module.jdbc.ds.hikari;

import org.springframework.lang.NonNullApi;
import org.springframework.lang.NonNullFields;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@NonNullApi
@NonNullFields
package spring.turbo.module.jdbc.ds;

import org.springframework.lang.NonNullApi;
import org.springframework.lang.NonNullFields;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@NonNullApi
@NonNullFields
package spring.turbo.module.jdbc;

import org.springframework.lang.NonNullApi;
import org.springframework.lang.NonNullFields;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.turbo.module.jdbc.autoconfiguration.RoutingDataSourceAutoConfiguration
Empty file.
Empty file.
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-2</version>
<version>3.4.1-3</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-2</version>
<version>3.4.1-3</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-2</version>
<version>3.4.1-3</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-2</version>
<version>3.4.1-3</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-turbo-module-security</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static TokenResolverBuilder builder() {
*
* @return 排序值
* @see CompositeTokenResolver
* @see Ordered#getOrder()
* @see Ordered#order()
* @see Ordered#LOWEST_PRECEDENCE
* @see Ordered#HIGHEST_PRECEDENCE
* @see org.springframework.core.OrderComparator
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-2</version>
<version>3.4.1-3</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-2</version>
<version>3.4.1-3</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-turbo-module-webmvc</artifactId>
Expand Down

0 comments on commit 887cb11

Please sign in to comment.