在Spring Boot Actuators中,有没有一种标准方法来检查子服务的健康状态?

huangapple go评论115阅读模式
英文:

Is there a standard way in Spring Boot Actuators of checking the health of child services?

问题

以下是要翻译的内容:

假设我有一个依赖(调用)Spring Boot服务B的Spring Boot服务A。

A -> B

Spring Boot Actuators可以告诉我A是否正常运行。

https://A/health

我想知道的是通过调用A来检查B是否正常运行。

https://A/integratedhealth

我的问题是:在Spring Boot Actuators中,是否有标准的方法来检查子服务的健康状态?
(还是我必须构建一个自定义的执行器服务?)

英文:

Suppose I have Spring Boot service A which depends on (calls) Spring Boot service B.

  1. A -> B

Spring Boot Actuators can tell me if A is up.

https://A/health

What I want to know is if B is up, by calling A.

https://A/integratedhealth

My question is: Is there a standard way in Spring Boot Actuators of checking the health of child services?
(or do I just have to build a custom actuator service?)

答案1

得分: 1

Spring Boot提供了许多开箱即用的健康指标。但是,您可以通过实现HealthIndicator接口(对于响应式应用程序,使用ReactiveHealthIndicator)来添加自定义健康指标:

  1. @Component
  2. public class ServiceBHealthIndicator implements HealthIndicator {
  3. private final String message_key = "Service B";
  4. @Override
  5. public Health health() {
  6. if (!isRunningServiceB()) {
  7. return Health.down().withDetail(message_key, "Not Available").build();
  8. }
  9. return Health.up().withDetail(message_key, "Available").build();
  10. }
  11. private Boolean isRunningServiceB() {
  12. Boolean isRunning = true;
  13. // 在这里添加您的逻辑
  14. return isRunning;
  15. }
  16. }

如果将其与之前的健康指标等组合,您可以获得类似以下方式的健康端点响应:

  1. {
  2. "status":"DOWN",
  3. "details":{
  4. "serviceB":{
  5. "status":"UP",
  6. "details":{
  7. "Service B":"Available"
  8. }
  9. },
  10. "serviceC":{
  11. "status":"DOWN",
  12. "details":{
  13. "Service C":"Not Available"
  14. }
  15. }
  16. }
  17. }

您可以在Spring Boot文档中找到有关自定义健康检查端点的更多信息。

英文:

Spring boot provides a lot of Health Indicators out of the box. However, you can add your own custom health indicator by implementing HealthIndicator interface (ReactiveHealthIndicator for reactive apps):

  1. @Component
  2. public class ServiceBHealthIndicator implements HealthIndicator {
  3. private final String message_key = "Service B";
  4. @Override
  5. public Health health() {
  6. if (!isRunningServiceB()) {
  7. return Health.down().withDetail(message_key, "Not Available").build();
  8. }
  9. return Health.up().withDetail(message_key, "Available").build();
  10. }
  11. private Boolean isRunningServiceB() {
  12. Boolean isRunning = true;
  13. // Your logic here
  14. return isRunning;
  15. }
  16. }

If you combine it with other health indicators like the one before, you can get a health endpoint response in a way like this:

  1. {
  2. "status":"DOWN",
  3. "details":{
  4. "serviceB":{
  5. "status":"UP",
  6. "details":{
  7. "Service B":"Available"
  8. }
  9. },
  10. "serviceC":{
  11. "status":"DOWN",
  12. "details":{
  13. "Service C":"Not Available"
  14. }
  15. }
  16. }
  17. }

You can find more information about custom health checks and endpoints in the spring boot documentation.

huangapple
  • 本文由 发表于 2020年8月14日 11:53:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63406277.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定