栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用弹簧护套2和千分尺测量维修方法

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用弹簧护套2和千分尺测量维修方法

这是一个小样本,应该可以帮助您。这里

Timer.record()
没有显示更多的变体。(此外:字段注入仅出于简洁目的。)您不必将调用的方法名称放入标签中。您也可以使其成为度量标准名称本身的一部分。只是想展示您可以做什么。

更新2018年3月12日: 中作为

Micrometer1.0.0
一个
TimedAspect
已经出台,让你也可以使用
@Timed
注解。现在,您需要注册
Bean
自己。(尽管在
@Timed
Spring-MVC或Jersey资源上具有自定义注释时,您必须要谨慎。)[MichalStepan]在后续回答中已经提到了这一点。

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

如果您选择普罗米修斯,您最终会得到类似的东西:

# HELP myservice_seconds  # TYPE myservice_seconds summarymyservice_seconds_count{application="micrometered",method="manual",} 4.0myservice_seconds_sum{application="micrometered",method="manual",} 0.200378014myservice_seconds_max{application="micrometered",method="manual",} 0.050115291myservice_seconds_count{application="micrometered",method="supplier",} 4.0myservice_seconds_sum{application="micrometered",method="supplier",} 0.200393455myservice_seconds_max{application="micrometered",method="supplier",} 0.05011635myservice_seconds_count{application="micrometered",method="sample",} 4.0myservice_seconds_sum{application="micrometered",method="sample",} 0.200527005myservice_seconds_max{application="micrometered",method="sample",} 0.050250191# HELP myservice_aspect_seconds  # TYPE myservice_aspect_seconds summarymyservice_aspect_seconds_count{application="micrometered",,method="helloAspect",} 4.0myservice_aspect_seconds_sum{application="micrometered",,method="helloAspect",} 0.201824272myservice_aspect_seconds_max{application="micrometered",,method="helloAspect",} 0.051014296


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/441006.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号