java - 是否可以在单个 bean 声明中声明多个微米绑定(bind)器

标签 java spring-boot spring-boot-actuator micrometer

By default, metrics from all MeterBinder beans will be automatically bound to the Spring-managed MeterRegistry

当可以单独配置 Binder 时,这非常有用。然而,我正在寻找的是能够批量声明多个 Binder 。我的用例基于以编程方式声明和创建多个兔子队列。 spring-amqp 项目使用了 Declarables 类的概念,它是:

A collection of Declarable objects; used to declare multiple objects on the broker using a single bean declaration for the collection.

spring-boot 中是否存在类似的概念,我将其称为 MeterBinders,它允许我使用单个 bean 声明来声明多个计量对象。

我想避免同时声明我的队列和计量表(使用 new QueueSizeMeterBinder(queueInformation).bindTo(meterRegistry) - 将它们分开)。

目前,为了将这些概念分开,我正在使用额外的配置类,在我看来,它不是惯用的 spring-way

@Configuration
@RequiredArgsConstructor
class QueueMetricsConfiguration {

    private final Declarables queues;
    private final AmqpAdmin amqpAdmin;
    private final MeterRegistry meterRegistry;

    @PostConstruct
    public void bindMetrics() {
        queues.getDeclarablesByType(Queue.class)
                .forEach(queue -> new QueueSizeMeterBinder(amqpAdmin.getQueueInfo(queue.getName()))
                        .bindTo(meterRegistry));
    }

}

我可以做得更好吗?

最佳答案

我不确定我的解释是否正确,但在一个 MeterBinder 中注册多个绑定(bind)将是我在项目中称为复合绑定(bind)器的东西:

@Bean
MeterBinder compositeBinder() {
    return (registry) -> {
        // register Meters via a binder
        new FooBinder().bindTo(registry);
        new BarBinder().bindTo(registry);
        // register without indirection
        registry.gauge("mygauge", BigDecimal.TEN);
    };
}

关于java - 是否可以在单个 bean 声明中声明多个微米绑定(bind)器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58792428/

相关文章:

java - 在 mysql jdbc 中自动生成 key

spring-boot - 在 Spring Oauth2 中避免使用 JDBCTokenStore

java - 使用 New Relic 的 Spring Boot 无需修改依赖项

spring-boot - 普罗米修斯执行器的自定义路径

java - 如何终止 JavaFx 中的服务或任务?

java - 获取以下代码片段的 java NPE。不确定出了什么问题

java - 如何安排任务运行一次?

java - Spring Boot传递局部变量

java - IllegalStateException : org. springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@xxxxxxxx 已关闭

java - [Spring][Actuators] 无法处理配置类的导入候选