Просмотр исходного кода

1、切换数据源,同步切换redis

yys 3 месяцев назад
Родитель
Сommit
37a6fb91a9

+ 35 - 0
fs-common/src/main/java/com/fs/common/config/RedisTenantContext.java

@@ -0,0 +1,35 @@
+package com.fs.common.config;
+
+/**
+ * Redis租户上下文管理类
+ * 用于在多租户环境下管理Redis的租户上下文
+ */
+public class RedisTenantContext {
+
+    private static final ThreadLocal<Long> TENANT_ID_CONTEXT = new ThreadLocal<>();
+
+    /**
+     * 设置当前线程的租户ID
+     *
+     * @param tenantId 租户ID
+     */
+    public static void setTenantId(Long tenantId) {
+        TENANT_ID_CONTEXT.set(tenantId);
+    }
+
+    /**
+     * 获取当前线程的租户ID
+     *
+     * @return 租户ID
+     */
+    public static Long getTenantId() {
+        return TENANT_ID_CONTEXT.get();
+    }
+
+    /**
+     * 清理当前线程的租户ID
+     */
+    public static void clear() {
+        TENANT_ID_CONTEXT.remove();
+    }
+}

+ 6 - 1
fs-common/src/main/java/com/fs/common/config/TenantKeyRedisSerializer.java

@@ -31,7 +31,12 @@ public class TenantKeyRedisSerializer extends StringRedisSerializer {
 
         Long tenantId = null;
         try {
-            tenantId = getTenantId();
+            // 优先从Redis租户上下文获取
+            tenantId = RedisTenantContext.getTenantId();
+            // 如果上下文没有,则从SecurityUtils获取
+            if (ObjectUtil.isEmpty(tenantId)) {
+                tenantId = getTenantId();
+            }
         } catch (Exception e) {
             tenantId = null;
         }

+ 25 - 8
fs-qw-api/src/main/java/com/fs/framework/datasource/TenantDataSourceUtil.java

@@ -1,5 +1,6 @@
 package com.fs.framework.datasource;
 
+import com.fs.common.config.RedisTenantContext;
 import com.fs.tenant.domain.TenantInfo;
 import com.fs.tenant.service.TenantInfoService;
 import lombok.extern.slf4j.Slf4j;
@@ -48,7 +49,9 @@ public class TenantDataSourceUtil {
         try {
             // 切换到租户数据源
             tenantDataSourceManager.switchTenant(tenantInfo);
-            log.debug("[TenantDS] 已切换到租户数据源: tenantId={}, tenantCode={}",
+            // 切换Redis租户上下文
+            RedisTenantContext.setTenantId(tenantInfo.getId());
+            log.debug("[TenantDS] 已切换到租户数据源和Redis: tenantId={}, tenantCode={}",
                     tenantInfo.getId(), tenantInfo.getTenantCode());
 
             // 执行操作
@@ -57,7 +60,9 @@ public class TenantDataSourceUtil {
         } finally {
             // 清理数据源上下文
             tenantDataSourceManager.clear();
-            log.debug("[TenantDS] 已清理租户数据源上下文");
+            // 清理Redis租户上下文
+            RedisTenantContext.clear();
+            log.debug("[TenantDS] 已清理租户数据源和Redis上下文");
         }
     }
 
@@ -89,7 +94,9 @@ public class TenantDataSourceUtil {
         try {
             // 切换到租户数据源
             tenantDataSourceManager.switchTenant(tenantInfo);
-            log.debug("[TenantDS] 已切换到租户数据源: tenantId={}, tenantCode={}",
+            // 切换Redis租户上下文
+            RedisTenantContext.setTenantId(tenantInfo.getId());
+            log.debug("[TenantDS] 已切换到租户数据源和Redis: tenantId={}, tenantCode={}",
                     tenantInfo.getId(), tenantInfo.getTenantCode());
 
             // 执行操作并返回结果
@@ -98,7 +105,9 @@ public class TenantDataSourceUtil {
         } finally {
             // 清理数据源上下文
             tenantDataSourceManager.clear();
-            log.debug("[TenantDS] 已清理租户数据源上下文");
+            // 清理Redis租户上下文
+            RedisTenantContext.clear();
+            log.debug("[TenantDS] 已清理租户数据源和Redis上下文");
         }
     }
 
@@ -126,7 +135,9 @@ public class TenantDataSourceUtil {
         try {
             // 切换到租户数据源
             tenantDataSourceManager.switchTenant(tenantInfo);
-            log.debug("[TenantDS] 已切换到租户数据源: tenantId={}, tenantCode={}",
+            // 切换Redis租户上下文
+            RedisTenantContext.setTenantId(tenantInfo.getId());
+            log.debug("[TenantDS] 已切换到租户数据源和Redis: tenantId={}, tenantCode={}",
                     tenantInfo.getId(), tenantInfo.getTenantCode());
 
             // 执行操作
@@ -135,7 +146,9 @@ public class TenantDataSourceUtil {
         } finally {
             // 清理数据源上下文
             tenantDataSourceManager.clear();
-            log.debug("[TenantDS] 已清理租户数据源上下文");
+            // 清理Redis租户上下文
+            RedisTenantContext.clear();
+            log.debug("[TenantDS] 已清理租户数据源和Redis上下文");
         }
     }
 
@@ -167,7 +180,9 @@ public class TenantDataSourceUtil {
         try {
             // 切换到租户数据源
             tenantDataSourceManager.switchTenant(tenantInfo);
-            log.debug("[TenantDS] 已切换到租户数据源: tenantId={}, tenantCode={}",
+            // 切换Redis租户上下文
+            RedisTenantContext.setTenantId(tenantInfo.getId());
+            log.debug("[TenantDS] 已切换到租户数据源和Redis: tenantId={}, tenantCode={}",
                     tenantInfo.getId(), tenantInfo.getTenantCode());
 
             // 执行操作并返回结果
@@ -176,7 +191,9 @@ public class TenantDataSourceUtil {
         } finally {
             // 清理数据源上下文
             tenantDataSourceManager.clear();
-            log.debug("[TenantDS] 已清理租户数据源上下文");
+            // 清理Redis租户上下文
+            RedisTenantContext.clear();
+            log.debug("[TenantDS] 已清理租户数据源和Redis上下文");
         }
     }