Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【Enhancement】Turbo编译加速缓存的client实例信息新增过期剔除功能 #168 #171

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ dependencies {
api("io.github.openfeign:feign-jaxrs")
api("io.github.openfeign:feign-okhttp")
api("io.github.openfeign:feign-jackson")
api("com.google.guava:guava")
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,25 @@

package com.tencent.devops.common.client.ms

import com.google.common.cache.Cache
import com.google.common.cache.CacheBuilder
import feign.Request
import feign.RequestTemplate
import feign.Target
import org.springframework.cloud.client.ServiceInstance
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit

abstract class FeignTarget<T>(
protected open val serviceName: String,
protected open val type: Class<T>,
protected open val commonUrlPrefix: String,
protected val usedInstance: ConcurrentHashMap<String, ServiceInstance> =
ConcurrentHashMap<String, ServiceInstance>()
// key: serviceName, value: List<ServiceInstance>
protected val usedInstance: Cache<String, List<ServiceInstance>> = CacheBuilder.newBuilder()
.maximumSize(1000)
.expireAfterWrite(3, TimeUnit.SECONDS)
.build<String, List<ServiceInstance>>()
) : Target<T> {

override fun apply(input: RequestTemplate?): Request {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
*
* Terms of the MIT License:
* ---------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
Expand Down Expand Up @@ -49,45 +50,37 @@ open class ConsulServiceTarget<T> constructor(
}

override fun choose(serviceName: String): ServiceInstance {
val instances = discoveryClient.getInstances(serviceName)
?: throw ClientException(
"找不到任何有效的[$serviceName]服务提供者"
)
if (instances.isEmpty()) {
throw ClientException(
"找不到任何有效的[$serviceName]服务提供者"
)
}

val matchTagInstances = ArrayList<ServiceInstance>()
val serviceInstanceList: List<ServiceInstance> = usedInstance.getIfPresent(serviceName) ?: emptyList()

instances.forEach { serviceInstance ->
if (serviceInstance is ConsulServiceInstance && serviceInstance.tags.contains(tag) &&
!usedInstance.contains(serviceInstance.url())) {
matchTagInstances.add(serviceInstance)
if (serviceInstanceList.isEmpty()) {
val currentInstanceList = discoveryClient.getInstances(serviceName)
if (currentInstanceList.isEmpty()) {
logger.error("Unable to find any valid [$serviceName] service provider")
throw ClientException(
"Unable to find any valid [$serviceName] service provider"
)
}
currentInstanceList.forEach { serviceInstance ->
if (serviceInstance is ConsulServiceInstance && serviceInstance.tags.contains(tag)) {
serviceInstanceList.toMutableList().add(serviceInstance)
}
}
}

// 如果为空,则将之前用过的实例重新加入选择
if (matchTagInstances.isEmpty() && usedInstance.isNotEmpty()) {
matchTagInstances.addAll(usedInstance.values)
}

if (matchTagInstances.isEmpty()) {
throw ClientException(
"找不到任何有效的[$serviceName]服务提供者"
)
} else if (matchTagInstances.size > 1) {
matchTagInstances.shuffle()
if (serviceInstanceList.isEmpty()) {
logger.error("Unable to find any valid [$serviceName] service provider")
throw ClientException(
"Unable to find any valid [$serviceName] service provider"
)
} else {
usedInstance.put(serviceName, serviceInstanceList)
serviceInstanceList.toMutableList().shuffle()
}
}

usedInstance[matchTagInstances[0].url()] = matchTagInstances[0]
return matchTagInstances[0]
return serviceInstanceList[0]
}

override fun url(): String {
return choose(serviceName).url()
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
*
* Terms of the MIT License:
* ---------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
Expand All @@ -27,6 +28,7 @@
package com.tencent.devops.common.client.ms

import com.tencent.devops.common.api.exception.ClientException
import org.apache.commons.lang3.RandomUtils
import org.slf4j.LoggerFactory
import org.springframework.cloud.client.ServiceInstance
import org.springframework.cloud.kubernetes.client.discovery.KubernetesInformerDiscoveryClient
Expand All @@ -47,40 +49,21 @@ class KubernetesServiceTarget<T> constructor(
}

override fun choose(serviceName: String): ServiceInstance {
val instances = discoveryClient.getInstances(serviceName)
?: throw ClientException(
"找不到任何有效的[$serviceName]服务提供者"
)
if (instances.isEmpty()) {
throw ClientException(
"找不到任何有效的[$serviceName]服务提供者"
)
}
val serviceInstanceList: List<ServiceInstance> = usedInstance.getIfPresent(serviceName) ?: emptyList()

val matchTagInstances = ArrayList<ServiceInstance>()

instances.forEach { serviceInstance ->
if (!usedInstance.contains(serviceInstance.url())) {
// logger.info("service instance url: ${serviceInstance.url()}")
matchTagInstances.add(serviceInstance)
if (serviceInstanceList.isEmpty()) {
val currentInstanceList = discoveryClient.getInstances(serviceName)
if (currentInstanceList.isEmpty()) {
logger.error("Unable to find any valid [$serviceName] service provider")
throw ClientException(
"Unable to find any valid [$serviceName] service provider"
)
}
serviceInstanceList.toMutableList().addAll(currentInstanceList)
usedInstance.put(serviceName, serviceInstanceList)
}

// 如果为空,则将之前用过的实例重新加入选择
if (matchTagInstances.isEmpty() && usedInstance.isNotEmpty()) {
matchTagInstances.addAll(usedInstance.values)
}

if (matchTagInstances.isEmpty()) {
throw ClientException(
"找不到任何有效的[$serviceName]服务提供者"
)
} else if (matchTagInstances.size > 1) {
matchTagInstances.shuffle()
}

usedInstance[matchTagInstances[0].url()] = matchTagInstances[0]
return matchTagInstances[0]
return serviceInstanceList[RandomUtils.nextInt(0, serviceInstanceList.size)]
}

}
Loading