原文: https://howtodoinjava.com/dropwizard/health-check-configuration-example/

我们已经看到 dropwizard 在开发自包含的 REST API 甚至 REST 客户端服务方面如此有效。 Dropwizard 包含几乎所有必需的包,它们可以非常简单地构建 API,而无需使事情复杂化。 dropwizard 的一项易于实现的功能是运行状况检查服务,该服务可用于在运行时监视正在创建的应用/组件的状态。

实现 Dropwizard 运行状况检查

DropWizard 运行状况检查是通过扩展HealthCheck类并在一切正常的情况下返回Result.healthy()以及在某些情况下无效的情况下返回Result.unhealthy()来实现的 - 符合预期。

运行状况检查配置

  1. public class AppHealthCheck extends HealthCheck
  2. {
  3. @Override
  4. protected Result check() throws Exception
  5. {
  6. if(Check some condition == true){
  7. return Result.healthy();
  8. }
  9. return Result.unhealthy("Error message");
  10. }
  11. }

要在 dropwizard 应用中注册此AppHealthCheck类,请使用Environment.healthChecks()注册表。

  1. public void run(Configuration c, Environment e) throws Exception
  2. {
  3. //Application health check
  4. e.healthChecks().register("APIHealthCheck", new AppHealthCheck());
  5. }

验证系统运行状况

Dropwizard 将在管理端口上的/healthcheck上使用 HTTP 资源终结点(默认为 8081)。 在默认情况下,Dropwizard 还包括对死锁的检查以及您定义的自定义运行状况检查AppHealthCheck

  1. http://localhost:8081/healthcheck

上面的运行状况检查网址将返回一些结果,如下所示:

  1. {
  2. "APIHealthCheck": {
  3. "healthy": true
  4. },
  5. "deadlocks": {
  6. "healthy": true
  7. }
  8. }

自定义 REST 资源以运行运行状况检查

如果您不想使用管理端口,则还可以创建一个自定义 REST 资源,该资源将为您运行运行状况检查,并以所需的响应格式返回结果。

要运行所有运行状况检查并获取所有结果,您将在 REST 资源中调用registry.runHealthChecks()

  1. @Produces(MediaType.APPLICATION_JSON)
  2. @Path("/status")
  3. public class HealthCheckController
  4. {
  5. private HealthCheckRegistry registry;
  6. public HealthCheckController(HealthCheckRegistry registry) {
  7. this.registry = registry;
  8. }
  9. @GET
  10. public Set<Entry<String, Result>> getStatus(){
  11. return registry.runHealthChecks().entrySet();
  12. }
  13. }

现在,当我们使用http://localhost:8080/status调用此 REST API 时,我们将得到如下响应:

  1. [
  2. {
  3. "APIHealthCheck": {
  4. "healthy": true,
  5. "message": null,
  6. "error": null
  7. }
  8. },
  9. {
  10. "deadlocks": {
  11. "healthy": true,
  12. "message": null,
  13. "error": null
  14. }
  15. }
  16. ]

您可以根据需要自定义消息。

Dropwizard 运行状况检查示例

为了演示上述两种功能,我修改了 dropwizard HelloWorld 应用中给出的代码。

AppHealthCheck.java

  1. package com.howtodoinjava.healthcheck;
  2. import java.util.ArrayList;
  3. import javax.ws.rs.client.Client;
  4. import javax.ws.rs.client.Invocation;
  5. import javax.ws.rs.client.WebTarget;
  6. import javax.ws.rs.core.MediaType;
  7. import javax.ws.rs.core.Response;
  8. import com.codahale.metrics.health.HealthCheck;
  9. public class AppHealthCheck extends HealthCheck {
  10. private final Client client;
  11. public AppHealthCheck(Client client) {
  12. super();
  13. this.client = client;
  14. }
  15. @Override
  16. protected Result check() throws Exception {
  17. WebTarget webTarget = client.target("http://localhost:8080/employees");
  18. Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
  19. Response response = invocationBuilder.get();
  20. @SuppressWarnings("rawtypes")
  21. ArrayList employees = response.readEntity(ArrayList.class);
  22. if(employees !=null && employees.size() > 0){
  23. return Result.healthy();
  24. }
  25. return Result.unhealthy("API Failed");
  26. }
  27. }

HealthCheckController.java

  1. package com.howtodoinjava.healthcheck;
  2. import java.util.Map.Entry;
  3. import java.util.Set;
  4. import javax.ws.rs.GET;
  5. import javax.ws.rs.Path;
  6. import javax.ws.rs.Produces;
  7. import javax.ws.rs.core.MediaType;
  8. import com.codahale.metrics.health.HealthCheck.Result;
  9. import com.codahale.metrics.health.HealthCheckRegistry;
  10. @Produces(MediaType.APPLICATION_JSON)
  11. @Path("/status")
  12. public class HealthCheckController
  13. {
  14. private HealthCheckRegistry registry;
  15. public HealthCheckController(HealthCheckRegistry registry) {
  16. this.registry = registry;
  17. }
  18. @GET
  19. public Set<Entry<String, Result>> getStatus(){
  20. return registry.runHealthChecks().entrySet();
  21. }
  22. }

App.java

  1. package com.howtodoinjava.rest;
  2. import io.dropwizard.Application;
  3. import io.dropwizard.Configuration;
  4. import io.dropwizard.client.JerseyClientBuilder;
  5. import io.dropwizard.setup.Bootstrap;
  6. import io.dropwizard.setup.Environment;
  7. import javax.ws.rs.client.Client;
  8. import com.howtodoinjava.healthcheck.AppHealthCheck;
  9. import com.howtodoinjava.healthcheck.HealthCheckController;
  10. import com.howtodoinjava.rest.controller.EmployeeRESTController;
  11. import com.howtodoinjava.rest.controller.RESTClientController;
  12. public class App extends Application<Configuration> {
  13. @Override
  14. public void initialize(Bootstrap<Configuration> b) {
  15. }
  16. @Override
  17. public void run(Configuration c, Environment e) throws Exception
  18. {
  19. e.jersey().register(new EmployeeRESTController(e.getValidator()));
  20. final Client client = new JerseyClientBuilder(e).build("DemoRESTClient");
  21. e.jersey().register(new RESTClientController(client));
  22. //Application health check
  23. e.healthChecks().register("APIHealthCheck", new AppHealthCheck(client));
  24. //Run multiple health checks
  25. e.jersey().register(new HealthCheckController(e.healthChecks()));
  26. }
  27. public static void main(String[] args) throws Exception {
  28. new App().run(args);
  29. }
  30. }

验证运行状况检查 URL

1)http://localhost:8081/healthcheck

Dropwizard 运行状况检查配置示例 - 图1

在管理端口上进行 Dropwizard 运行状况检查

2)http://localhost:8080/status

Dropwizard 运行状况检查配置示例 - 图2

在应用端口上进行 Dropwizard 运行状况检查

在评论部分让我知道您的问题。

学习愉快!

源码下载

参考:

Dropwizard 文档