backend scaffold
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
dependencies {
|
||||
api project(':platform:platform-integration')
|
||||
implementation project(':platform:platform-web')
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.continental.retailapp.integration.mini
|
||||
|
||||
import com.continental.retailapp.platform.integration.IntegrationClientProperties
|
||||
import com.continental.retailapp.platform.integration.IntegrationException
|
||||
import com.continental.retailapp.platform.integration.RestClientFactory
|
||||
import com.continental.retailapp.platform.web.ErrorCode
|
||||
import org.springframework.context.annotation.Bean
|
||||
import org.springframework.context.annotation.Configuration
|
||||
import org.springframework.web.client.RestClient
|
||||
|
||||
/** Mini 侧(O2O 等自建服务)的 [RestClient],构建方式同 F6,见 05-integration-layer.md。 */
|
||||
@Configuration
|
||||
class MiniRestClientConfig {
|
||||
|
||||
@Bean
|
||||
fun miniRestClient(
|
||||
factory: RestClientFactory,
|
||||
props: IntegrationClientProperties,
|
||||
): RestClient = factory.build(props.require("mini")) { builder ->
|
||||
builder.defaultStatusHandler({ it.isError }) { _, response ->
|
||||
throw MiniUnavailableException("status=${response.statusCode}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class MiniUnavailableException(detail: String) :
|
||||
IntegrationException(ErrorCode.MINI_UNAVAILABLE, "服务暂不可用,请稍后重试") {
|
||||
init {
|
||||
org.slf4j.LoggerFactory.getLogger(javaClass).warn("Mini 服务错误: {}", detail)
|
||||
}
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package com.continental.retailapp.integration.mini
|
||||
|
||||
import io.github.resilience4j.bulkhead.annotation.Bulkhead
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.stereotype.Component
|
||||
import org.springframework.web.client.RestClient
|
||||
|
||||
/** 保修概要,`degraded` 的含义同 F6 侧。 */
|
||||
data class WarrantySummary(
|
||||
val storeId: Long,
|
||||
val openCount: Int,
|
||||
val degraded: Boolean = false,
|
||||
) {
|
||||
companion object {
|
||||
fun degraded(storeId: Long) = WarrantySummary(storeId, openCount = 0, degraded = true)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* O2O 客户端,见 05-integration-layer.md。
|
||||
*
|
||||
* 这里只用舱壁不用熔断:这是自建服务,抖动通常是短暂的,
|
||||
* 熔断打开反而会在恢复后多压 10 秒不可用。真正要防的是它把 Tomcat 线程全占住,
|
||||
* 那正是 `@Bulkhead` 的职责(`max-wait-duration: 0` —— 拿不到许可立刻失败,不排队)。
|
||||
*/
|
||||
@Component
|
||||
class O2OClient(private val miniRestClient: RestClient) {
|
||||
|
||||
private val log = LoggerFactory.getLogger(javaClass)
|
||||
|
||||
@Bulkhead(name = "mini-o2o")
|
||||
fun fetchWarrantySummary(storeId: Long): WarrantySummary =
|
||||
try {
|
||||
miniRestClient.get()
|
||||
.uri("/o2o/warranty/summary?storeId={storeId}", storeId)
|
||||
.retrieve()
|
||||
.body(WarrantySummary::class.java)
|
||||
?: WarrantySummary.degraded(storeId)
|
||||
} catch (ex: Exception) {
|
||||
log.warn("O2O 保修概要降级 storeId={} cause={}", storeId, ex.javaClass.simpleName)
|
||||
WarrantySummary.degraded(storeId)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user