Skip to content

Commit

Permalink
feat: 支持同时连接多个mongo TencentBlueKing#2591
Browse files Browse the repository at this point in the history
  • Loading branch information
yaoxuwan committed Sep 24, 2024
1 parent 24e0bf7 commit 544c2f4
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@

package com.tencent.bkrepo.analyst.configuration

import com.mongodb.ConnectionString
import com.mongodb.MongoClientSettings
import com.mongodb.client.MongoClients
import com.tencent.bkrepo.analysis.executor.api.ExecutorClient
import com.tencent.bkrepo.analyst.dispatcher.SubtaskDispatcherFactory
import com.tencent.bkrepo.analyst.dispatcher.SubtaskPoller
Expand All @@ -42,10 +45,18 @@ import com.tencent.bkrepo.common.service.condition.ConditionalOnNotAssembly
import com.tencent.bkrepo.repository.api.OperateLogClient
import com.tencent.bkrepo.repository.api.ProjectUsageStatisticsClient
import org.springframework.beans.factory.ObjectProvider
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.boot.autoconfigure.mongo.MongoProperties
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.data.mongodb.MongoDatabaseFactory
import org.springframework.data.mongodb.SpringDataMongoDB
import org.springframework.data.mongodb.core.MongoTemplate
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory
import org.springframework.data.mongodb.core.convert.MappingMongoConverter
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor
import java.util.function.Consumer

Expand Down Expand Up @@ -97,4 +108,36 @@ class ScannerConfiguration {
analystScanEventConsumer.accept(it)
}
}

@Bean
@ConfigurationProperties("spring.data.mongodb.analyst")
fun analystMongoProperties(): MongoProperties {
return MongoProperties()
}

@Bean
fun analystMongoTemplate(
mongoTemplate: MongoTemplate,
@Qualifier("analystMongoProperties") analystMongoProperties: MongoProperties,
converter: MappingMongoConverter?
): MongoTemplate? {
// 没有设置独立数据库时,使用统一的数据库
return if (analystMongoProperties.uri == MongoProperties.DEFAULT_URI) {
mongoTemplate
} else {
val settings =
MongoClientSettings.builder()
.applyConnectionString(ConnectionString(analystMongoProperties.determineUri()))
.uuidRepresentation(analystMongoProperties.uuidRepresentation)
.build()

val databaseFactory: MongoDatabaseFactory =
SimpleMongoClientDatabaseFactory(
MongoClients.create(settings, SpringDataMongoDB.driverInformation()),
analystMongoProperties.database
)

MongoTemplate(databaseFactory, converter)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,22 @@ package com.tencent.bkrepo.analyst.dao

import com.tencent.bkrepo.common.api.pojo.Page
import com.tencent.bkrepo.common.mongo.dao.simple.SimpleMongoDao
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.data.domain.PageRequest
import org.springframework.data.mongodb.core.MongoTemplate
import org.springframework.data.mongodb.core.query.Query

abstract class ScannerSimpleMongoDao<T> : SimpleMongoDao<T>() {

@Autowired
@Qualifier("analystMongoTemplate")
private lateinit var analystMongoTemplate: MongoTemplate

override fun determineMongoTemplate(): MongoTemplate {
return analystMongoTemplate
}

fun page(query: Query, pageRequest: PageRequest): Page<T> {
val count = count(query)
val records = find(query.with(pageRequest))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

package com.tencent.bkrepo.analyst.dao

import com.tencent.bkrepo.common.mongo.dao.simple.SimpleMongoDao
import com.tencent.bkrepo.analyst.model.TSpdxLicense
import com.tencent.bkrepo.common.api.util.EscapeUtils
import org.bson.BsonRegularExpression
Expand All @@ -37,7 +36,7 @@ import org.springframework.data.mongodb.core.query.isEqualTo
import org.springframework.stereotype.Repository

@Repository
class SpdxLicenseDao : SimpleMongoDao<TSpdxLicense>() {
class SpdxLicenseDao : ScannerSimpleMongoDao<TSpdxLicense>() {
fun findByLicenseId(licenseId: String): TSpdxLicense? {
return this.findOne(Query(TSpdxLicense::licenseId.isEqualTo(licenseId)))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.boot.autoconfigure.mongo.MongoProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Primary
import org.springframework.context.annotation.PropertySource
import org.springframework.data.mongodb.MongoDatabaseFactory
import org.springframework.data.mongodb.MongoTransactionManager
import org.springframework.data.mongodb.core.MongoTemplate
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver
import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper
import org.springframework.data.mongodb.core.convert.MappingMongoConverter
import org.springframework.data.mongodb.core.convert.MongoConverter
import org.springframework.data.mongodb.core.convert.MongoCustomConversions
import org.springframework.data.mongodb.core.mapping.MongoMappingContext

Expand Down Expand Up @@ -75,9 +78,21 @@ class MongoAutoConfiguration {
mappingContext.isAutoIndexCreation = mongoProperties.isAutoIndexCreation

val converter = MappingMongoConverter(dbRefResolver, mappingContext)
converter.typeMapper = DefaultMongoTypeMapper(null)
converter.setTypeMapper(DefaultMongoTypeMapper(null))
converter.afterPropertiesSet()
converter.setMapKeyDotReplacement("#dot#")
return converter
}

@Bean
@Primary
fun mongoProperties(): MongoProperties {
return MongoProperties()
}

@Bean
@Primary
fun mongoTemplate(factory: MongoDatabaseFactory, converter: MongoConverter?): MongoTemplate {
return MongoTemplate(factory, converter)
}
}

0 comments on commit 544c2f4

Please sign in to comment.