Skip to content

Commit

Permalink
feat: Added expired deletion function for cached client instance info…
Browse files Browse the repository at this point in the history
  • Loading branch information
eazence committed May 9, 2024
1 parent e13d285 commit 2e11ae2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ abstract class FeignTarget<T>(
protected open val type: Class<T>,
protected open val commonUrlPrefix: String,
// key: serviceName, value: List<ServiceInstance>
protected val usedInstance: Cache<String, List<ServiceInstance>> = CacheBuilder.newBuilder()
protected val usedInstance: Cache<String, MutableList<ServiceInstance>> = CacheBuilder.newBuilder()
.maximumSize(1000)
.expireAfterWrite(3, TimeUnit.SECONDS)
.build<String, List<ServiceInstance>>()
.build<String, MutableList<ServiceInstance>>()
) : Target<T> {

override fun apply(input: RequestTemplate?): Request {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> constructor(
override val serviceName: String,
Expand All @@ -50,34 +51,31 @@ open class ConsulServiceTarget<T> constructor(
}

override fun choose(serviceName: String): ServiceInstance {
val serviceInstanceList: List<ServiceInstance> = usedInstance.getIfPresent(serviceName) ?: emptyList()
val serviceInstanceList: MutableList<ServiceInstance> = 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,16 @@ class KubernetesServiceTarget<T> constructor(
}

override fun choose(serviceName: String): ServiceInstance {
val serviceInstanceList: List<ServiceInstance> = usedInstance.getIfPresent(serviceName) ?: emptyList()
val serviceInstanceList: MutableList<ServiceInstance> = 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)
}

Expand Down

0 comments on commit 2e11ae2

Please sign in to comment.