diff --git a/src/backend/ci/core/common/common-db-sharding/src/main/kotlin/com/tencent/devops/common/db/config/BkShardingDataSourceConfiguration.kt b/src/backend/ci/core/common/common-db-sharding/src/main/kotlin/com/tencent/devops/common/db/config/BkShardingDataSourceConfiguration.kt index b92f504976e2..e8019a55f82a 100644 --- a/src/backend/ci/core/common/common-db-sharding/src/main/kotlin/com/tencent/devops/common/db/config/BkShardingDataSourceConfiguration.kt +++ b/src/backend/ci/core/common/common-db-sharding/src/main/kotlin/com/tencent/devops/common/db/config/BkShardingDataSourceConfiguration.kt @@ -43,6 +43,8 @@ import com.tencent.devops.common.db.pojo.TableShardingStrategyEnum import com.zaxxer.hikari.HikariDataSource import com.zaxxer.hikari.metrics.micrometer.MicrometerMetricsTrackerFactory import io.micrometer.core.instrument.MeterRegistry +import java.util.Properties +import javax.sql.DataSource import org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration @@ -62,8 +64,6 @@ import org.springframework.context.annotation.Configuration import org.springframework.context.annotation.Primary import org.springframework.core.Ordered import org.springframework.transaction.annotation.EnableTransactionManagement -import java.util.Properties -import javax.sql.DataSource @Suppress("LongParameterList", "MagicNumber", "ComplexMethod") @Configuration @@ -122,6 +122,9 @@ class BkShardingDataSourceConfiguration { @Value("\${sharding.tableShardingStrategy.defaultShardingNum:#{5}}") private val defaultTableShardingNum: Int = 5 + @Value("\${spring.datasource.connectionTestQuery:select 1;}") + private lateinit var dataSourceConnectionTestQuery: String + private fun dataSourceMap( dataSourcePrefixName: String, dataSourceConfigs: List, @@ -165,6 +168,7 @@ class BkShardingDataSourceConfiguration { connectionInitSql = datasourceInitSql leakDetectionThreshold = datasourceLeakDetectionThreshold metricsTrackerFactory = MicrometerMetricsTrackerFactory(metricsRegistry) + connectionTestQuery = dataSourceConnectionTestQuery } } diff --git a/src/backend/ci/core/common/common-db/src/main/kotlin/com/tencent/devops/common/db/DBAutoConfiguration.kt b/src/backend/ci/core/common/common-db/src/main/kotlin/com/tencent/devops/common/db/DBAutoConfiguration.kt index a18cd456a844..49f900611e5b 100644 --- a/src/backend/ci/core/common/common-db/src/main/kotlin/com/tencent/devops/common/db/DBAutoConfiguration.kt +++ b/src/backend/ci/core/common/common-db/src/main/kotlin/com/tencent/devops/common/db/DBAutoConfiguration.kt @@ -30,6 +30,7 @@ package com.tencent.devops.common.db import com.mysql.cj.jdbc.Driver import com.tencent.devops.common.db.config.DBBaseConfiguration import com.zaxxer.hikari.HikariDataSource +import javax.sql.DataSource import org.springframework.beans.factory.annotation.Value import org.springframework.boot.autoconfigure.AutoConfigureBefore import org.springframework.boot.autoconfigure.AutoConfigureOrder @@ -42,7 +43,6 @@ import org.springframework.context.annotation.Primary import org.springframework.context.annotation.PropertySource import org.springframework.core.Ordered import org.springframework.transaction.annotation.EnableTransactionManagement -import javax.sql.DataSource /** * @@ -58,21 +58,31 @@ class DBAutoConfiguration { @Value("\${spring.datasource.url:#{null}}") private val datasourceUrl: String? = null + @Value("\${spring.datasource.username:#{null}}") private val datasourceUsername: String? = null + @Value("\${spring.datasource.password:#{null}}") private val datasourcePassword: String? = null + @Value("\${spring.datasource.initSql:#{null}}") private val datasourceInitSql: String? = null + @Value("\${spring.datasource.leakDetectionThreshold:#{0}}") private val datasouceLeakDetectionThreshold: Long = 0 + @Value("\${spring.datasource.minimumIdle:#{1}}") private val datasourceMinimumIdle: Int = 1 + @Value("\${spring.datasource.maximumPoolSize:#{50}}") private val datasourceMaximumPoolSize: Int = 50 + @Value("\${spring.datasource.idleTimeout:#{60000}}") private val datasourceIdleTimeout: Long = 60000 + @Value("\${spring.datasource.connectionTestQuery:select 1;}") + private lateinit var dataSourceConnectionTestQuery: String + @Bean @Primary fun dataSource(): DataSource { @@ -90,6 +100,7 @@ class DBAutoConfiguration { idleTimeout = datasourceIdleTimeout connectionInitSql = datasourceInitSql leakDetectionThreshold = datasouceLeakDetectionThreshold + connectionTestQuery = dataSourceConnectionTestQuery } } } diff --git a/src/backend/ci/core/common/common-service/src/main/resources/common-service.properties b/src/backend/ci/core/common/common-service/src/main/resources/common-service.properties index 33291f28133f..7da63d49dcca 100644 --- a/src/backend/ci/core/common/common-service/src/main/resources/common-service.properties +++ b/src/backend/ci/core/common/common-service/src/main/resources/common-service.properties @@ -29,11 +29,5 @@ spring.jmx.enabled=true management.endpoints.web.exposure.include=* management.health.elasticsearch.enabled=false management.endpoint.health.show-details=ALWAYS -management.security.enabled=false management.endpoints.web.base-path=/management -management.metrics.tags.application=${spring.application.name} -spring.cloud.consul.discovery.health-check-path=/management/health -spring.cloud.consul.discovery.heartbeat.enabled=true -spring.cloud.consul.discovery.query-passing=true -spring.cloud.consul.discovery.preferIpAddress=true -spring.cloud.kubernetes.discovery.all-namespaces=true +management.metrics.tags.application=${spring.application.name} \ No newline at end of file diff --git a/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/actuate/ProcessHealthIndicator.kt b/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/actuate/ProcessHealthIndicator.kt new file mode 100644 index 000000000000..602bdc886934 --- /dev/null +++ b/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/actuate/ProcessHealthIndicator.kt @@ -0,0 +1,30 @@ +package com.tencent.devops.process.actuate + +import com.tencent.devops.process.dao.PipelineFavorDao +import org.jooq.DSLContext +import org.slf4j.LoggerFactory +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.actuate.health.AbstractHealthIndicator +import org.springframework.boot.actuate.health.Health +import org.springframework.stereotype.Component + +@Component +class ProcessHealthIndicator @Autowired constructor( + private val pipelineFavorDao: PipelineFavorDao, + private val dslContext: DSLContext +) : AbstractHealthIndicator() { + @SuppressWarnings("SwallowedException", "TooGenericExceptionCaught") + override fun doHealthCheck(builder: Health.Builder) { + try { + val maxId = pipelineFavorDao.getMaxId(dslContext) + builder.up().withDetail("MaxId", maxId) + } catch (e: Exception) { + logger.error("Get max id failed") + builder.down() + } + } + + companion object { + private val logger = LoggerFactory.getLogger(ProcessHealthIndicator::class.java) + } +} \ No newline at end of file diff --git a/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/dao/PipelineFavorDao.kt b/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/dao/PipelineFavorDao.kt index 61d014d3022f..0797ef1f126a 100644 --- a/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/dao/PipelineFavorDao.kt +++ b/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/dao/PipelineFavorDao.kt @@ -29,11 +29,12 @@ package com.tencent.devops.process.dao import com.tencent.devops.model.process.tables.TPipelineFavor import com.tencent.devops.model.process.tables.records.TPipelineFavorRecord +import java.time.LocalDateTime import org.jooq.DSLContext import org.jooq.Result +import org.jooq.impl.DSL import org.slf4j.LoggerFactory import org.springframework.stereotype.Repository -import java.time.LocalDateTime /** * 用户收藏流水线 @@ -138,6 +139,14 @@ class PipelineFavorDao { } } + fun getMaxId( + dslContext: DSLContext + ): Long { + with(TPipelineFavor.T_PIPELINE_FAVOR) { + return dslContext.select(DSL.max(ID)).from(this).fetchOne(0, Long::class.java)!! + } + } + companion object { private val logger = LoggerFactory.getLogger(PipelineFavorDao::class.java) }