[英]How to override Spring Boot health response codes
How to override actuator's default /health response status code based on its state:DOWN, UP, UNKNOWN so and so ?如何根据其状态覆盖执行器的默认 /health 响应状态代码:DOWN、UP、UNKNOWN 等等? For example, if the health state is "UP", the response code should be 200. If DOWN: 400, unknown 300. Is there any work around to achieve this ?
例如,如果健康状态为“UP”,则响应代码应为 200。如果 DOWN:400,未知 300。是否有任何解决方法可以实现这一点?
Note: I do not want my own health endpoint.注意:我不想要我自己的健康端点。 Instead, the existing one needs to be overriden .
相反,需要覆盖现有的。
In Spring boot 2.x these are the built in status and their defaults, which you can override to the relevant code you want.在 Spring boot 2.x 中,这些是内置状态及其默认值,您可以将其覆盖为所需的相关代码。
management.endpoint.health.status.http-mapping.UP=200
management.endpoint.health.status.http-mapping.UNKNOWN=200
management.endpoint.health.status.http-mapping.DOWN=503
management.endpoint.health.status.http-mapping.OUT_OF_SERVICE=503
or a custom mapping或自定义映射
management.endpoint.health.status.http-mapping.DOWN=400
or in yaml,或在 yaml 中,
management:
endpoint:
health:
status:
http-mapping:
UP: 200
UNKNOWN: 200
DOWN: 503
OUT_OF_SERVICE: 503
In Spring boot 1.x these are the properties with custom status applied,在 Spring boot 1.x 中,这些是应用了自定义状态的属性,
endpoints.health.mapping.DOWN=400
endpoints.health.mapping.UP=200
endpoints.health.mapping.UNKNOWN=300
or in yaml,或在 yaml 中,
endpoints:
health:
mapping:
UP: 200
DOWN: 400
UNKNOWN: 300
Spring Boot Current Actuator Health Documentation Spring Boot 当前执行器运行状况文档
The status codes are configured via application properties .状态代码是通过应用程序属性配置的。 These are the defaults:
这些是默认值:
management.health.status.http-mapping:
UP: 200
DOWN: 503
OUT_OF_SERVICE: 503
UNKNOWN: 200
For Spring Boot 1.x the property is endpoints.health.mapping
instead.对于Spring Boot 1.x,该属性改为
endpoints.health.mapping
。
It's a bad idea to use the codes you suggest, because they already mean other things.使用您建议的代码是一个坏主意,因为它们已经意味着其他事情。 A reasonable change is to set
UNKNOWN: 500
if you want your upstream system to treat it differently to UP
.如果您希望上游系统将其与
UP
区别对待,则合理的更改是设置UNKNOWN: 500
。
It seems the documentation has been updated again (second time).似乎文档已再次更新(第二次)。 The current properties for Health Check status mappings are:
健康检查状态映射的当前属性是:
management.health.status.http-mapping.DOWN=503
management.health.status.http-mapping.UP=200
By default, OUT_OF_SERVICE
and DOWN
map to 503. Any unmapped health statuses, including UP
, map to 200.默认情况下,
OUT_OF_SERVICE
和DOWN
映射到 503。任何未映射的健康状态,包括UP
,映射到 200。
These values can be overridden as mentioned in other answers.如其他答案中所述,这些值可以被覆盖。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.