Skip to content

Commit

Permalink
Merge pull request TencentBlueKing#10373 from carlyin0801/issue_10372…
Browse files Browse the repository at this point in the history
…_dbClusterName_config_fix

pref:获取db集群名称方法支持db集群列表实现可配置化 TencentBlueKing#10372
  • Loading branch information
bkci-bot authored Jun 3, 2024
2 parents fed302f + 5ec8193 commit 0289a09
Showing 1 changed file with 31 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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<String>,
activeProfile: String
): String? {
dbClusterNames.forEach { dbClusterName ->
if (activeProfile.contains(dbClusterName)) {
return dbClusterName
}
}
return null
}

/**
* 获取jooq上下文对象
* @param archiveFlag 归档标识
Expand Down

0 comments on commit 0289a09

Please sign in to comment.