Eureka Health 前置

  • spring boot/cloud 版本 2.0.1
  • JDK 1.8

默认情况下注册到eureka server的服务是通过心跳来告知自己是UP还是DOWN,并不是通过spring-boot-actuator模块的/health端点来实现的。
默认的心跳实现方式可以有效的检查eureka客户端进程是否正常运作,但是无法保证客户端应用能够正常提供服务。由于大多数微服务应用都会有一些其他的外部资源依赖,比如数据库,REDIS缓存等,如果我们的应用与这些外部资源无法连通的时候,实际上已经不能提供正常的对外服务了,但因为客户端心跳依然在运行,所以它还是会被服务消费者调用,而这样的调用实际上并不能获得预期的后果。

我们可以通过在eureka客户端中配置:eureka.client.healthcheck.enabled=true,就可以改变eureka server对客户端健康检测的方式,改用actuator的/health端点来检测。

关于 spring-boot-starter-actuator

依赖

org.springframework.boot:spring-boot-starter-actuator

API

http://ip:port/[project-content-path]/actuator/**

以前的版本是没有/actuator前缀的,2.0以后的版本都加了/actuator前缀

默认情况下,actuator只暴露了 /health/info 两个接口,其他接口可以在yml中添加如下配置打开:

1
2
3
4
5
management:
endpoints:
web:
exposure:
include: "*"
  • 查看支持的api列表:http://ip:port/[project-content-path]/actuator
    返回:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    {
    "_links": {
    "self": {
    "href": "http://192.168.9.73:19342/actuator",
    "templated": false
    },
    "archaius": {
    "href": "http://192.168.9.73:19342/actuator/archaius",
    "templated": false
    },
    "auditevents": {
    "href": "http://192.168.9.73:19342/actuator/auditevents",
    "templated": false 显示应用暴露的审计事件 (比如认证进入、失败)
    },
    "beans": {
    "href": "http://192.168.9.73:19342/actuator/beans",
    "templated": false 加载的bean列表
    },
    "health": {
    "href": "http://192.168.9.73:19342/actuator/health",
    "templated": false 健康状态 up down
    },
    "conditions": {
    "href": "http://192.168.9.73:19342/actuator/conditions",
    "templated": false 显示在配置和自动配置类上评估的条件以及它们匹配或不匹配的原因
    },
    "configprops": {
    "href": "http://192.168.9.73:19342/actuator/configprops",
    "templated": false 显示所有的配置列表 @ConfigurationProperties
    },
    "env": {
    "href": "http://192.168.9.73:19342/actuator/env",
    "templated": false 环境变量信息
    },
    "env-toMatch": {
    "href": "http://192.168.9.73:19342/actuator/env/{toMatch}",
    "templated": true
    },
    "info": {
    "href": "http://192.168.9.73:19342/actuator/info",
    "templated": false
    },
    "loggers": {
    "href": "http://192.168.9.73:19342/actuator/loggers",
    "templated": false 日志埋点信息
    },
    "loggers-name": {
    "href": "http://192.168.9.73:19342/actuator/loggers/{name}",
    "templated": true
    },
    "heapdump": {
    "href": "http://192.168.9.73:19342/actuator/heapdump",
    "templated": false
    },
    "threaddump": {
    "href": "http://192.168.9.73:19342/actuator/threaddump",
    "templated": false 执行线程转储
    },
    "metrics-requiredMetricName": {
    "href": "http://192.168.9.73:19342/actuator/metrics/{requiredMetricName}",
    "templated": true 显示当前应用程序的“指标”信息
    },
    "metrics": {
    "href": "http://192.168.9.73:19342/actuator/metrics",
    "templated": false 显示当前应用程序的“指标”信息名称列表(requiredMetricName 列表)
    },
    "scheduledtasks": {
    "href": "http://192.168.9.73:19342/actuator/scheduledtasks",
    "templated": false
    },
    "httptrace": {
    "href": "http://192.168.9.73:19342/actuator/httptrace",
    "templated": false 最近的100个(默认)http请求信息
    },
    "mappings": {
    "href": "http://192.168.9.73:19342/actuator/mappings",
    "templated": false 显示所有@RequestMapping路径的整理列表
    },
    "refresh": {
    "href": "http://192.168.9.73:19342/actuator/refresh",
    "templated": false
    },
    "features": {
    "href": "http://192.168.9.73:19342/actuator/features",
    "templated": false
    },
    "service-registry": {
    "href": "http://192.168.9.73:19342/actuator/service-registry",
    "templated": false
    },
    "hystrix.stream": {
    "href": "http://192.168.9.73:19342/actuator/hystrix.stream",
    "templated": false
    }
    }
    }

    更多说明:https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html

