|
|
@@ -14,6 +14,7 @@ import java.util.function.Supplier;
|
|
|
/**
|
|
|
* 租户数据源切换工具类
|
|
|
* 用于在指定租户数据源下执行增删改查操作
|
|
|
+ * 支持多租户切换,当租户不存在时自动回退到主库
|
|
|
*/
|
|
|
@Slf4j
|
|
|
@Component
|
|
|
@@ -28,13 +29,13 @@ public class TenantDataSourceUtil {
|
|
|
/**
|
|
|
* 在指定租户数据源下执行操作(无返回值)
|
|
|
*
|
|
|
- * @param tenantId 租户ID
|
|
|
* @param action 要执行的操作
|
|
|
*/
|
|
|
public void execute(Long tenantId, Runnable action) {
|
|
|
TenantInfo tenantInfo = getTenantInfo(tenantId);
|
|
|
if (tenantInfo == null) {
|
|
|
- throw new IllegalArgumentException("租户不存在或已禁用,tenantId=" + tenantId);
|
|
|
+ action.run();
|
|
|
+ return;
|
|
|
}
|
|
|
execute(tenantInfo, action);
|
|
|
}
|
|
|
@@ -51,8 +52,6 @@ public class TenantDataSourceUtil {
|
|
|
tenantDataSourceManager.switchTenant(tenantInfo);
|
|
|
// 切换Redis租户上下文
|
|
|
RedisTenantContext.setTenantId(tenantInfo.getId());
|
|
|
- log.debug("[TenantDS] 已切换到租户数据源和Redis: tenantId={}, tenantCode={}",
|
|
|
- tenantInfo.getId(), tenantInfo.getTenantCode());
|
|
|
|
|
|
// 执行操作
|
|
|
action.run();
|
|
|
@@ -69,7 +68,6 @@ public class TenantDataSourceUtil {
|
|
|
/**
|
|
|
* 在指定租户数据源下执行操作(带返回值)
|
|
|
*
|
|
|
- * @param tenantId 租户ID
|
|
|
* @param action 要执行的操作
|
|
|
* @param <T> 返回值类型
|
|
|
* @return 操作结果
|
|
|
@@ -77,7 +75,7 @@ public class TenantDataSourceUtil {
|
|
|
public <T> T executeWithResult(Long tenantId, Supplier<T> action) {
|
|
|
TenantInfo tenantInfo = getTenantInfo(tenantId);
|
|
|
if (tenantInfo == null) {
|
|
|
- throw new IllegalArgumentException("租户不存在或已禁用,tenantId=" + tenantId);
|
|
|
+ return action.get();
|
|
|
}
|
|
|
return executeWithResult(tenantInfo, action);
|
|
|
}
|
|
|
@@ -96,8 +94,6 @@ public class TenantDataSourceUtil {
|
|
|
tenantDataSourceManager.switchTenant(tenantInfo);
|
|
|
// 切换Redis租户上下文
|
|
|
RedisTenantContext.setTenantId(tenantInfo.getId());
|
|
|
- log.debug("[TenantDS] 已切换到租户数据源和Redis: tenantId={}, tenantCode={}",
|
|
|
- tenantInfo.getId(), tenantInfo.getTenantCode());
|
|
|
|
|
|
// 执行操作并返回结果
|
|
|
return action.get();
|
|
|
@@ -113,14 +109,16 @@ public class TenantDataSourceUtil {
|
|
|
|
|
|
/**
|
|
|
* 在指定租户数据源下执行操作(消费租户信息,无返回值)
|
|
|
+ * 如果租户ID为空或租户不存在/已禁用,则自动回退到主库执行
|
|
|
*
|
|
|
- * @param tenantId 租户ID
|
|
|
+ * @param tenantId 租户ID(可为null,为null时走主库)
|
|
|
* @param action 要执行的操作,接收租户信息作为参数
|
|
|
*/
|
|
|
public void executeWithTenant(Long tenantId, Consumer<TenantInfo> action) {
|
|
|
TenantInfo tenantInfo = getTenantInfo(tenantId);
|
|
|
if (tenantInfo == null) {
|
|
|
- throw new IllegalArgumentException("租户不存在或已禁用,tenantId=" + tenantId);
|
|
|
+ action.accept(null);
|
|
|
+ return;
|
|
|
}
|
|
|
executeWithTenant(tenantInfo, action);
|
|
|
}
|
|
|
@@ -137,8 +135,6 @@ public class TenantDataSourceUtil {
|
|
|
tenantDataSourceManager.switchTenant(tenantInfo);
|
|
|
// 切换Redis租户上下文
|
|
|
RedisTenantContext.setTenantId(tenantInfo.getId());
|
|
|
- log.debug("[TenantDS] 已切换到租户数据源和Redis: tenantId={}, tenantCode={}",
|
|
|
- tenantInfo.getId(), tenantInfo.getTenantCode());
|
|
|
|
|
|
// 执行操作
|
|
|
action.accept(tenantInfo);
|
|
|
@@ -155,7 +151,6 @@ public class TenantDataSourceUtil {
|
|
|
/**
|
|
|
* 在指定租户数据源下执行操作(消费租户信息,带返回值)
|
|
|
*
|
|
|
- * @param tenantId 租户ID
|
|
|
* @param action 要执行的操作,接收租户信息并返回结果
|
|
|
* @param <R> 返回值类型
|
|
|
* @return 操作结果
|
|
|
@@ -163,7 +158,7 @@ public class TenantDataSourceUtil {
|
|
|
public <R> R executeWithTenantAndResult(Long tenantId, Function<TenantInfo, R> action) {
|
|
|
TenantInfo tenantInfo = getTenantInfo(tenantId);
|
|
|
if (tenantInfo == null) {
|
|
|
- throw new IllegalArgumentException("租户不存在或已禁用,tenantId=" + tenantId);
|
|
|
+ return action.apply(null);
|
|
|
}
|
|
|
return executeWithTenantAndResult(tenantInfo, action);
|
|
|
}
|
|
|
@@ -182,8 +177,6 @@ public class TenantDataSourceUtil {
|
|
|
tenantDataSourceManager.switchTenant(tenantInfo);
|
|
|
// 切换Redis租户上下文
|
|
|
RedisTenantContext.setTenantId(tenantInfo.getId());
|
|
|
- log.debug("[TenantDS] 已切换到租户数据源和Redis: tenantId={}, tenantCode={}",
|
|
|
- tenantInfo.getId(), tenantInfo.getTenantCode());
|
|
|
|
|
|
// 执行操作并返回结果
|
|
|
return action.apply(tenantInfo);
|
|
|
@@ -204,15 +197,20 @@ public class TenantDataSourceUtil {
|
|
|
* @return 租户信息,不存在或已禁用则返回null
|
|
|
*/
|
|
|
private TenantInfo getTenantInfo(Long tenantId) {
|
|
|
- TenantInfo tenantInfo = tenantInfoService.getById(tenantId);
|
|
|
- if (tenantInfo == null) {
|
|
|
- log.warn("[TenantDS] 租户不存在,tenantId={}", tenantId);
|
|
|
- return null;
|
|
|
- }
|
|
|
- if (!Integer.valueOf(1).equals(tenantInfo.getStatus())) {
|
|
|
- log.warn("[TenantDS] 租户已禁用,tenantId={}", tenantId);
|
|
|
+ try {
|
|
|
+ TenantInfo tenantInfo = tenantInfoService.getById(tenantId);
|
|
|
+ if (tenantInfo == null) {
|
|
|
+ log.warn("[TenantDS] 租户不存在,tenantId={}", tenantId);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (!Integer.valueOf(1).equals(tenantInfo.getStatus())) {
|
|
|
+ log.warn("[TenantDS] 租户已禁用,tenantId={}", tenantId);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return tenantInfo;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("[TenantDS] 查询租户信息异常,tenantId={}", tenantId, e);
|
|
|
return null;
|
|
|
}
|
|
|
- return tenantInfo;
|
|
|
}
|
|
|
}
|