【发布时间】:2020-04-13 09:45:55
【问题描述】:
问题说明: 将 Spring Boot 应用程序的延迟、错误率、吞吐量、重试次数(用于持续到 SQL DB 失败和 Kafka 发布失败)和 Unsuccessful-Retries 作为 ManagedBeans 公开控制台。此应用程序中没有 REST 端点。它通过 Kafka(使用 Spring StreamListener)纯粹是异步事件驱动的。
由于生产级别的许可问题,我无法使用 Prometheus。
我的方法:创建一个 MetricsBean 类(实现一个接口),基本上包含上述指标的 POJO:
import org.springframework.context.annotation.Lazy;
import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedResource;
@Lazy(false)
@ManagedResource
public class ConfigMBean implements IConfigMBean {
private Double latency;
private Double errorRate;
private Double throughput;
private Long retryCount;
private Long unsuccessfulRetries;
public GatewayConfigMBean(Double latency, Double errorRate, Double throughput, Long retryCount,
Long unsuccessfulRetries) {
super();
this.latency = latency;
this.errorRate = errorRate;
this.throughput = throughput;
this.retryCount = retryCount;
this.unsuccessfulRetries = unsuccessfulRetries;
}
public GatewayConfigMBean() {
}
@ManagedAttribute
@Override
public Double getLatency() {
return latency;
}
@ManagedAttribute
@Override
public Double getErrorRate() {
return errorRate;
}
@ManagedAttribute
@Override
public Double getThroughput() {
return throughput;
}
@ManagedAttribute
@Override
public Long getRetryCount() {
return retryCount;
}
@ManagedAttribute
@Override
public Long getUnsuccessfulRetries() {
return unsuccessfulRetries;
}
@ManagedOperation
@Override
public String showMetrics() {
StringBuilder builder = new StringBuilder();
builder.append("GatewayConfigMBean [errorRate=").append(errorRate).append(", latency=").append(latency)
.append(", retryCount=").append(retryCount).append(", throughput=").append(throughput)
.append(", unsuccessfulRetries=").append(unsuccessfulRetries).append("]");
return builder.toString();
}
}
然后我在 Config 类中暴露了这个 Bean:
@Bean("My_Microservice_Name")
public ConfigMBean configMBean () {
return new ConfigMBean ();
}
我能够在 JConsole MBeans 选项卡上看到 My_Microservice_Name,但正如预期的那样,所有值都是空的。
问题:如何使用 Spring Micrometer 为这些指标分别分配正确的值?另外,如果我需要使用某种 AOP 来获取这些指标,我需要如何将其合并到我的代码中?
我在互联网上搜索并找到了一些点击:
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Metrics;
private Counter counter = Metrics.counter("ErrorCount");
counter.increment();
private Timer timer = Metrics.timer("Latency");
// Assume startTime was declared earlier
timer.record(System.nanoTime() - startTime, TimeUnit.NANOSECONDS);
但不确定如何获取其他指标并将这些值设置为 ConfigMBean 类? 假设所有依赖项都可以在 Spring 中使用。 在上述问题上需要帮助,如果还需要任何其他信息,请注明。
【问题讨论】:
标签: java spring spring-boot micrometer