diff --git a/src/backend/turbo/common-turbo/common-turbo-client/common-client-base/src/main/kotlin/com/tencent/devops/common/client/ms/FeignTarget.kt b/src/backend/turbo/common-turbo/common-turbo-client/common-client-base/src/main/kotlin/com/tencent/devops/common/client/ms/FeignTarget.kt index 9326a6267..bbdbd2c8c 100644 --- a/src/backend/turbo/common-turbo/common-turbo-client/common-client-base/src/main/kotlin/com/tencent/devops/common/client/ms/FeignTarget.kt +++ b/src/backend/turbo/common-turbo/common-turbo-client/common-client-base/src/main/kotlin/com/tencent/devops/common/client/ms/FeignTarget.kt @@ -41,10 +41,10 @@ abstract class FeignTarget( protected open val type: Class, protected open val commonUrlPrefix: String, // key: serviceName, value: List - protected val usedInstance: Cache> = CacheBuilder.newBuilder() + protected val usedInstance: Cache> = CacheBuilder.newBuilder() .maximumSize(1000) .expireAfterWrite(3, TimeUnit.SECONDS) - .build>() + .build>() ) : Target { override fun apply(input: RequestTemplate?): Request { diff --git a/src/backend/turbo/common-turbo/common-turbo-client/common-client-consul/src/main/kotlin/com/tencent/devops/common/client/ms/ConsulServiceTarget.kt b/src/backend/turbo/common-turbo/common-turbo-client/common-client-consul/src/main/kotlin/com/tencent/devops/common/client/ms/ConsulServiceTarget.kt index 3686e3ed5..b059513d5 100644 --- a/src/backend/turbo/common-turbo/common-turbo-client/common-client-consul/src/main/kotlin/com/tencent/devops/common/client/ms/ConsulServiceTarget.kt +++ b/src/backend/turbo/common-turbo/common-turbo-client/common-client-consul/src/main/kotlin/com/tencent/devops/common/client/ms/ConsulServiceTarget.kt @@ -32,6 +32,7 @@ import org.slf4j.LoggerFactory import org.springframework.cloud.client.ServiceInstance import org.springframework.cloud.client.discovery.DiscoveryClient import org.springframework.cloud.consul.discovery.ConsulServiceInstance +import java.util.Random open class ConsulServiceTarget constructor( override val serviceName: String, @@ -50,34 +51,31 @@ open class ConsulServiceTarget constructor( } override fun choose(serviceName: String): ServiceInstance { - val serviceInstanceList: List = usedInstance.getIfPresent(serviceName) ?: emptyList() + val serviceInstanceList: MutableList = usedInstance.getIfPresent(serviceName) ?: mutableListOf() 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" - ) + val errMsg = "Unable to find any valid [$serviceName] service provider" + logger.error(errMsg) + throw ClientException(errMsg) } currentInstanceList.forEach { serviceInstance -> - if (serviceInstance is ConsulServiceInstance && serviceInstance.tags.contains(tag)) { - serviceInstanceList.toMutableList().add(serviceInstance) + if ((serviceInstance is ConsulServiceInstance) && serviceInstance.tags.contains(tag)) { + serviceInstanceList.add(serviceInstance) } } if (serviceInstanceList.isEmpty()) { - logger.error("Unable to find any valid [$serviceName] service provider") - throw ClientException( - "Unable to find any valid [$serviceName] service provider" - ) + val errMsg = "Unable to find any valid [$serviceName] service provider" + logger.error(errMsg) + throw ClientException(errMsg) } else { usedInstance.put(serviceName, serviceInstanceList) - serviceInstanceList.toMutableList().shuffle() } } - - return serviceInstanceList[0] + return serviceInstanceList[Random().nextInt(serviceInstanceList.size)] } override fun url(): String { diff --git a/src/backend/turbo/common-turbo/common-turbo-client/common-client-k8s/src/main/kotlin/com/tencent/devops/common/client/ms/KubernetesServiceTarget.kt b/src/backend/turbo/common-turbo/common-turbo-client/common-client-k8s/src/main/kotlin/com/tencent/devops/common/client/ms/KubernetesServiceTarget.kt index 8777919bc..da610e273 100644 --- a/src/backend/turbo/common-turbo/common-turbo-client/common-client-k8s/src/main/kotlin/com/tencent/devops/common/client/ms/KubernetesServiceTarget.kt +++ b/src/backend/turbo/common-turbo/common-turbo-client/common-client-k8s/src/main/kotlin/com/tencent/devops/common/client/ms/KubernetesServiceTarget.kt @@ -49,17 +49,16 @@ class KubernetesServiceTarget constructor( } override fun choose(serviceName: String): ServiceInstance { - val serviceInstanceList: List = usedInstance.getIfPresent(serviceName) ?: emptyList() + val serviceInstanceList: MutableList = usedInstance.getIfPresent(serviceName) ?: mutableListOf() 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" - ) + val errMessage = "Unable to find any valid [$serviceName] service provider" + logger.error(errMessage) + throw ClientException(errMessage) } - serviceInstanceList.toMutableList().addAll(currentInstanceList) + serviceInstanceList.addAll(currentInstanceList) usedInstance.put(serviceName, serviceInstanceList) }