【发布时间】:2020-03-22 13:42:45
【问题描述】:
我正在编写一个 Web UI 来监控 Spring Boot 中的另一个 Java 应用程序,由 Spring Actuator 支持。
现在我可以使用常规 Get 请求(通过 Spring WebFlux 的 WebClient.get() 方法)从其他客户端 Sprint Boot Web 应用程序轮询 Spring Actuator 的端点。
但是,如果某些数据发生变化,比如说指标端点之一(即 CPU 使用率总是在变化),我必须“刷新”get 请求。我可以通过@Scheduled 注解将它设置在预定的计时器上,但我觉得有更好的方法吗?
我知道如何从不断流动的数据的 get 请求中检索 Flux,但是在服务器上,必须创建一个 Flux。 Spring Boot Actuator 可以提供一个 Flux 的指标,以便我可以流式传输它吗?
这里有一些代码,我目前通过每 500 毫秒 ping 一次的预定方法调用此方法。
private Flux<Health> getHealth(String baseUrl) {
var fallbackValue = new Health();
fallbackValue.setStatus("Unknown");
return webClient.get().uri(baseUrl + "/actuator/health").retrieve().bodyToFlux(Health.class)
.onErrorReturn(fallbackValue);
}
但只有当我调用它并使用预定的注释总是刷新它时它才会更新:
@Scheduled(fixedDelay = 500) // I want to remove this, instead of refreshing, I want the data to stream
public void pollCapHandlers() {
getHealth("http://localhost:8080").subscribe(health -> {
ui.access(() -> {
healthStatusTextField.setValue(health.getStatus()); // this doesn't update in real time
});
})
【问题讨论】:
标签: spring-boot spring-webflux server-sent-events spring-boot-actuator spring-webclient