【问题标题】:Is it possible to declare multiple micrometer binders in a single bean declaration是否可以在单个 bean 声明中声明多个微米粘合剂
【发布时间】:2019-11-10 19:59:52
【问题描述】:

默认情况下,来自所有 MeterBinder bean 的指标将自动绑定到 Spring 管理的 MeterRegistry

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

Declarable 对象的集合;用于使用集合的单个 bean 声明在代理上声明多个对象。

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

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

目前,为了将这些概念分开,我正在使用额外的配置类,这在我看来并不像惯用的弹簧方式

@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));
    }

}

我可以做得更好吗?

【问题讨论】:

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


    【解决方案1】:

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

    @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);
        };
    }
    

    【讨论】:

    • 谢谢,这就是我要找的。还没想过用binder的功能接口
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-04
    • 2021-08-24
    • 1970-01-01
    • 2020-03-10
    • 1970-01-01
    • 1970-01-01
    • 2012-02-28
    相关资源
    最近更新 更多