如何使用kotlin协同程序在spring flux上实现多租户?

如何使用kotlin协同程序在spring flux上实现多租户?,spring,kotlin,spring-webflux,kotlin-coroutines,reactor,Spring,Kotlin,Spring Webflux,Kotlin Coroutines,Reactor,对不起我的英语 我在春天和科特林是新来的。 我试图解决的问题是在kotlin协同程序中获取租户价值。 我举了一个简单的例子 我有一个TenantContext类,它保存传递的租户值 @Component object TenantContext { const val DEFAULT: String = "default" private val logger = LoggerFactory.getLogger(javaClass) private val curre

对不起我的英语

我在春天和科特林是新来的。
我试图解决的问题是在kotlin协同程序中获取租户价值。 我举了一个简单的例子

我有一个TenantContext类,它保存传递的租户值

@Component
object TenantContext {
    const val DEFAULT: String = "default"

    private val logger = LoggerFactory.getLogger(javaClass)

    private val currentTenant = InheritableThreadLocal<String?>()

    fun getTenant() : String {
        return currentTenant.get() ?: DEFAULT
    }
    fun set(tenantId : String) {
        currentTenant.set(tenantId)
    }
    fun remove() {
        currentTenant.remove()
    }

    fun asContextElement(): ThreadContextElement<String?> {
        logger.debug("[d] asContextElement ${getTenant()}")
        return currentTenant.asContextElement(getTenant())
    }
}

如何从/failed方法中获取租户值?

我对Kotlin不太熟悉,无法给出一个具体的示例,但在flux应用程序中不应该使用ThreadLocal或InheritableThreadLocal,因为在传统web应用程序中,每个请求没有专用线程。这可能就是为什么你会得到意想不到的结果

考虑使用所描述的上下文

@Component
class TenantFilter (
    private val tenantContext: TenantContext
) : WebFilter {
    private val logger = LoggerFactory.getLogger(javaClass)

    private val tenantHeader = "tenant"

    override fun filter(
        serverWebExchange: ServerWebExchange,
        webFilterChain: WebFilterChain
    ): Mono<Void> {
        val tenant = serverWebExchange.request.headers[tenantHeader]

        if (tenant.isNullOrEmpty()) {
            setTenant(TenantContext.DEFAULT)
        } else {
            setTenant(tenant.first())
        }
        logger.debug("[d] currentThread =  ${Thread.currentThread()}")
        return webFilterChain.filter(serverWebExchange)
    }

    private fun setTenant(tenant: String) {
        try {
            tenantContext.set(tenant)
        } catch (e: Exception) {
            throw RuntimeException()
        }
    }
}
@RestController
class RestController(
    private val service: SomeService
) {
    private val logger = LoggerFactory.getLogger(javaClass)

    @PostMapping("/working")
    suspend fun working(@RequestParam("param") param : Int) : ResponseEntity<*> = coroutineScope(){
        val res = async{ service.doSomething(param)}
        return@coroutineScope ResponseEntity.ok(res)
    }

    @PostMapping("/failed")
    suspend fun failed(@RequestBody body: BodyParam) : ResponseEntity<*> = coroutineScope(){
        logger.debug("[d] ${body.toString()}")
        val res = async { service.doSomething(body.value) }

        return@coroutineScope ResponseEntity.ok(res)
    }
}
printf "\nworking go"

curl -i -X POST \
   -H "tenant:properTenant" \
   -H "Content-Type:application/json" \
 'http://localhost:8080/working?param=123'

printf "\nfailed go"

curl -i -X POST \
   -H "tenant:properTenant" \
   -H "Content-Type:application/json" \
   -d \
'{"value":21312}' \
 'http://localhost:8080/failed'

working returns
"param = 123 and tenant was = properTenant"

failed returns
"param = 21312 and tenant was = default"