springbootPrometheusgrafana
1。SpringBoot工程集成Micrometer1。1引入依赖dependencygroupIdorg。springframework。bootgroupIdspringbootstarteractuatorartifactIddependencydependencygroupIdio。micrometergroupIdmicrometerregistryprometheusartifactIddependency1。2配置management。server。port9003management。endpoints。web。exposure。includemanagement。endpoint。metrics。enabledtruemanagement。endpoint。health。showdetailsalwaysmanagement。endpoint。health。probes。enabledtruemanagement。endpoint。prometheus。enabledtruemanagement。metrics。export。prometheus。enabledtruemanagement。metrics。tags。applicationvoiceqcbackend
这里management。endpoints。web。exposure。include配置为开启Actuator服务,因为SpringBootActuator会自动配置一个URL为actuatorPrometheus的HTTP服务来供Prometheus抓取数据,不过默认该服务是关闭的,该配置将打开所有的Actuator服务。
management。metrics。tags。application配置会将该工程应用名称添加到计量器注册表的tag中去,方便后边Prometheus根据应用名称来区分不同的服务。1。3监控jvm信息
然后在工程启动主类中添加Bean如下来监控JVM性能指标信息:SpringBootApplicationpublicclassGatewayDatumApplication{publicstaticvoidmain(String〔〕args){SpringApplication。run(GatewayDatumApplication。class,args);}BeanMeterRegistryCustomizerMeterRegistryconfigurer(Value({spring。application。name})StringapplicationName){return(registry)registry。config()。commonTags(application,applicationName);}}1。4创建自定义监控
监控请求次数与响应时间packagecom。lianxin。gobot。api。monitor;importio。micrometer。core。instrument。Counter;importio。micrometer。core。instrument。MeterRegistry;importio。micrometer。core。instrument。Timer;importlombok。Getter;importorg。springframework。beans。factory。annotation。Autowired;importorg。springframework。beans。factory。annotation。Value;importorg。springframework。stereotype。Component;importjavax。annotation。PostConstruct;Author:GZCreateTime:2022083010:50Description:自定义监控服务Version:1。0ComponentpublicclassPrometheusCustomMonitor{上报拨打请求次数GetterprivateCounterreportDialRequestCount;上报拨打URLValue({lx。callresultreport。url})privateStringcallReportUrl;上报拨打响应时间GetterprivateTimerreportDialResponseTime;GetterprivatefinalMeterRegistryregistry;AutowiredpublicPrometheusCustomMonitor(MeterRegistryregistry){this。registryregistry;}PostConstructprivatevoidinit(){reportDialRequestCountregistry。counter(goapireportdialrequestcount,url,callReportUrl);reportDialResponseTimeregistry。timer(goapireportdialresponsetime,url,callReportUrl);}}1。5添加具体业务代码监控统计请求次数prometheusCustomMonitor。getReportDialRequestCount()。increment();longstartTimeSystem。currentTimeMillis();StringcompanyHttpUtils。post(companyUrl,);统计响应时间longendTimeSystem。currentTimeMillis();prometheusCustomMonitor。getReportDialResponseTime()。record(endTimestartTime,TimeUnit。MILLISECONDS);
在浏览器访问http:127。0。0。1:9001actuatorprometheus,就可以看到服务的一系列不同类型metrics信息,例如jvmmemoryusedbytesgauge、jvmgcmemorypromotedbytestotalcounter,goapireportdialrequestcount等
到此,SpringBoot工程集成Micrometer就已经完成,接下里就要与Prometheus进行集成了。2。集成Prometheus2。1安装bashdockerpullpromprometheusbashmdkirusrlocalprometheusbashviprometheus。ymlmyglobalconfigglobal:scrapeinterval:15sSetthescrapeintervaltoevery15seconds。Defaultisevery1minute。evaluationinterval:15sEvaluaterulesevery15seconds。Thedefaultisevery1minute。scrapetimeoutissettotheglobaldefault(10s)。Alertmanagerconfigurationalerting:alertmanagers:staticconfigs:targets:alertmanager:9093Loadrulesonceandperiodicallyevaluatethemaccordingtotheglobalevaluationinterval。rulefiles:firstrules。ymlsecondrules。ymlAscrapeconfigurationcontainingexactlyoneendpointtoscrape:HereitsPrometheusitself。scrapeconfigs:Thejobnameisaddedasalabeljobjobnametoanytimeseriesscrapedfromthisconfig。jobname:prometheusmetricspathdefaultstometricsschemedefaultstohttp。staticconfigs:targets:〔192。168。136。129:9090〕bashdockerrundnameprometheusp9090:9090vusrlocalprometheusprometheus。yml:etcprometheusprometheus。ymlpromprometheus
2。2集成配置global:scrapeinterval:15sscrapeconfigs:jobname:prometheusstaticconfigs:targets:〔localhost:9090〕jobname:metricsLocalTestmetricspath:actuatorprometheusstaticconfigs:targets:〔localhost:9003〕
这里localhost:9001就是上边本地启动的服务地址,也就是Prometheus要监控的服务地址。同时可以添加一些与应用相关的标签,方便后期执行PromSQL查询语句区分。最后重启Prometheus服务
3。使用GrafanaDashboard展示监控项3。1安装grafanabashdockerpullgrafanagrafanabashdockerrundnamegrafanap3000:3000vusrlocalgrafana:varlibgrafanagrafanagrafana
默认用户名密码adminadmin
3。2配置prometheus数据源
3。3增加jvm面板
模板编号为4701
3。4配置业务接口监控面板