springboot 1.5.10.RELEASElinux
Grafana 5.4.2git
Prometheus 2.6.0github
jdk 1.8web
在项目pom.xml中添加以下依赖spring
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <version>1.5.4.RELEASE</version> </dependency> <!--<dependency> <groupId>io.prometheus</groupId> <artifactId>simpleclient_spring_boot</artifactId> <version>0.5.0</version> </dependency>--> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-spring-legacy</artifactId> <version>1.1.1</version> </dependency>
gradle.build中增长以下:json
compile 'io.micrometer:micrometer-registry-prometheus:1.1.1' compile 'io.micrometer:micrometer-spring-legacy:1.1.1'
在 application.yml中添加以下配置(由于是测试,因此我把全部端点都暴露了,生产环境自行选择打开端点)ubuntu
management:
endpoints:
web:
exposure:
include: health,info,env,prometheus,metrics,httptrace,threaddump,heapdump,springmetrics
jmx:
exposure:
include: '*'
shutdown:
enabled: false
metrics:
distribution:
percentiles-histogram[http.server.requests]: true
security:
enabled: false
启动项目,在eclipse中能够看到接口 /prometheus 以下图浏览器
经过浏览器查看prometheus.json文件以下图:springboot
至此,应用侧的prometheus client的工做已经完成。app
下载你想安装的prometheus版本,地址为download prometheus
我下载的是prometheus-2.6.0.linux-amd64.tar.gz
解压
tar xvfz prometheus-*.tar.gz
cd 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 defaults to '/metrics' # scheme defaults to 'http'. metrics_path: /prometheus static_configs: - targets: ['10.200.110.100:8082']#此处填写 Spring Boot 应用的 IP + 端口号
注意:metrics_path:和targets的配置。
启动
./prometheus --config.file="prometheus.yml"
在Status->Targets页面下,咱们能够看到咱们配置的Target,它们的State为UP ,以下图
至此,prometheus和springboot已经链接成功。
使用的是ubuntu 16.04TLS,因此找到官网相对应的Ubuntu方式,这是官网的连接地址:https://grafana.com/grafana/download?platform=linux
wget https://dl.grafana.com/oss/release/grafana_5.4.2_amd64.deb sudo dpkg -i grafana_5.4.2_amd64.deb
启动grafana
方式1、Start Grafana by running:
sudo service grafana-server start sudo update-rc.d grafana-server defaults //设置开机启动(可选)
方式2、To start the service using systemd:
systemctl daemon-reload systemctl start grafana-server systemctl status grafana-server sudo systemctl enable grafana-server.service //设置开机启动
grafana添加数据源,配置以下
6.建立看板
grafana支持不少种看板,你能够根据不一样的指标生成图表,
由于图表的配置比较复杂,这里没有深刻的研究,而是选用了大神 们作好的模板,对接数据源进行展现
https://grafana.com/dashboards 在这里能够搜索不一样的模板
选择一个你想要的点击去,而后复制id
打开下图页面,并将id粘贴进去,光标离开输入框,会自动加载模板配置
接着选datasource:
而后选取数据源,点击import按钮,完成模板添加
看数据都是空的,由于这个是springboot2.x的。要换成springboot1.x的,以下图:
结果:
完成。
多个应用的配置,以下配置两个微服务应用:
# 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: 'service-productor' # metrics_path defaults to '/metrics' # scheme defaults to 'http'. metrics_path: /prometheus static_configs: - targets: ['10.200.110.100:8082'] - job_name: 'service-consumer' # metrics_path defaults to '/metrics' # scheme defaults to 'http'. metrics_path: /prometheus static_configs: - targets: ['10.200.110.100:8081']
重启prometheus后,再刷新target页面:
回到Grafana的页面: