仅替换执行器的当前行为/一个组件的运行状况

replace current behavior of actuator/health of one componant only

提问人:user1361815 提问时间:11/16/2023 最后编辑:Paulouser1361815 更新时间:11/16/2023 访问量:22

问:

当我打电话时

[GET] /actuator/health

我正在获取我的 API 正在使用的所有外部服务的 json 格式列表:elasticsearch + mongo 和 rabbit。

没关系

但 我只想更改对elasticSearch的检查方式。

我添加了一个扩展 HealthIndicator 的类,但它只为 elastic 添加了一个新行为。它目前不会取代当前的行为。

    import lombok.extern.slf4j.Slf4j
    import okhttp3.*
    import org.springframework.beans.factory.annotation.Value
    import org.springframework.boot.actuate.health.Health
    import org.springframework.boot.actuate.health.HealthIndicator
    import org.springframework.http.HttpStatus
    import org.springframework.stereotype.Component
    import org.springframework.web.server.ResponseStatusException
    import java.io.IOException
    
    @Component
    @Slf4j
    class UrlShortenerServiceHealthIndicator(@Value("\${spring.elasticsearch.uris}") elsaticUrl: String,
                                             @Value("\${spring.elasticsearch.username}") elsaticUsername: String,
                                             @Value("\${spring.elasticsearch.password}") elsaticPassword: String) : HealthIndicator {
    
        private var url: String = ""
        private var username: String = ""
        private var password: String = ""
    
        init {
            url = elsaticUrl
            username = elsaticUsername
            password = elsaticPassword
        }
    
        override fun health(): Health {
            // check if the URL shortener service URL is reachable
            try {
    
                val client = OkHttpClient.Builder().authenticator(object : Authenticator {
                    @Throws(IOException::class)
                    override fun authenticate(route: Route?, response: Response): Request? {
                        if (response.request.header("Authorization") != null) {
                            return null // Give up, we've already attempted to authenticate.
                        }
    
                        println("Authenticating for response: $response")
                        println("Challenges: ${response.challenges()}")
                        val credential = Credentials.basic(username, password)
                        return response.request.newBuilder().header("Authorization", credential).build()
                    }
                }).build()
    
                val request = Request.Builder().url("http://" + url + "/_cat/indices?health=red&h=idx").build()
    
                client.newCall(request).execute().use { response ->
                    when (response.code) {
                        200 -> {
                            if (!response.isSuccessful) throw IOException("Unexpected code $response")
    
                            val result = response.body!!.string()
    
                            val indexStatus: List<String> = result.split("\n").filter {
                                it.startsWith("mita-")
                            }
    
                            return if (indexStatus.isEmpty()) Health.up().build() else return Health.down().withDetail("error :", indexStatus).build()
                        }
    
                        else -> throw ResponseStatusException(HttpStatus.valueOf(response.code), "Error with WS Scaneo Active Store ! ")
                    }
                }
            } catch (e: Exception) {
                return Health.down().withDetail("error", e.message).build()
            }
            return Health.up().build()
        }
    }

关于如何更换弹性方式或检查弹性的任何想法?

谢谢

Elasticsearch spring-boot-actuator

评论


答: 暂无答案