Skip to content

Commit

Permalink
feat:蓝盾本地开发实现 TencentBlueKing#9576
Browse files Browse the repository at this point in the history
  • Loading branch information
fcfang123 committed Oct 18, 2023
1 parent 0fbcc39 commit 46fe034
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,16 @@ plugins {
val toImageRepo = System.getProperty("to.image.repo")
val toImageTag = System.getProperty("to.image.tag")
var toImage = System.getProperty("jib.to.image")
val serviceName = System.getProperty("serviceName")


// 加这个判断 , 主要是为了编译kts时不报错
if (toImage.isNullOrBlank() || (toImageRepo.isNullOrBlank() && toImageTag.isNullOrBlank())) {
val service = serviceName.takeUnless { it.isNullOrBlank() }
?: name.replace("boot-", "").replace("-tencent", "")
val service = name.replace("boot-", "").replace("-tencent", "")
if (toImage.isNullOrBlank() && !toImageRepo.isNullOrBlank()) {
toImage = toImageRepo.let {
if (toImageRepo.endsWith("/")) it else it + "/"
} + "bkci-" + service + ":" + toImageTag
}
val configNamespace = System.getProperty("config.namespace")
val jvmFlagList = System.getProperty("jvmFlags.file")?.let { File(it).readLines() } ?: emptyList()
val multiModelService = System.getProperty("devops.multi.from")
val multiDataSource = System.getProperty("devops.multi.datasource")

val finalJvmFlags = mutableListOf(
"-server",
Expand Down Expand Up @@ -82,14 +76,7 @@ if (toImage.isNullOrBlank() || (toImageRepo.isNullOrBlank() && toImageTag.isNull
"-Dspring.cloud.kubernetes.config.includeProfileSpecificSources=false",
"-Dio.undertow.legacy.cookie.ALLOW_HTTP_SEPARATORS_IN_V0=true",
"-Dserver.port=80"
).also {
if (multiModelService != null) {
it.add("-Ddevops.multi.from=$multiModelService")
}
if (multiDataSource != null) {
it.add("-Ddevops.multi.datasource=$multiDataSource")
}
}
)

finalJvmFlags.addAll(jvmFlagList)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
tasks.register("multiBootJar") {
System.getProperty("devops.multi.from")?.let { multiModuleStr ->
val finalServices = System.getProperty("devops.multi.from") ?: localRunMultiServices
finalServices.let { multiModuleStr ->
val multiModuleList = multiModuleStr.split(",").toMutableList()
rootProject.subprojects.filter {
isSpecifiedModulePath(it.path, multiModuleList)
Expand All @@ -50,3 +51,7 @@ fun addDependencies(path: String) {
add("implementation", project(path))
}
}

val localRunMultiServices = "process,auth,image,environment,repository,ticket,project," +
"notify,openapi,quality,dispatch,dispatch-docker,dispatch-kubernetes,artifactory," +
"monitoring,plugin,websocket,worker,misc,store,log"
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import org.springframework.boot.gradle.tasks.run.BootRun

tasks.register<BootRun>("multiBootRun") {
doFirst {
systemProperty("devops.multi.from", System.getProperty("devops.multi.from"))
systemProperty("devops.multi.from", localRunMultiServices)
systemProperty("spring.main.allow-circular-references", "true")
systemProperty("spring.cloud.config.enabled", "false")
systemProperty("spring.cloud.config.fail-fast", "true")
Expand All @@ -53,3 +53,7 @@ tasks.register<BootRun>("multiBootRun") {
*/
fun joinPath(vararg folders: String) = folders.joinToString(File.separator)
tasks.getByName("compileKotlin").dependsOn("replacePlaceholders")

val localRunMultiServices = "process,auth,image,environment,repository,ticket,project," +
"notify,openapi,quality,dispatch,dispatch-docker,dispatch-kubernetes,artifactory," +
"monitoring,plugin,websocket,worker,misc,store,log"
Original file line number Diff line number Diff line change
Expand Up @@ -28,72 +28,75 @@ class BkServiceInstanceApplicationRunner constructor(

@Suppress("SpreadOperator")
override fun run(args: ApplicationArguments) {
object : Thread() {
override fun run() {
val serviceName = BkServiceUtil.findServiceName()
logger.info("initServiceHostInfo serviceName:$serviceName begin")
val cacheKey = BkServiceUtil.getServiceHostKey(serviceName)
logger.info("initServiceHostInfo serviceName:$serviceName cacheKey:$cacheKey")
val discoveryTag = bkTag.getFinalTag()
val namespace = discoveryTag.replace("kubernetes-", "")
val svrName = KubernetesUtils.getSvrName(serviceName, namespace)
// 睡眠一会儿以便从注册最新拿到微服务最新的IP列表
sleep(THREAD_SLEEP_TIMEOUT)
val serviceHosts = compositeDiscoveryClient.getInstances(svrName).map { it.host }.toTypedArray()
logger.info(
"initServiceHostInfo serviceName:[$serviceName],IP:[${CommonUtils.getInnerIP()}],serviceHosts:${
JsonUtil.toJson(
serviceHosts
)
}"
)
val environment: Environment = SpringContextUtil.getBean(Environment::class.java)
val shardingDbFlag = environment.containsProperty("spring.datasource.dataSourceConfigs[0].url")
logger.info("initServiceHostInfo serviceName:$serviceName shardingDbFlag:$shardingDbFlag")
if (shardingDbFlag) {
// 如果微服务的DB采用了分库分表方案,那么需要清理规则动态队列
try {
deleteDynamicMqQueue(cacheKey, serviceName, serviceHosts)
} catch (ignored: Throwable) {
logger.warn(
"serviceName:$serviceName delete dynamicMqQueue fail!", ignored
)
// 当本地运行时为单体服务,不需要存储各服务的实例ip
if (!System.getProperty("local.run").toBoolean()) {
object : Thread() {
override fun run() {
val serviceName = BkServiceUtil.findServiceName()
logger.info("initServiceHostInfo serviceName:$serviceName begin")
val cacheKey = BkServiceUtil.getServiceHostKey(serviceName)
logger.info("initServiceHostInfo serviceName:$serviceName cacheKey:$cacheKey")
val discoveryTag = bkTag.getFinalTag()
val namespace = discoveryTag.replace("kubernetes-", "")
val svrName = KubernetesUtils.getSvrName(serviceName, namespace)
// 睡眠一会儿以便从注册最新拿到微服务最新的IP列表
sleep(THREAD_SLEEP_TIMEOUT)
val serviceHosts = compositeDiscoveryClient.getInstances(svrName).map { it.host }.toTypedArray()
logger.info(
"initServiceHostInfo serviceName:[$serviceName],IP:[${CommonUtils.getInnerIP()}],serviceHosts:${
JsonUtil.toJson(
serviceHosts
)
}"
)
val environment: Environment = SpringContextUtil.getBean(Environment::class.java)
val shardingDbFlag = environment.containsProperty("spring.datasource.dataSourceConfigs[0].url")
logger.info("initServiceHostInfo serviceName:$serviceName shardingDbFlag:$shardingDbFlag")
if (shardingDbFlag) {
// 如果微服务的DB采用了分库分表方案,那么需要清理规则动态队列
try {
deleteDynamicMqQueue(cacheKey, serviceName, serviceHosts)
} catch (ignored: Throwable) {
logger.warn(
"serviceName:$serviceName delete dynamicMqQueue fail!", ignored
)
}
}
// 清空redis中微服务的主机IP列表
redisOperation.delete(cacheKey)
// 把微服务的最新主机IP列表写入redis中
redisOperation.sadd(cacheKey, *serviceHosts)
}
// 清空redis中微服务的主机IP列表
redisOperation.delete(cacheKey)
// 把微服务的最新主机IP列表写入redis中
redisOperation.sadd(cacheKey, *serviceHosts)
}

private fun deleteDynamicMqQueue(cacheKey: String, serviceName: String, serviceHosts: Array<String>) {
// 将微服务在redis的主机IP列表取出
val historyServiceHosts = redisOperation.getSetMembers(cacheKey)?.toMutableSet()
logger.info("initServiceHostInfo serviceName:$serviceName historyServiceHosts:$historyServiceHosts")
if (historyServiceHosts.isNullOrEmpty()) {
return
}
historyServiceHosts.removeAll(serviceHosts.toSet())
historyServiceHosts.forEach { historyServiceHost ->
val queueName = BkServiceUtil.getDynamicMqQueue(serviceName, historyServiceHost)
logger.info("serviceName:$serviceName delete dynamicMqQueue($queueName) start!")
var queueProperties = rabbitAdmin.getQueueProperties(queueName)
if (queueProperties == null) {
// 队列属性为空说明删除成功
logger.info("serviceName:$serviceName dynamicMqQueue($queueName) does not exist!")
private fun deleteDynamicMqQueue(cacheKey: String, serviceName: String, serviceHosts: Array<String>) {
// 将微服务在redis的主机IP列表取出
val historyServiceHosts = redisOperation.getSetMembers(cacheKey)?.toMutableSet()
logger.info("initServiceHostInfo serviceName:$serviceName historyServiceHosts:$historyServiceHosts")
if (historyServiceHosts.isNullOrEmpty()) {
return
}
rabbitAdmin.purgeQueue(queueName)
rabbitAdmin.deleteQueue(queueName)
queueProperties = rabbitAdmin.getQueueProperties(queueName)
if (queueProperties != null) {
// 队列属性不为空说明删除未成功,把队列名称写入redis中
logger.info("serviceName:$serviceName delete dynamicMqQueue($queueName) fail!")
} else {
logger.info("serviceName:$serviceName delete dynamicMqQueue($queueName) success!")
historyServiceHosts.removeAll(serviceHosts.toSet())
historyServiceHosts.forEach { historyServiceHost ->
val queueName = BkServiceUtil.getDynamicMqQueue(serviceName, historyServiceHost)
logger.info("serviceName:$serviceName delete dynamicMqQueue($queueName) start!")
var queueProperties = rabbitAdmin.getQueueProperties(queueName)
if (queueProperties == null) {
// 队列属性为空说明删除成功
logger.info("serviceName:$serviceName dynamicMqQueue($queueName) does not exist!")
return
}
rabbitAdmin.purgeQueue(queueName)
rabbitAdmin.deleteQueue(queueName)
queueProperties = rabbitAdmin.getQueueProperties(queueName)
if (queueProperties != null) {
// 队列属性不为空说明删除未成功,把队列名称写入redis中
logger.info("serviceName:$serviceName delete dynamicMqQueue($queueName) fail!")
} else {
logger.info("serviceName:$serviceName delete dynamicMqQueue($queueName) success!")
}
}
}
}
}.start()
}.start()
}
}
}
4 changes: 0 additions & 4 deletions src/backend/ci/core/multijar/boot-multijar/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,4 @@ plugins {
`task-multi-boot-run`
}

tasks.named<BootJar>("bootJar") {
val finalModuleName = System.getProperty("devops.multi.to")
archiveBaseName.set("boot-$finalModuleName")
}
tasks.getByName("multiBootRun").dependsOn("replacePlaceholders")
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,13 @@ class DataSourceDefinitionRegistrar : ImportBeanDefinitionRegistrar {
}
}


companion object {
private val logger = LoggerFactory.getLogger(DataSourceDefinitionRegistrar::class.java)
private val notNeedDataSourceService = listOf(
"buildless", "dockerhost", "metrics", "misc", "monitoring", "process","worker","websocket"
)

private
val multiDataSource = System.getProperty("devops.multi.from")
private val multiDataSource = System.getProperty("devops.multi.from")
.split(",").filterNot { notNeedDataSourceService.contains(it) }
}
}

0 comments on commit 46fe034

Please sign in to comment.