Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将dropwizards注释与Spring Boot一起使用_Java_Spring Boot_Metrics_Codahale Metrics_Spring Boot Actuator - Fatal编程技术网

Java 将dropwizards注释与Spring Boot一起使用

Java 将dropwizards注释与Spring Boot一起使用,java,spring-boot,metrics,codahale-metrics,spring-boot-actuator,Java,Spring Boot,Metrics,Codahale Metrics,Spring Boot Actuator,我打算使用提供的@Timed注释,而不是通过在每个感兴趣的方法中插入代码来进行测量。但这些指标没有显示任何相应的值: 这是我的代码,想法是将包含的SQL的执行时间放入度量中 @Component public class Foo { private JdbcTemplate jdbcTemplate; @Autowired public Metadata(JdbcTemplate jdbcTemplate) { this.jdbcTemplate =

我打算使用提供的
@Timed
注释,而不是通过在每个感兴趣的方法中插入代码来进行测量。但这些指标没有显示任何相应的值:

这是我的代码,想法是将包含的SQL的执行时间放入度量中

@Component
public class Foo {
    private JdbcTemplate jdbcTemplate;

    @Autowired
    public Metadata(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Timed(name = "myapp.get.foo")
    public boolean getFoo(final String foo ) {
        String foo = jdbcTemplate.query( ...
    }
}
@Timed
未显示的问题可能是因为

但是
@仪表
@计量
@计数
也不起作用


为了使Spring Boot支持的度量标注至少能够正常工作,我缺少了什么?(1.3.1在我的测试中)

您可能想看看

此项目简化了dropwizard指标与Spring Boot项目的集成。
它将自动创建度量和代理bean,以使@Timed注释正常工作。

我最终没有使用注释,只是在我想要插入的bean上实现了度量集。特别是对于计时器,使用计时器并不难

@Service
public class MyBean implements MetricSet {
    Timer putTimer = new Timer();

    public void someService() {
       try(Context context = putTimer.time()) {
          ....
       }
    }

    @Override
    public Map<String, Metric> getMetrics() {
        Map<String,Metric> metrics=new HashMap<>();
        metrics.put("mymetricname",putTimer);
        return metrics;
    }
}

@Slf4j
@Service
public class MetricsContextListener implements ApplicationListener<ContextRefreshedEvent> {

    private final MetricRegistry metricRegistry;

    public MetricsContextListener(MetricRegistry metricRegistry) {
        this.metricRegistry = metricRegistry;
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        Map<String, MetricSet> beans = event.getApplicationContext().getBeansOfType(MetricSet.class);
        for(Map.Entry<String, MetricSet> e: beans.entrySet()) {
            log.info("registering " + e.getKey() + " " + e.getValue().getClass().getName());
            metricRegistry.register(e.getKey(), e.getValue());
        }
    }
}
所有这些就绪后,SpringBoot做了正确的事情,并将度量添加到/metrics中

@Bean
public MetricRegistry metricRegistry() {
    // explicitly register a metricRegistry so we can run our spring tests without relying on spring boot creating
    // one for us. Spring boot seems to do the right thing either way.
    // Used to register codahale metrics from MetricSet implementing beans.
    return new MetricRegistry();
}