【问题标题】:Wildlfy-Swarm Microprofile HealthCheck not respondingWildfly-Swarm Microprofile HealthCheck 没有响应
【发布时间】:2018-01-09 14:42:10
【问题描述】:

我正在尝试在我的 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>

【问题讨论】:

标签: health-monitoring wildfly-swarm


【解决方案1】:

如果不知道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 的代码了,我已经解决了。我看到的唯一区别是您拥有@ApplicationScoped,而我拥有@Dependent。我用@ApplicationScoped试过了,没有效果。
  • 但是,你告诉我,我忘记了 AbstractHealthCheck 给了我这个想法,将它完全排除在外,现在它可以工作了。似乎接口和 Bean 之间的额外层不足以让 swarm 将其注入健康端点。所以谢谢你:)
【解决方案2】:

我已经删除了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();
  }
}

它有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-04
    • 2017-07-28
    • 2016-07-09
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多