/health 相关

http://ip:port/[project-content-path]/actuator/health
默认只会显示status状态值 up 或者 down,如果需要显示详细详细需要在配置中设置:

1
2
3
4
5
6
7
management:
endpoint:
health:
show-details: always
# 默认为 'never' 永远不显示
# 'when-authorized' 详细信息仅向授权用户显示。可以使用配置授权角色 management.endpoint.health.roles。
# always 始终显示

HealthIndicators 健康指标

  • 默认自带以下健康检测项目:
名称 说明
CassandraHealthIndicator Checks that a Cassandra database is up.
CouchbaseHealthIndicator Checks that a Couchbase cluster is up.
DiskSpaceHealthIndicator Checks for low disk space.
DataSourceHealthIndicator Checks that a connection to DataSource can be obtained.
ElasticsearchHealthIndicator Checks that an Elasticsearch cluster is up.
InfluxDbHealthIndicator Checks that an InfluxDB server is up.
JmsHealthIndicator Checks that a JMS broker is up.
MailHealthIndicator Checks that a mail server is up.
MongoHealthIndicator Checks that a Mongo database is up.
Neo4jHealthIndicator Checks that a Neo4j server is up.
RabbitHealthIndicator Checks that a Rabbit server is up.
RedisHealthIndicator Checks that a Redis server is up.
SolrHealthIndicator Checks that a Solr server is up.
  • 自定义 HealthIndicator

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    @Component
    public class ExtendedHealthIndicator implements HealthIndicator {
    @Override
    public Health health() {
    int errorCode = check(); // perform some specific health check 执行自定义的检测逻辑。
    if (errorCode != 0) {
    return Health.down().withDetail("Error Code", errorCode).build();
    }
    return Health.up().build();
    }

    private int check() {
    return 1; // 假装检测到异常
    }
    }

    /health api中会多返回一个名为Extended的项,如图:

    同时在eureka中:

    响应式 HealthIndicator

    响应式编程相关 spring reactor.

    1
    2
    3
    4
    5
    6
    7
    public class ExtendedReactiveHealthIndicator implements ReactiveHealthIndicator {
    @Override
    public Mono<Health> health() {
    return doHealthCheck() //perform some specific health check that returns a Mono<Health>
    .onErrorResume(ex -> Mono.just(new Health.Builder().down(ex).build())));
    }
    }

/loggers 相关

  • 查看
    http://ip:port/[project-content-path]/actuator/loggers
    查看应用中可配置的loggers的列表和相关的日志等级。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    {
    "levels": [
    "OFF",
    "ERROR",
    "WARN",
    "INFO",
    "DEBUG",
    "TRACE"
    ],
    "loggers": {
    "ROOT": {
    "configuredLevel": "INFO",
    "effectiveLevel": "INFO"
    },
    "com": {
    "configuredLevel": null,
    "effectiveLevel": "INFO"
    }
    ...
    }
    }
  • 查看某一项
    http://ip:port/[project-content-path]/actuator/loggers

  • 运行时改变日志等级
    使用POST请求可以修改某一项的日志等级,例如:

    1
    2
    3
    4
    5
    POST http://ip:port/[project-content-path]/actuator/loggers/ROOT
    body:
    {
    "configuredLevel": "INFO"
    }