Spring Boot执行器 – Java端点

huangapple go评论74阅读模式
英文:

Spring Boot Actuator - Java Endpoint

问题

在我们的基础设施模型中,我们有一个样例执行器健康检查,我们正在主服务中使用它(将依赖项添加到包中,@Import 类名等)。

我们知道关于 HTTP 端点,通过添加后缀 /actuator/health,以下是结果:

{
	"status": "UP",
	"components": {
		"com......DbServiceValidator": {
			"status": "UP"
		},
		"com......StorageServiceValidator": {
			"status": "UP"
		},
		"diskSpace": {
			"status": "UP",
			"details": {
				"total": 505370734592,
				"free": 362675372032,
				"threshold": 10485760,
				"exists": true
			}
		},
		"ping": {
			"status": "UP"
		}
	}
}

但我们想要通过 Java 代码以编程方式获取所有这些数据。我进行了一些研究,但没有找到任何信息。

是否有可能直接从代码中获取数据(无需使用 REST 等)?

英文:

We have a sample actuator health check in our infrastructure model which we are consuming in our main service (adding dependencies to the package, @Import class name, etc.).

We know about the http endpoint, by adding the suffix /actuator/health, below is the result:

{
	"status": "UP",
	"components": {
		"com......DbServiceValidator": {
			"status": "UP"
		},
		"com......StorageServiceValidator": {
			"status": "UP"
		},
		"diskSpace": {
			"status": "UP",
			"details": {
				"total": 505370734592,
				"free": 362675372032,
				"threshold": 10485760,
				"exists": true
			}
		},
		"ping": {
			"status": "UP"
		}
	}
}

But we want to get all this data programmatically, from java code.
I did some research but found nothing.

Is it possible to get the data (/json) directly from the code (without rest, etc.)?

答案1

得分: 3

有一些由Spring Boot自动配置的名为HealthIndicators的bean,完整列表在这里

可以像这样使用它:

@Autowired
DiskSpaceHealthIndicator diskSpaceHealthIndicator;

void getDiskStatus() {
    logger.info("当前磁盘状态 {}", diskSpaceHealthIndicator.health().getStatus());
}

编辑:

受到@MarkBramnik的答案启发,我验证了可以注入HealthEndpoint并将结果打印为JSON:

@Autowired
private HealthEndpoint healthEndpoint;

public void printHealthStatus() {
    ObjectMapper mapper = new ObjectMapper();
    log.info(mapper.writeValueAsString(healthEndpoint.health().getStatus()));
    // 输出为:{"status":"UP"}
}
英文:

There are some auto-configured beans by Spring Boot called HealthIndicators, full list here.

Should be possible to use it like this:

@Autowired
DiskSpaceHealthIndicator diskSpaceHealthIndicator;

void getDiskStatus() {
    logger.info("Current Disk status {}", diskSpaceHealthIndicator.health().getStatus());
}

Edit:

Inspired by the answer from @MarkBramnik I verified that is possible to inject the HealthEndpoint and print the result as JSON:

@Autowired
private HealthEndpoint healthEndpoint;

public void printHealthStatus() {
	ObjectMapper mapper = new ObjectMapper();
	log.info(mapper.writeValueAsString(healthEndpoint.health().getStatus()));
    // output is: {"status":"UP"}
}

答案2

得分: 2

总之,您可以采取以下策略:

  1. 使用JMX。Spring Boot的执行器除了通过REST暴露端点外,还可以通过JMX暴露端点。
    有关详细信息,请参阅文档。因此,您可以编写代码以以编程方式访问“health”MBean并获取聚合信息。

  2. 尝试将HealthEndpoint注入(它应该是一个bean,尽管我自己从未尝试过)到您的类中,并通过直接调用来自服务器的对象(就像是常规类一样)来获取有关健康状况的信息 - 您将获得从JSON中逻辑地表示信息的对象。健康端点的源代码可在此处找到。

英文:

All-in-all there are following strategies you can follow:

  1. Use JMX. Spring Boot actuator can also expose endpoints via JMX in addition to REST.
    See the documentation. So you could write a code that programmatically accesses the "health" mbean and gets the aggregated information.

  2. Try to inject the HealthEndpoint (it should be a bean, although I've never tried that by myself) into your class and obtain the information about the health by directly calling the object from your server (as if its a regular class) - you'll get the object that logically represents the information from the JSon. The source code of the health endpoint is available here

huangapple
  • 本文由 发表于 2020年9月8日 13:32:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63787646.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定