南京做网站的公司排名,做房地产信息网怎么做,wordpress站群 企业,网站如何布局本文介绍SpringBoot如何使用Prometheus配合Grafana监控。 1.关于Prometheus 
Prometheus是一个根据应用的metrics来进行监控的开源工具。相信很多工程都在使用它来进行监控#xff0c;有关详细介绍可以查看官网#xff1a;https://prometheus.io/docs/introduction/overview/… 本文介绍SpringBoot如何使用Prometheus配合Grafana监控。 1.关于Prometheus 
Prometheus是一个根据应用的metrics来进行监控的开源工具。相信很多工程都在使用它来进行监控有关详细介绍可以查看官网https://prometheus.io/docs/introduction/overview/。 
2.有关Grafana 
Grafana是一个开源监控利器如图所示。 从图中就可以看出来使用Grafana监控很高大上提供了很多可视化的图标。 
官网地址https://grafana.com/ 
3.SpringBoot使用Prometheus 
3.1 依赖内容 
在SpringBoot中使用Prometheus其实很简单不需要配置太多的东西在pom文件中加入依赖完整内容如下所示。 
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.1.3.RELEASE/versionrelativePath/ !-- lookup parent from repository --/parentgroupIdcom.dalaoyang/groupIdartifactIdspringboot2_prometheus/artifactIdversion0.0.1-SNAPSHOT/versionnamespringboot2_prometheus/namedescriptionspringboot2_prometheus/descriptionpropertiesjava.version1.8/java.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdio.micrometer/groupIdartifactIdmicrometer-registry-prometheus/artifactIdversion1.1.3/version/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/project 
3.2 配置文件 
配置文件中加入配置这里就只进行一些简单配置management.metrics.tags.application属性是本文配合Grafana的Dashboard设置的如下所示 
spring.application.namespringboot_prometheus
management.endpoints.web.exposure.include*
management.metrics.tags.application${spring.application.name} 
3.3 设置application 
修改启动类如下所示. 
SpringBootApplication
public class Springboot2PrometheusApplication {public static void main(String[] args) {SpringApplication.run(Springboot2PrometheusApplication.class, args);}BeanMeterRegistryCustomizerMeterRegistry configurer(Value(${spring.application.name}) String applicationName) {return (registry) - registry.config().commonTags(application, applicationName);}
} 
SpringBoot项目到这里就配置完成了启动项目访问http://localhost:8080/actuator/prometheus如图所示可以看到一些度量指标。 4.Prometheus配置 
4.1 配置应用 
在prometheus配置监控我们的SpringBoot应用完整配置如下所示。 
# my global config
global:scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.# scrape_timeout is set to the global default (10s).# Alertmanager configuration
alerting:alertmanagers:- static_configs:- targets:# - alertmanager:9093# Load rules once and periodically evaluate them according to the global evaluation_interval.
rule_files:# - first_rules.yml# - second_rules.yml# A scrape configuration containing exactly one endpoint to scrape:
# Here its Prometheus itself.
scrape_configs:- job_name: prometheusstatic_configs:- targets: [127.0.0.1:9090]
###以下内容为SpringBoot应用配置- job_name: springboot_prometheusscrape_interval: 5smetrics_path: /actuator/prometheusstatic_configs:- targets: [127.0.0.1:8080] 
4.2 启动Prometheus 
启动Prometheus浏览器访问查看Prometheus页面如图所示。 点击如图所示位置可以查看Prometheus监控的应用。 列表中UP的页面为存活的实例如图所示。 也可以查看很多指数如下所示。 5.Grafana配置 
启动Grafana配置Prometheus数据源这里以ID是4701的Doshboard为例地址https://grafana.com/dashboards/4701如图。 在Grafana内点击如图所示import按钮 在如图所示位置填写4701然后点击load。 接下来导入Doshboard。 导入后就可以看到我们的SpringBoot项目对应的指标图表了如图。 6.源码 
源码地址https://gitee.com/dalaoyang/springboot_learn/tree/master/springboot2_prometheus 
原文链接 本文为云栖社区原创内容未经允许不得转载。