当前位置: 首页 > 知识库问答 >
问题:

在spring boot 2中配置micrometer-registry-statsd

冯驰
2023-03-14

我花了几天的时间,不能让它工作,在Spring的仪器新。

我有一个spring Boot2应用程序。在pom.xml中,我定义了:

<dependency>
  <groupId>io.micrometer</groupId>
  <artifactId>micrometer-registry-statsd</artifactId>
  <version>1.1.5</version>
</dependency>

application.conf中:

management.metrics.export.statsd.host=localhost
management.metrics.export.statsd.port=8125
management.metrics.export.statsd.flavor=etsy
management.metrics.export.statsd.step=2m
management.metrics.export.statsd.enabled=true
management.endpoints.web.exposure.include=health,metrics

在应用程序中,当它启动时,我想导出一个新的度量(计数器):

@SpringBootApplication
public class MyApplication {

  private static final Logger LOG = LoggerFactory.getLogger(MyApplication.class);

  private static final StatsdConfig config = new StatsdConfig() {
    @Override
    public String get(String k) { return null; }
    @Override
    public StatsdFlavor flavor() { return StatsdFlavor.ETSY; }
  };

  private static final MeterRegistry registry = new StatsdMeterRegistry(config, Clock.SYSTEM);

  public static void main(String[] args) {
    // globalRegistry is composite hence was hoping they will unite into one
    Metrics.globalRegistry.add(registry);

    Counter myCounter = Counter
        .builder("myCounter")
        .description("indicates instance count of the object")
        .tags("dev", "performance")
        .register(registry);
//      .register(Metrics.globalRegistry);

    myCounter.increment(2.0);
    LOG.info("Counter: " + myCounter.count());
    SpringApplication.run(MyApplication.class, args);
  }

}

我想要的是让我的自定义注册表包含跨应用程序定义的自定义度量,并在度量endpoint下正确注册和可用,然后它可以导出到statsd。你知道我在上面遗漏了什么吗?

我查阅了这些文件https://www.baeldung.com/micrometer和https://micrometer.io/docs/registry/statsd。如何为我的代码创建一个bean,或者如何使用Spring Boot自动配置的注册表?

共有1个答案

姚胡媚
2023-03-14

Spring Boot的测微计自动配置将自动调用任何MeterBinderbean来将它们的测微计绑定到自动配置的MeterRegistry。通过对类路径的必要的StatsD依赖关系(您已经拥有了这些依赖关系),这将是一个基于StatsD的注册表。我建议使用这种自动配置,而不是自己配置。就目前情况而言,您将同时拥有一个自动配置的注册表和您自己的注册表。如果您将注册表公开为Spring bean,那么自动配置的注册表将会退出并且不会被创建。

我建议删除statsdconfigstatsdmeterregistry,改用自动配置。然后可以使用MeterBinderbean绑定计数器。这将使应用程序的主类看起来如下所示:

@SpringBootApplication
public class MyApplication {

    @Bean
    public MeterBinder exampleMeterBinder() {
        return (meterRegistry) -> Counter.builder("myCounter")
            .description("indicates instance count of the object")
            .tags("dev", "performance")
            .register(meterRegistry);
    }

    public static void main(String[] args) {
        SpringApplication.run(MyApplication, args);
    }

}
 类似资料:
  • Micrometer 是一款监控指标的度量类库,可以让您在没有供应商锁定的情况下对 JVM 的应用程序代码进行调整。

  • 是否可以在restendpoint中公开所有的Spring度量? 我使用的是spring Boot2.2.2并添加了这个依赖项 application.yml

  • 我正在尝试使用 https://github.com/spring-projects/spring-security-oauth2-boot 使用本教程:https://docs.spring.io/spring-security-oauth2-boot/docs/current-SNAPSHOT/reference/htmlsingle/ SpringBoot应用程序 服务器初始化器 用户详细信

  • 允许将自定义的注册表插入到任务系统中,以期提供共享任务或增强功能。 注意: 只有用 task() 方法注册的任务才会进入自定义注册表中。直接传递给 series() 或 parallel() 的任务函数(task functions)不会进入自定义任务注册表 - 如果你需要自定义注册表的行为,请通过字符串引用的方式将任务(task)组合在一起。 分配新注册表时,将传输当前注册表中的每个任务,并将用

  • �� npm registry documentation A collection of archived documentation about registry endpoints/API If you're having issues with the website or registry itself pleasecontact npm's support staff.

  • 《分布式服务注册中心 XXL-REGISTRY》 为方便项目维护,该项目已经合并至 XXL-RPC :https://www.oschina.net/p/xxl-rpc