小编faf*_*afl的帖子

Kotlin有像Python这样的"枚举"函数吗?

在Python中我可以写:

for i, element in enumerate(my_list):
    print i          # the index, starting from 0
    print element    # the list-element
Run Code Online (Sandbox Code Playgroud)

我怎么能在Kotlin写这个?

list enumerate kotlin

16
推荐指数
2
解决办法
2909
查看次数

如何在Kotlin中拥有复合钥匙?

在Python中,我可以使用复杂的字典键,例如:

d = {}
d[(1, 2)] = 3
print d[(1, 2)]  # prints 3
Run Code Online (Sandbox Code Playgroud)

如何在Kotlin中声明和填充此类地图?

编辑:我试图声明一个这样的地图,但我不知道如何填充它:

val my_map = HashMap<Pair<Int, Int>, Int>()
Run Code Online (Sandbox Code Playgroud)

dictionary kotlin

7
推荐指数
1
解决办法
1539
查看次数

如何将 CircuitBreaker 与 TimeLimiter 和 Bulkhead 结合使用?

我有一个通过 REST 调用依赖项的服务。服务和依赖是微服务架构的一部分,所以我想使用弹性模式。我的目标是:

  • 有一个断路器来保护它在挣扎时的依赖
  • 限制调用可以运行的时间。该服务具有 SLA,并且必须在特定时间进行响应。在超时时,我们使用回退值。
  • 限制对依赖项的并发调用数。通常调用率低,响应快,但我们希望保护依赖免受服务内部的突发和队列请求。

下面是我目前的代码。它有效,但理想情况下我想使用TimeLimiterBulkhead类,因为它们似乎可以协同工作。

我怎样才能更好地写这个?

@Component
class FooService(@Autowired val circuitBreakerRegistry: CircuitBreakerRegistry)
{
    ...

    // State machine to take load off the dependency when slow or unresponsive
    private val circuitBreaker = circuitBreakerRegistry
        .circuitBreaker("fooService")

    // Limit parallel requests to dependency
    private var semaphore = Semaphore(maxParallelRequests)

    // The protected function
    private suspend fun makeHttpCall(customerId: String): Boolean {
        val client = webClientProvider.getCachedWebClient(baseUrl)

        val response = client
            .head()
            .uri("/the/request/url")
            .awaitExchange()

        return when (val status = response.rawStatusCode()) { …
Run Code Online (Sandbox Code Playgroud)

spring-boot spring-webflux resilience4j kotlin-coroutines

5
推荐指数
1
解决办法
1071
查看次数