如何使用弹簧靴2和千分尺测量服务方法

时间:2018-02-09 11:25:45

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

我在Spring Boot 2(RC1)上开始了我的第一个项目。感谢已经很好的文档,这并不是来自Spring Boot 1.x。

但是现在我想要整合指标我绊倒了。据我目前所知,目前只有默认情况下发布的指标文档。但是我还要测量服务水平执行时间以及dynamodb中使用的时间。

修改 我正在寻找使用Micrometer的解决方案,Micrometer是弹簧启动2附带的新执行器库中使用的库。

有没有关于如何做到这一点的指南?从this开始,我读到没有基于注释的简单解决方案来处理任意春豆。可以s.o.给我一个示例/链接到文档,说明如何计量下面的方法?

@Service
@Timed
public class MyService {
    public void doSomething() {
        ...;
    }
}

2 个答案:

答案 0 :(得分:14)

由于范围缩小,

@io.micrometer.core.annotation.Timed注释似乎与自定义调用无关,提到in link in your question

您需要手动设置Aspect:

@Configuration
@EnableAspectJAutoProxy
public class AutoTimingConfiguration {
    @Bean
    public TimedAspect timedAspect(MeterRegistry registry) {
        return new TimedAspect(registry);
        }
}

这种方法是这样的:

@Timed("GET_CARS")
public List<Car> getCars(){
        return Lists.newArrayList();
}

将在GET_CARS(默认)端点中生成/actuator/metrics指标。

答案 1 :(得分:12)

这是一个小样本,可以帮助你。 <nav class="navbar fixed-top navbar-expand-lg navbar-dark bg-dark justify-content-center"> .... </nav> 有更多变种,这里没有显示。 (另外:现场注入仅用于简洁。) 您不必将被调用的方法名称放入标记中。您还可以将其作为度量标准名称本身的一部分。只是想表明你能做些什么。

更新2018-03-12:Timer.record()起,我们引入了Micrometer 1.0.0,您也可以使用TimedAspect注释。现在您需要自己注册@Timed。 (当您在Spring-MVC或Jersey资源上有自定义Bean注释时,您需要谨慎。)Michal Stepan已在后续answer中提及此问题。

@Timed

如果你去普罗米修斯,你最终会得到类似的东西:

package io.github.mweirauch.micrometered.eval;

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import io.micrometer.core.annotation.Timed;
import io.micrometer.core.aop.TimedAspect;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import io.micrometer.core.instrument.Timer.Sample;

@Configuration
@EnableAspectJAutoProxy
public class TimingStuff {

    @Service
    static class MyService {

        @Autowired
        private MeterRegistry registry;

        public void helloManual() {
            // you can keep a ref to this; ok to call multiple times, though
            Timer timer = Timer.builder("myservice").tag("method", "manual").register(registry);

            // manually do the timing calculation
            long start = System.nanoTime();
            doSomething();
            timer.record(System.nanoTime() - start, TimeUnit.NANOSECONDS);
        }

        public void helloSupplier() {
            Timer timer = Timer.builder("myservice").tag("method", "supplier").register(registry);

            // execution of the method is timed internally
            timer.record(() -> doSomething());
        }

        public void helloSample() {
            Timer timer = Timer.builder("myservice").tag("method", "sample").register(registry);

            // records time taken between Sample creation and registering the
            // stop() with the given Timer
            Sample sample = Timer.start(registry);
            doSomething();
            sample.stop(timer);
        }

        // TimedAspect adds "class" and "method" tags
        @Timed(value = "myservice.aspect")
        public void helloAspect() {
            doSomething();
        }

        private void doSomething() {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                //
            }
        }

    }

    @Autowired
    private MyService myService;

    @Bean
    TimedAspect timedAspect(MeterRegistry registry) {
        return new TimedAspect(registry);
    }

    @Scheduled(fixedRate = 1000)
    public void postConstruct() {
        myService.helloManual();
        myService.helloSupplier();
        myService.helloSample();
        myService.helloAspect();
    }

}