From c39abccd937de093fc067abffac5f66b758bc97b Mon Sep 17 00:00:00 2001 From: 潘志宝 <979469083@qq.com> Date: 星期三, 21 八月 2024 15:45:44 +0800 Subject: [PATCH] TenantDS --- iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/dal/dataobject/tenant/TenantDO.java | 8 ++++ iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/convert/tenant/TenantConvert.java | 4 ++ iailab-module-system/iailab-module-system-api/src/main/java/com/iailab/module/system/api/tenant/dto/TenantDataSourceConfigRespDTO.java | 31 +++++++++++++++ iailab-module-system/iailab-module-system-biz/db/mysql.sql | 1 iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/framework/rpc/config/RpcConfiguration.java | 3 + iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/api/tenant/TenantApiImpl.java | 18 +++++++++ iailab-module-system/iailab-module-system-api/src/main/java/com/iailab/module/system/api/tenant/TenantApi.java | 6 +++ 7 files changed, 70 insertions(+), 1 deletions(-) diff --git a/iailab-module-system/iailab-module-system-api/src/main/java/com/iailab/module/system/api/tenant/TenantApi.java b/iailab-module-system/iailab-module-system-api/src/main/java/com/iailab/module/system/api/tenant/TenantApi.java index 5348ee5..90cc16c 100644 --- a/iailab-module-system/iailab-module-system-api/src/main/java/com/iailab/module/system/api/tenant/TenantApi.java +++ b/iailab-module-system/iailab-module-system-api/src/main/java/com/iailab/module/system/api/tenant/TenantApi.java @@ -1,6 +1,7 @@ package com.iailab.module.system.api.tenant; import com.iailab.framework.common.pojo.CommonResult; +import com.iailab.module.system.api.tenant.dto.TenantDataSourceConfigRespDTO; import com.iailab.module.system.enums.ApiConstants; import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.Parameter; @@ -26,4 +27,9 @@ @Parameter(name = "id", description = "租户编号", required = true, example = "1024") CommonResult<Boolean> validTenant(@RequestParam("id") Long id); + @GetMapping(PREFIX + "/data-source") + @Operation(summary = "获得租户的数据源配置") + @Parameter(name = "tenantId", description = "租户编号", required = true, example = "1024") + TenantDataSourceConfigRespDTO getTenantDataSourceConfig(@RequestParam("tenantId") Long tenantId); + } diff --git a/iailab-module-system/iailab-module-system-api/src/main/java/com/iailab/module/system/api/tenant/dto/TenantDataSourceConfigRespDTO.java b/iailab-module-system/iailab-module-system-api/src/main/java/com/iailab/module/system/api/tenant/dto/TenantDataSourceConfigRespDTO.java new file mode 100644 index 0000000..dc04670 --- /dev/null +++ b/iailab-module-system/iailab-module-system-api/src/main/java/com/iailab/module/system/api/tenant/dto/TenantDataSourceConfigRespDTO.java @@ -0,0 +1,31 @@ +package com.iailab.module.system.api.tenant.dto; + +import lombok.Data; + +/** + * 多租户的数据源配置 Response DTO + * + * @author 芋道源码 + */ +@Data +public class TenantDataSourceConfigRespDTO { + + /** + * 连接名 + */ + private String name; + + /** + * 数据源连接 + */ + private String url; + /** + * 用户名 + */ + private String username; + /** + * 密码 + */ + private String password; + +} diff --git a/iailab-module-system/iailab-module-system-biz/db/mysql.sql b/iailab-module-system/iailab-module-system-biz/db/mysql.sql index 103881a..9fb18f5 100644 --- a/iailab-module-system/iailab-module-system-biz/db/mysql.sql +++ b/iailab-module-system/iailab-module-system-biz/db/mysql.sql @@ -16,6 +16,7 @@ `package_id` bigint NOT NULL COMMENT '租户套餐编号', `expire_time` datetime NOT NULL COMMENT '过期时间', `account_count` int NOT NULL COMMENT '账号数量', + `data_source_config_id` bigint DEFAULT NULL COMMENT '数据源配置的编号', `creator` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '创建者', `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `updater` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '更新者', diff --git a/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/api/tenant/TenantApiImpl.java b/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/api/tenant/TenantApiImpl.java index 9dd9f65..e03959e 100644 --- a/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/api/tenant/TenantApiImpl.java +++ b/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/api/tenant/TenantApiImpl.java @@ -1,6 +1,10 @@ package com.iailab.module.system.api.tenant; import com.iailab.framework.common.pojo.CommonResult; +import com.iailab.module.infra.api.db.DataSourceConfigServiceApi; +import com.iailab.module.system.api.tenant.dto.TenantDataSourceConfigRespDTO; +import com.iailab.module.system.convert.tenant.TenantConvert; +import com.iailab.module.system.dal.dataobject.tenant.TenantDO; import com.iailab.module.system.service.tenant.TenantService; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.RestController; @@ -17,6 +21,9 @@ @Resource private TenantService tenantService; + @Resource + private DataSourceConfigServiceApi dataSourceConfigServiceApi; + @Override public CommonResult<List<Long>> getTenantIdList() { return success(tenantService.getTenantIdList()); @@ -28,4 +35,15 @@ return success(true); } + @Override + public TenantDataSourceConfigRespDTO getTenantDataSourceConfig(Long tenantId) { + // 获得租户信息 + TenantDO tenant = tenantService.getTenant(tenantId); + if (tenant == null) { + return null; + } + // 获得租户的数据源配置 + return TenantConvert.INSTANCE.convert( + dataSourceConfigServiceApi.getDataSourceConfig(tenant.getDataSourceConfigId())); + } } diff --git a/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/convert/tenant/TenantConvert.java b/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/convert/tenant/TenantConvert.java index f0f7a52..6791518 100644 --- a/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/convert/tenant/TenantConvert.java +++ b/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/convert/tenant/TenantConvert.java @@ -1,5 +1,7 @@ package com.iailab.module.system.convert.tenant; +import com.iailab.module.infra.api.db.dto.DataSourceConfigRespDTO; +import com.iailab.module.system.api.tenant.dto.TenantDataSourceConfigRespDTO; import com.iailab.module.system.controller.admin.tenant.vo.tenant.TenantSaveReqVO; import com.iailab.module.system.controller.admin.user.vo.user.UserSaveReqVO; import org.mapstruct.Mapper; @@ -23,4 +25,6 @@ return reqVO; } + TenantDataSourceConfigRespDTO convert(DataSourceConfigRespDTO bean); + } diff --git a/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/dal/dataobject/tenant/TenantDO.java b/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/dal/dataobject/tenant/TenantDO.java index 0f670b1..a561ea6 100644 --- a/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/dal/dataobject/tenant/TenantDO.java +++ b/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/dal/dataobject/tenant/TenantDO.java @@ -76,5 +76,13 @@ * 账号数量 */ private Integer accountCount; + /** + * 数据源配置编号 + * + * 多租户采用“分库”方案时,通过该字段配置所在数据源 + * + * 关联 DataSourceConfigDO 的 id 字段 + */ + private Long dataSourceConfigId; } diff --git a/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/framework/rpc/config/RpcConfiguration.java b/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/framework/rpc/config/RpcConfiguration.java index a534dc8..f6f8c09 100644 --- a/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/framework/rpc/config/RpcConfiguration.java +++ b/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/framework/rpc/config/RpcConfiguration.java @@ -1,12 +1,13 @@ package com.iailab.module.system.framework.rpc.config; import com.iailab.module.infra.api.config.ConfigApi; +import com.iailab.module.infra.api.db.DataSourceConfigServiceApi; import com.iailab.module.infra.api.file.FileApi; import com.iailab.module.infra.api.websocket.WebSocketSenderApi; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.Configuration; @Configuration(proxyBeanMethods = false) -@EnableFeignClients(clients = {FileApi.class, WebSocketSenderApi.class, ConfigApi.class}) +@EnableFeignClients(clients = {FileApi.class, WebSocketSenderApi.class, DataSourceConfigServiceApi.class, ConfigApi.class}) public class RpcConfiguration { } -- Gitblit v1.9.3