Wildfly-Swarm Microprofile HealthCheck 没有响应

Wildlfy-Swarm Microprofile HealthCheck not responding

我正在尝试在我的 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 端点上的系统检查也没有真正的输出,我只得到以下内容

{
  "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>

在不知道 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();
    }
}

我删除了 AbstractHealthChecker 以直接实现 HealthCheck 接口,现在可以使用了。 我还有一个第二个 HealthCheck 实现,它似乎不适用于 @Dependent 注释。用 @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();
  }
}

而且有效。