From 5ec8193d1a75ede9630cab0d115ea2661d95a065 Mon Sep 17 00:00:00 2001 From: carlyin <449036249@qq.com> Date: Tue, 21 May 2024 17:14:59 +0800 Subject: [PATCH] =?UTF-8?q?pref:=E8=8E=B7=E5=8F=96db=E9=9B=86=E7=BE=A4?= =?UTF-8?q?=E5=90=8D=E7=A7=B0=E6=96=B9=E6=B3=95=E6=94=AF=E6=8C=81db?= =?UTF-8?q?=E9=9B=86=E7=BE=A4=E5=88=97=E8=A1=A8=E5=AE=9E=E7=8E=B0=E5=8F=AF?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E5=8C=96=20#10372?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/service/utils/CommonUtils.kt | 44 +++++++++++++------ 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/src/backend/ci/core/common/common-service/src/main/kotlin/com/tencent/devops/common/service/utils/CommonUtils.kt b/src/backend/ci/core/common/common-service/src/main/kotlin/com/tencent/devops/common/service/utils/CommonUtils.kt index b2c6d7aaaa9..082e862d9ac 100755 --- a/src/backend/ci/core/common/common-service/src/main/kotlin/com/tencent/devops/common/service/utils/CommonUtils.kt +++ b/src/backend/ci/core/common/common-service/src/main/kotlin/com/tencent/devops/common/service/utils/CommonUtils.kt @@ -34,11 +34,9 @@ import com.tencent.devops.common.api.pojo.Result import com.tencent.devops.common.api.util.JsonUtil import com.tencent.devops.common.api.util.MessageUtil import com.tencent.devops.common.api.util.OkhttpUtils -import com.tencent.devops.common.service.PROFILE_AUTO import com.tencent.devops.common.service.PROFILE_DEFAULT import com.tencent.devops.common.service.PROFILE_DEVELOPMENT import com.tencent.devops.common.service.PROFILE_PRODUCTION -import com.tencent.devops.common.service.PROFILE_STREAM import com.tencent.devops.common.service.PROFILE_TEST import com.tencent.devops.common.service.Profile import org.apache.commons.lang3.StringUtils @@ -235,17 +233,7 @@ object CommonUtils { PROFILE_TEST } profile.isProd() -> { - when { - profile.isAuto() -> { - PROFILE_AUTO - } - profile.isStream() -> { - PROFILE_STREAM - } - else -> { - PROFILE_PRODUCTION - } - } + getProdDbClusterName(profile) } profile.isLocal() -> { PROFILE_DEFAULT @@ -256,6 +244,36 @@ object CommonUtils { } } + private fun getProdDbClusterName(profile: Profile): String { + // 从配置文件获取db集群名称列表 + val dbClusterNames = (SpringContextUtil.getValue("bk.db.clusterNames") ?: PROFILE_PRODUCTION).split(",") + val activeProfiles = profile.getActiveProfiles() + var finalDbClusterName = PROFILE_PRODUCTION + run breaking@{ + // 获取当前服务器集群对应的db集群名称 + activeProfiles.forEach { activeProfile -> + val dbClusterName = getDbClusterNameByProfile(dbClusterNames, activeProfile) + dbClusterName?.let { + finalDbClusterName = dbClusterName + return@breaking + } + } + } + return finalDbClusterName + } + + private fun getDbClusterNameByProfile( + dbClusterNames: List, + activeProfile: String + ): String? { + dbClusterNames.forEach { dbClusterName -> + if (activeProfile.contains(dbClusterName)) { + return dbClusterName + } + } + return null + } + /** * 获取jooq上下文对象 * @param archiveFlag 归档标识