1、Prometheus
1)介紹
Prometheus是一套開源的監控&報警&時間序列數據庫的組合,基於應用的metrics來進行監控的開源工具 。
架構圖:
2)下載
https://prometheus.io/download/
3)安裝
tar -xvzf prometheus-2.1.0.linux-amd64.tar.gz
ln -s prometheus-2.1.0.linux-amd64 prometheus
./prometheus --config.file=prometheus.yml &
ps -ef|grep prometheus |grep -v grep
通過指定配置文件prometheus.yml啟動Prometheus
配置文件官方說明: https://prometheus.io/docs/prometheus/latest/configuration/configuration/
默認情況下,Prometheus會監控自己本身。
4)prometheus自身metrics
http://192.168.8.101:9090/metrics
太多了,真是沒法看,還好有個弱弱的圖形頁面 (待會整合到Grafana 中就方便看了)
5)prometheus自身graph
http://192.168.8.101:9090/graph
選中某個指標,點擊Execute . 超多指標可以查看 …
6)查看prometheus配置
prometheus.yml文件
7)查看監控對象
2、SpringBoot
1)pom依賴
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.test</groupId>
<artifactId>clientmonitor</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>clientmonitor</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--增加依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2) application.yaml or application.properties
spring:
application:
name: springbootPrometheusGrafana
management:
endpoints:
web:
exposure:
include: '*'
metrics:
tags:
application: ${spring.application.name}
spring.application.name=springbootPrometheusGrafana
management.endpoints.web.exposure.include=*
management.metrics.tags.application=${spring.application.name}
#注意此處使用,訪問的時候必須訪問/actuator/prometheus,如不配置則訪問/prometheus
management.endpoints.web.base-path=/actuator
3)實例化MeterRegistryCustomizer
@SpringBootApplication
public class ClientmonitorApplication {
public static void main(String[] args) {
SpringApplication.run(ClientmonitorApplication.class, args);
}
@Bean
MeterRegistryCustomizer<MeterRegistry> configurer(@Value("${spring.application.name}") String applicationName) {
return (registry) -> registry.config().commonTags("application", applicationName);
}
}
4) 注冊Metrics
實現 MeterBinder 接口的 bindTo 方法,將要采集的指標注冊到 MeterRegistry
@Component
public class JobMetrics implements MeterBinder {
public Counter job1Counter;
public Counter job2Counter;
public Map<String, Double> map;
JobMetrics() {
map = new HashMap<>();
}
@Override
public void bindTo(MeterRegistry meterRegistry) {
this.job1Counter = Counter.builder("counter_builder_job_counter1")
.tags(new String[]{"name", "tag_job_counter1"})
.description("description-Job counter1 execute count").register(meterRegistry);
this.job2Counter = Counter.builder("counter_builder_job_counter2")
.tags(new String[]{"name", "tag_job_counter2"})
.description("description-Job counter2 execute count ").register(meterRegistry);
Gauge.builder("gauge_builder_job_gauge", map, x -> x.get("x"))
.tags("name", "tag_job_gauge")
.description("description-Job gauge")
.register(meterRegistry);
}
}
5) 定時任務實現counter,gauge數據填充
@Component
@EnableScheduling
public class MyJob {
private Integer count1 = 0;
private Integer count2 = 0;
@Autowired
private JobMetrics jobMetrics;
@Async("main")
@Scheduled(fixedDelay = 1000)
public void doSomething() {
count1++;
jobMetrics.job1Counter.increment();
jobMetrics.map.put("x", Double.valueOf(count1));
System.out.println("task1 count:" + count1);
if(count1%2==0){
System.out.println("%5==0");
jobMetrics.map.put("x", Double.valueOf(1));
}
}
@Async
@Scheduled(fixedDelay = 10000)
public void doSomethingOther() {
count2++;
jobMetrics.job2Counter.increment();
System.out.println("task2 count:" + count2);
}
}
7) Controller 接口方式實現counter,gauge數據填充
@RestController
public class CounterController {
@Autowired
private JobMetrics jobMetrics;
@RequestMapping(value = "/counter1", method= RequestMethod.GET)
public void counter1() {
jobMetrics.job2Counter.increment();
}
@RequestMapping(value = "/counter2", method= RequestMethod.GET)
public void counter2() {
jobMetrics.job2Counter.increment();
}
@RequestMapping(value = "/gauge", method= RequestMethod.GET)
public void gauge(@RequestParam(value = "x") String x) {
System.out.println("gauge controller x"+x);
jobMetrics.map.put("x",Double.valueOf(x));
}
}
6) prometheus 更改配置文件,接入SpringBoot
增加如下配置
#SpringBoot應用配置
- job_name: 'springbootPrometheusGrafana'
scrape_interval: 5s
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['192.168.8.1:8080']
Targets
metrisc_path:/actuator/prometheus
Configuration
Service Discovery:
3、Grafana
1)介紹
Prometheus 的可視化功能比較弱,這里我們來接入Grafana 。
Grafana是一個跨平台的開源的度量分析和可視化工具,可以通過將采集的數據查詢然后可視化的展示,並及時通知。它主要有以下六大特點:
-
展示方式:快速靈活的客戶端圖表,面板插件有許多不同方式的可視化指標和日志,官方庫中具有豐富的儀表盤插件,比如熱圖、折線圖、圖表等多種展示方式;
-
數據源:Graphite,InfluxDB,OpenTSDB,Prometheus,Elasticsearch,CloudWatch和KairosDB等;
-
通知提醒:以可視方式定義最重要指標的警報規則,Grafana將不斷計算並發送通知,在數據達到閾值時通過Slack、PagerDuty等獲得通知;
-
混合展示:在同一圖表中混合使用不同的數據源,可以基於每個查詢指定數據源,甚至自定義數據源;
-
注釋:使用來自不同數據源的豐富事件注釋圖表,將鼠標懸停在事件上會顯示完整的事件元數據和標記;
-
過濾器:Ad-hoc過濾器允許動態創建新的鍵/值過濾器,這些過濾器會自動應用於使用該數據源的所有查詢。
2) 安裝
yum localinstall grafana-6.2.1-1.x86_64.rpm
systemctl start grafana-server
#設為開機啟動
systemctl enable grafana-server
ps -ef|grep grafana-server |grep -v grep
3)訪問
配置文件 /etc/grafana/grafana.ini , 默認3000端口,按需修改
##################### Grafana Configuration Example #####################
#
# Everything has defaults so you only need to uncomment things you want to
# change
# possible values : production, development
;app_mode = production
# instance name, defaults to HOSTNAME environment variable value or hostname if HOSTNAME var is empty
;instance_name = ${HOSTNAME}
#################################### Paths ####################################
[paths]
# Path to where grafana can store temp files, sessions, and the sqlite3 db (if that is used)
;data = /var/lib/grafana
# Temporary files in `data` directory older than given duration will be removed
;temp_data_lifetime = 24h
# Directory where grafana can store logs
;logs = /var/log/grafana
# Directory where grafana will automatically scan and look for plugins
;plugins = /var/lib/grafana/plugins
# folder that contains provisioning config files that grafana will apply on startup and while running.
;provisioning = conf/provisioning
#################################### Server ####################################
[server]
# Protocol (http, https, socket)
;protocol = http
# The ip address to bind to, empty will bind to all interfaces
;http_addr =
# The http port to use
;http_port = 3000
"/etc/grafana/grafana.ini" 530L, 16841C
;access_key =
;secret_key =
[external_image_storage.webdav]
;url =
;public_url =
;username =
;password =
[external_image_storage.gcs]
;key_file =
;bucket =
;path =
[external_image_storage.azure_blob]
;account_name =
;account_key =
;container_name =
[external_image_storage.local]
# does not require any configuration
[rendering]
# Options to configure external image rendering server like https://github.com/grafana/grafana-image-renderer
;server_url =
;callback_url =
[enterprise]
# Path to a valid Grafana Enterprise license.jwt file
;license_path =
[panels]
# If set to true Grafana will allow script tags in text panels. Not recommended as it enable XSS vulnerabilities.
;disable_sanitize_html = false
[plugins]
;enable_alpha = false
;app_tls_skip_verify_insecure = false
訪問 http://192.168.8.101:3000/login
默認的用戶名和密碼為 admin/admin
SpringBoot JVM監控 grafana 導入471 dashboard