1,spring boot工程引入jar包
compile 'org.springframework.boot:spring-boot-starter-actuator' compile 'io.micrometer:micrometer-core:1.3.5' compile 'io.micrometer:micrometer-registry-prometheus:1.3.5'
2,修改spring boot工程配置
management: server: port: 10091 endpoints: web: exposure: include: '*'
3,监控代码示例
Counter counter = Metrics.counter("counter", "tag-1", "tag-2");
counter.increment();
4, 启动Spring boot工程
启动后访问http://localhost:10091/actuator,应该能看到返回很多链接,如能看到http://localhost:10091/actuator/prometheus, 说明正确。
5,安装配置prometheus
下载安装后后,修改prometheus.yml
# 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 it's Prometheus itself. scrape_configs: # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config. - job_name: 'prometheus' metrics_path: /actuator/prometheus # scheme defaults to 'http'. static_configs: - targets: ['localhost:10091']
6, 启动prometheus,查看运行效果