简体   繁体   English

如何从Spring Boot Health端点获取某些字段

[英]How to get certain fields from spring boot health endpoint

I have successfully created a springboot app that returns all the basic endpoints. 我已经成功创建了一个springboot应用程序,该应用程序返回所有基本端点。 Now I want to return just few fields from that endpoint in my request. 现在,我想在请求中仅返回该端点的几个字段。 For instance, return status from /health page to my rest call. 例如,将状态从/ health页面返回到我的休息呼叫。 How do I filter this or make my rest call more specific? 我该如何过滤或让我的休息电话更具体?

The actual requirement is two return few fields from /env, /health of different apps in one call. 实际要求是在一次调用中从/ env,/ app的/ health返回两个字段。 For which I am able to do it by returning all fields for both env and health. 为此,我可以通过返回env和health的所有字段来做到这一点。 I just need to return specific fields from them. 我只需要从中返回特定字段。 Also can I use in memory json objects, if so how should I do it? 我还可以在内存中使用json对象,如果要怎么做呢?

Finally I figured out as how to create it. 最后,我弄清楚了如何创建它。 So the incoming json object consists of fields in LinkedHashMap type. 因此,传入的json对象由LinkedHashMap类型的字段组成。 So I consumed its field values using key 所以我用键消耗了它的字段值

LinkedHashMap response = (LinkedHashMap)restTemplate.getForObject("http://localhost:8080/env",Object.class);
EnvProperties variables = new EnvProperties (response);

Wrapper POJO for all fields, 包装POJO适用于所有领域,

public EnvProperties (LinkedHashMap body) {
   this.sysProperties = new SysEnvProperties((LinkedHashMap) body.get("systemProperties"));
 }

POJO for this field, 这个领域的POJO,

 public SysEnvProperties(LinkedHashMap body) {
   this.javaVersion = body.get("java.version").toString();
}

later creating a new json string 稍后创建一个新的json字符串

 @Override
 public String toString() {
  String s = null;
    try {
        s = mapper.writeValueAsString(this);
      } catch (JsonProcessingException e) {
          e.printStackTrace();
        }
      return s;
   }

I repeated the same for fields of interest, creating a POJO for each. 我对感兴趣的字段重复了相同的操作,为每个字段创建了一个POJO。 Finally called these fields using similar wrapper class whose toString method returned the expected json object of desired fields only. 最后使用类似的包装器类调用了这些字段,该类的toString方法仅返回所需字段的预期json对象。

You can create Custom health endpoint or custom heath checker too. 您也可以创建自定义运行状况终结点或自定义健康检查程序。

For eg 例如

@Component
public class CustomHealthCheck extends AbstractHealthIndicator {
    @Override
    protected void doHealthCheck(Health.Builder bldr) throws Exception {
        // TODO implement some check
        boolean running = true;
        if (running) {
          bldr.up();
        } else {
          bldr.down();
        }
    }
}

For further reading: 进一步阅读:

  1. http://www.christianmenz.ch/programmieren/spring-boot-health-checks/ http://www.christianmenz.ch/programmieren/spring-boot-health-checks/
  2. http://briansjavablog.blogspot.be/2017/09/health-checks-metric-s-more-with-spring.html http://briansjavablog.blogspot.be/2017/09/health-checks-metric-s-more-with-spring.html
  3. http://www.baeldung.com/spring-boot-actuators http://www.baeldung.com/spring-boot-actuators

You can find a tutorial here . 您可以在此处找到教程 However, the interfaces you want to look into implementing are: 但是,您要考虑实现的接口是:

org.springframework.boot.actuate.endpoint.Endpoint

Similar to creating a Controller. 类似于创建控制器。 This is your /custom-health endpoint. 这是您的/custom-health端点。

org.springframework.boot.actuate.metrics.CounterService

You can count integer-value metrics which will be available at /metrics . 您可以计算/metrics可用的整数值/metrics

org.springframework.boot.actuate.metrics.GaugeService

Or, you can measure double-value metrics which will be available at /metrics . 或者,您可以测量/metrics上可用的双值/metrics

org.springframework.boot.actuate.health.HealthIndicator

Add metrics to the /health endpoint. 将指标添加到/health端点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM