From afa8fc57084c423218c6816b710dfb2f0b87ea89 Mon Sep 17 00:00:00 2001
From: 潘志宝 <979469083@qq.com>
Date: 星期四, 14 十一月 2024 15:55:48 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'

---
 iailab-framework/iailab-common-biz-tenant/src/main/java/com/iailab/framework/tenant/core/service/TenantFrameworkServiceImpl.java |   37 +++++++++++++++++++++++++++++++++++++
 1 files changed, 37 insertions(+), 0 deletions(-)

diff --git a/iailab-framework/iailab-common-biz-tenant/src/main/java/com/iailab/framework/tenant/core/service/TenantFrameworkServiceImpl.java b/iailab-framework/iailab-common-biz-tenant/src/main/java/com/iailab/framework/tenant/core/service/TenantFrameworkServiceImpl.java
index fc3c9dc..eef1cf4 100644
--- a/iailab-framework/iailab-common-biz-tenant/src/main/java/com/iailab/framework/tenant/core/service/TenantFrameworkServiceImpl.java
+++ b/iailab-framework/iailab-common-biz-tenant/src/main/java/com/iailab/framework/tenant/core/service/TenantFrameworkServiceImpl.java
@@ -1,10 +1,13 @@
 package com.iailab.framework.tenant.core.service;
 
+import com.baomidou.dynamic.datasource.creator.DataSourceProperty;
 import com.iailab.framework.common.pojo.CommonResult;
 import com.iailab.framework.common.util.cache.CacheUtils;
 import com.iailab.module.system.api.tenant.TenantApi;
 import com.google.common.cache.CacheLoader;
 import com.google.common.cache.LoadingCache;
+
+import com.iailab.module.system.api.tenant.dto.TenantDataSourceConfigRespDTO;
 import lombok.RequiredArgsConstructor;
 import lombok.SneakyThrows;
 
@@ -51,6 +54,35 @@
 
             });
 
+    /**
+     * 针对 {@link #getDataSourceProperty(Long)} 的缓存
+     */
+    private final LoadingCache<Long, DataSourceProperty> dataSourcePropertyCache = CacheUtils.buildAsyncReloadingCache(
+            Duration.ofMinutes(1L), // 过期时间 1 分钟
+            new CacheLoader<Long, DataSourceProperty>() {
+
+                @Override
+                public DataSourceProperty load(Long id) {
+                    // 获得租户对应的数据源配置
+                    TenantDataSourceConfigRespDTO dataSourceConfig = tenantApi.getTenantDataSourceConfig(id);
+                    if (dataSourceConfig == null) {
+                        return null;
+                    }
+                    // 转换成 dynamic-datasource 配置
+//                    return new DataSourceProperty()
+//                            .setPoolName(dataSourceConfig.getName()).setUrl(dataSourceConfig.getUrl())
+//                            .setUsername(dataSourceConfig.getUsername()).setPassword(dataSourceConfig.getPassword());
+
+                    DataSourceProperty ds = new DataSourceProperty();
+                    ds.setPoolName(dataSourceConfig.getName());
+                    ds.setUrl(dataSourceConfig.getUrl());
+                    ds.setUsername(dataSourceConfig.getUsername());
+                    ds.setPassword(dataSourceConfig.getPassword());
+                    return ds;
+                }
+
+            });
+
     @Override
     @SneakyThrows
     public List<Long> getTenantIds() {
@@ -63,4 +95,9 @@
         validTenantCache.get(id).checkError();
     }
 
+    @Override
+    @SneakyThrows
+    public DataSourceProperty getDataSourceProperty(Long id) {
+        return dataSourcePropertyCache.get(id);
+    }
 }

--
Gitblit v1.9.3