mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-11 16:57:14 +08:00
refactor(extension/tenant): 将 dynamic-datasource 依赖设置为 optional
This commit is contained in:
@@ -52,9 +52,9 @@ public class TenantProperties {
|
|||||||
private String tenantIdHeader = "X-Tenant-Id";
|
private String tenantIdHeader = "X-Tenant-Id";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 超级租户 ID
|
* 超级/默认租户 ID
|
||||||
*/
|
*/
|
||||||
private Long superTenantId = 1L;
|
private Long superTenantId = 0L;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 忽略表(忽略拼接租户条件)
|
* 忽略表(忽略拼接租户条件)
|
||||||
|
@@ -30,9 +30,9 @@ public interface TenantProvider {
|
|||||||
/**
|
/**
|
||||||
* 根据租户 ID 获取租户上下文
|
* 根据租户 ID 获取租户上下文
|
||||||
*
|
*
|
||||||
* @param tenantId 租户 ID
|
* @param tenantIdAsString 租户 ID 字符串
|
||||||
* @param isVerify 是否验证有效性
|
* @param isVerify 是否验证有效性
|
||||||
* @return 租户上下文
|
* @return 租户上下文
|
||||||
*/
|
*/
|
||||||
TenantContext getByTenantId(String tenantId, boolean isVerify);
|
TenantContext getByTenantId(String tenantIdAsString, boolean isVerify);
|
||||||
}
|
}
|
||||||
|
@@ -32,6 +32,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.baomidou</groupId>
|
<groupId>com.baomidou</groupId>
|
||||||
<artifactId>dynamic-datasource-spring-boot3-starter</artifactId>
|
<artifactId>dynamic-datasource-spring-boot3-starter</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
@@ -16,8 +16,6 @@
|
|||||||
|
|
||||||
package top.continew.starter.extension.tenant.autoconfigure;
|
package top.continew.starter.extension.tenant.autoconfigure;
|
||||||
|
|
||||||
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
|
|
||||||
import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
|
|
||||||
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
|
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
|
||||||
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
|
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
|
||||||
import jakarta.annotation.PostConstruct;
|
import jakarta.annotation.PostConstruct;
|
||||||
@@ -25,6 +23,7 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
@@ -83,6 +82,7 @@ public class TenantAutoConfiguration {
|
|||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
|
@ConditionalOnClass(name = "com.baomidou.dynamic.datasource.DynamicRoutingDataSource")
|
||||||
public TenantDataSourceAdvisor tenantDataSourceAdvisor(TenantDataSourceInterceptor tenantDataSourceInterceptor) {
|
public TenantDataSourceAdvisor tenantDataSourceAdvisor(TenantDataSourceInterceptor tenantDataSourceInterceptor) {
|
||||||
return new TenantDataSourceAdvisor(tenantDataSourceInterceptor);
|
return new TenantDataSourceAdvisor(tenantDataSourceInterceptor);
|
||||||
}
|
}
|
||||||
@@ -92,6 +92,7 @@ public class TenantAutoConfiguration {
|
|||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
|
@ConditionalOnClass(name = "com.baomidou.dynamic.datasource.DynamicRoutingDataSource")
|
||||||
public TenantDataSourceInterceptor tenantDataSourceInterceptor(TenantDataSourceHandler tenantDataSourceHandler) {
|
public TenantDataSourceInterceptor tenantDataSourceInterceptor(TenantDataSourceHandler tenantDataSourceHandler) {
|
||||||
return new TenantDataSourceInterceptor(tenantDataSourceHandler);
|
return new TenantDataSourceInterceptor(tenantDataSourceHandler);
|
||||||
}
|
}
|
||||||
@@ -101,9 +102,9 @@ public class TenantAutoConfiguration {
|
|||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
public TenantDataSourceHandler tenantDataSourceHandler(DataSource dataSource,
|
@ConditionalOnClass(name = "com.baomidou.dynamic.datasource.DynamicRoutingDataSource")
|
||||||
DefaultDataSourceCreator dataSourceCreator) {
|
public TenantDataSourceHandler tenantDataSourceHandler(DataSource dataSource) {
|
||||||
return new DefaultTenantDataSourceHandler((DynamicRoutingDataSource)dataSource, dataSourceCreator);
|
return new DefaultTenantDataSourceHandler(dataSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -124,8 +125,8 @@ public class TenantAutoConfiguration {
|
|||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
public TenantHandler tenantHandler(TenantDataSourceHandler tenantDataSourceHandler, TenantProvider tenantProvider) {
|
public TenantHandler tenantHandler(TenantProvider tenantProvider) {
|
||||||
return new DefaultTenantHandler(tenantProperties, tenantDataSourceHandler, tenantProvider);
|
return new DefaultTenantHandler(tenantProperties, tenantProvider);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
|
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package top.continew.starter.extension.tenant.handler;
|
package top.continew.starter.extension.tenant.handler;
|
||||||
|
|
||||||
|
import cn.hutool.extra.spring.SpringUtil;
|
||||||
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
|
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
|
||||||
import top.continew.starter.extension.tenant.TenantDataSourceHandler;
|
import top.continew.starter.extension.tenant.TenantDataSourceHandler;
|
||||||
import top.continew.starter.extension.tenant.TenantHandler;
|
import top.continew.starter.extension.tenant.TenantHandler;
|
||||||
@@ -34,14 +35,10 @@ import top.continew.starter.extension.tenant.enums.TenantIsolationLevel;
|
|||||||
public class DefaultTenantHandler implements TenantHandler {
|
public class DefaultTenantHandler implements TenantHandler {
|
||||||
|
|
||||||
private final TenantProperties tenantProperties;
|
private final TenantProperties tenantProperties;
|
||||||
private final TenantDataSourceHandler dataSourceHandler;
|
|
||||||
private final TenantProvider tenantProvider;
|
private final TenantProvider tenantProvider;
|
||||||
|
|
||||||
public DefaultTenantHandler(TenantProperties tenantProperties,
|
public DefaultTenantHandler(TenantProperties tenantProperties, TenantProvider tenantProvider) {
|
||||||
TenantDataSourceHandler dataSourceHandler,
|
|
||||||
TenantProvider tenantProvider) {
|
|
||||||
this.tenantProperties = tenantProperties;
|
this.tenantProperties = tenantProperties;
|
||||||
this.dataSourceHandler = dataSourceHandler;
|
|
||||||
this.tenantProvider = tenantProvider;
|
this.tenantProvider = tenantProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,16 +47,16 @@ public class DefaultTenantHandler implements TenantHandler {
|
|||||||
if (!tenantProperties.isEnabled()) {
|
if (!tenantProperties.isEnabled()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
TenantContext tenantHandler = tenantProvider.getByTenantId(tenantId.toString(), false);
|
TenantContext tenantContext = tenantProvider.getByTenantId(tenantId.toString(), false);
|
||||||
// 保存当前的租户上下文
|
// 保存当前的租户上下文
|
||||||
TenantContext originalContext = TenantContextHolder.getContext();
|
TenantContext originalContext = TenantContextHolder.getContext();
|
||||||
boolean isPush = false;
|
boolean isPush = false;
|
||||||
try {
|
try {
|
||||||
// 设置新的租户上下文
|
// 设置新的租户上下文
|
||||||
TenantContextHolder.setContext(tenantHandler);
|
TenantContextHolder.setContext(tenantContext);
|
||||||
// 切换数据源
|
// 切换数据源
|
||||||
if (TenantIsolationLevel.DATASOURCE.equals(tenantHandler.getIsolationLevel())) {
|
if (TenantIsolationLevel.DATASOURCE.equals(tenantContext.getIsolationLevel())) {
|
||||||
dataSourceHandler.changeDataSource(tenantHandler.getDataSource());
|
SpringUtil.getBean(TenantDataSourceHandler.class).changeDataSource(tenantContext.getDataSource());
|
||||||
isPush = true;
|
isPush = true;
|
||||||
}
|
}
|
||||||
// 执行业务逻辑
|
// 执行业务逻辑
|
||||||
|
@@ -17,6 +17,7 @@
|
|||||||
package top.continew.starter.extension.tenant.handler.datasource;
|
package top.continew.starter.extension.tenant.handler.datasource;
|
||||||
|
|
||||||
import cn.hutool.core.text.CharSequenceUtil;
|
import cn.hutool.core.text.CharSequenceUtil;
|
||||||
|
import cn.hutool.extra.spring.SpringUtil;
|
||||||
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
|
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
|
||||||
import com.baomidou.dynamic.datasource.creator.DataSourceProperty;
|
import com.baomidou.dynamic.datasource.creator.DataSourceProperty;
|
||||||
import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
|
import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
|
||||||
@@ -40,10 +41,9 @@ public class DefaultTenantDataSourceHandler implements TenantDataSourceHandler {
|
|||||||
private final DynamicRoutingDataSource dynamicRoutingDataSource;
|
private final DynamicRoutingDataSource dynamicRoutingDataSource;
|
||||||
private final DefaultDataSourceCreator dataSourceCreator;
|
private final DefaultDataSourceCreator dataSourceCreator;
|
||||||
|
|
||||||
public DefaultTenantDataSourceHandler(DynamicRoutingDataSource dynamicRoutingDataSource,
|
public DefaultTenantDataSourceHandler(DataSource dataSource) {
|
||||||
DefaultDataSourceCreator dataSourceCreator) {
|
this.dynamicRoutingDataSource = (DynamicRoutingDataSource)dataSource;
|
||||||
this.dynamicRoutingDataSource = dynamicRoutingDataSource;
|
this.dataSourceCreator = SpringUtil.getBean(DefaultDataSourceCreator.class);
|
||||||
this.dataSourceCreator = dataSourceCreator;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Reference in New Issue
Block a user