Wildlfy-Swarm Microprofile HealthCheck无响应

时间:2018-01-09 14:42:10

标签: health-monitoring wildfly-swarm

我正在尝试在我的wildfly-swarm 2017.12.1应用程序中实现微文档HealthCheck

这是

@Health
@Dependent
public class SystemHealthChecker extends AbstractHealthChecker {

  @Override
  public HealthCheckResponse call() {
    boolean up;
    Runtime runtime = Runtime.getRuntime();
    long freeMemory = runtime.freeMemory();
    long totalMemory = runtime.totalMemory();
    double percentageFree = freeMemory*100/totalMemory;
    if(percentageFree < 10.0){
      up = false;
    }else{
      up = true;
    }

    Map<String, String> attributes = new HashMap<>();
    attributes.put("Total Memory", totalMemory+"");
    attributes.put("Free Memory", freeMemory+"");
    attributes.put("Percentage Free Memory", percentageFree+"%");
    HealthCheckResponse response = getResponse("System-Check", attributes, up);
    return response;
  }
}

AbstractHealthChecker如下:

public abstract class AbstractHealthChecker implements HealthCheck {

  protected HealthCheckResponse getResponse(String name, Map<String, String> data, boolean up){
    HealthCheckResponseBuilder responseBuilder = HealthCheckResponse.named(name);

    responseBuilder.state(up);

    for(String key: data.keySet()){
      responseBuilder.withData(key, data.get(key));
    }
    responseBuilder.withData("Date", new Date().toString());

    return responseBuilder.build();
  }
}

应检查系统可用内存是否低于10%,但并不重要。 但是,当我启动应用程序时,我收到警告:

2018-01-09 15:23:28,974 WARNING [ServiceLoader] (main) Could not load service class org.wildfly.swarm.microprofile.faulttolerance.deployment.HystrixExtension
2018-01-09 15:23:28,974 WARNING [ServiceLoader] (main) Could not load service class org.wildfly.swarm.microprofile.health.deployment.HealthExtension
2018-01-09 15:23:28,974 WARNING [ServiceLoader] (main) Could not load service class org.wildfly.swarm.microprofile.metrics.deployment.MetricCdiInjectionExtension
2018-01-09 15:23:28,975 WARNING [ServiceLoader] (main) Could not load service class org.wildfly.swarm.cdi.config.deployment.InjectConfigViewExtension
2018-01-09 15:23:28,975 WARNING [ServiceLoader] (main) Could not load service class org.wildfly.swarm.microprofile.jwtauth.deployment.auth.cdi.MPJWTExtension

/ health端点上的System-Check也没有实际输出,我只得到以下

{
  "outcome": "UP",
  "checks": []
}

所以我假设,我的HealthCheck没找到。 我使用以下分数。 有什么想法,出了什么问题?

<dependency>
    <groupId>org.wildfly.swarm</groupId>
    <artifactId>jaxrs</artifactId>
</dependency>
<dependency>
    <groupId>org.wildfly.swarm</groupId>
    <artifactId>ejb</artifactId>
</dependency>
<dependency>
    <groupId>org.wildfly.swarm</groupId>
    <artifactId>cdi</artifactId>
</dependency>
<dependency>
    <groupId>org.wildfly.swarm</groupId>
    <artifactId>microprofile</artifactId>
</dependency>
<dependency>
    <groupId>org.wildfly.swarm</groupId>
    <artifactId>logging</artifactId>
</dependency>

2 个答案:

答案 0 :(得分:2)

不知道AbstractHealthChecker中的内容很难确定,但您是否尝试过如下操作?我相信这是推荐的方式。

@Health
@ApplicationScoped
public class SystemHealthChecker implements HealthCheck {

    @Override
    public HealthCheckResponse call() {
        return HealthCheckResponse.named("sessions-check")
            .withData(sessionCountName, sessionStore.getSessions().size())
            .withData("lastCheckDate", new Date().toString())
            .up()
            .build();
    }
}

答案 1 :(得分:1)

我已删除AbstractHealthChecker以直接实现HealthCheck界面,现在可以正常使用。 另外,我有一个健康的HealthCheck实现,它似乎与@Dependent annoatation无关。用@ApplicationScoped注释两者都可以。

所以现在我有:

@Health
@ApplicationScoped
public class SystemHealthChecker extends AbstractHealthChecker {

  @Override
  public HealthCheckResponse call() {
    boolean up;
    Runtime runtime = Runtime.getRuntime();
    long freeMemory = runtime.freeMemory();
    long totalMemory = runtime.totalMemory();
    double percentageFree = freeMemory*100/totalMemory;
    if(percentageFree < 10.0){
      up = false;
    }else{
      up = true;
    }

    Map<String, String> attributes = new HashMap<>();
    attributes.put("Total Memory", totalMemory+"");
    attributes.put("Free Memory", freeMemory+"");
    attributes.put("Percentage Free Memory", percentageFree+"%");
    HealthCheckResponse response = getResponse("System-Check", attributes, up);
    return response;
  }

  private HealthCheckResponse getResponse(String name, Map<String, String> data, boolean up){
    HealthCheckResponseBuilder responseBuilder = HealthCheckResponse.named(name);

    responseBuilder.state(up);

    for(String key: data.keySet()){
        responseBuilder.withData(key, data.get(key));
    }
    responseBuilder.withData("Date", new Date().toString());

    return responseBuilder.build();
  }
}

它有效。