For any system, just like people, it is necessary to “know yourself and know your opponent” to win every battle. High availability is often discussed. On the one hand, it is necessary to ensure the high availability of the project from the aspects of architecture design, coding implementation, quality verification, etc. On the other hand, it is also very important to find problems as early as possible. At present, there are a large number of products to choose from in the monitoring ecology, especially in the Java ecology, especially in the Spring Boot ecology. How to choose a suitable scheme is particularly important.
Spring Boot Admin can be used to monitor Spring Boot applications, but it is not comprehensive enough. This article mainly introduces Prometheus and Grafana to monitor and alarm. The general flow chart is as follows
Introduction to the
Prometheus profile
Prometheus is a flexible monitoring solution developed since 2012 that stores its data into a timing database, provides a multi-dimensional data model and a powerful query language to generate reports on monitored resources. Originally published by SpringCloud
The characteristics of
- Multidimensional data model
- Flexible query language
- Does not rely on distributed storage
- Time series are collected in Pull mode and transmitted through HTTP protocol
- Support for push time series via mediation gateway
- Monitor data through service or static configuration
- Supports charts and dashboards
component
- Prometheus main program, mainly responsible for storage, capture, aggregation, query
- AlertManager, which is responsible for alarm implementation
- PushGateway, which implements index data pushed by the Client, is fetched by the main program at specified intervals
Grafana profile
Grafana is an open source application written in the GO language. It is mainly used for visualization of large-scale metric data, with hot swap control panel and extensible data source. It already supports most commonly used sequential databases, including: Graphite, Elasticsearch, CloudWatch, InfluxDB, OpenTSDB, Prometheus, etc
Build Prometheus application
Code-level configuration
Maven pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
Copy the code
Start the class
@SpringBootApplication
public class PrometheusApplication {
public static void main(String[] args) { SpringApplication.run(PrometheusApplication.class, args); }}Copy the code
Add the MeterRegistryCustomizer configuration class
@Configuration
public class MeterRegistryConfig {
@Value("${spring.application.name}")
private String applicationName;
@Bean
MeterRegistryCustomizer<MeterRegistry> metricsCommonTags(a) {
return registry -> registry.config().commonTags("application", applicationName); }}Copy the code
The configuration file
Add the configuration application name in the configuration file and enable all the endpoints
server:
port: 8074
spring:
application:
name: prometheus-demo
management:
endpoints:
web:
exposure:
include: The '*'
endpoint:
health:
show-details: always
Copy the code
Verification test
Start the project, visit http://localhost:8074/actuator/prometheus, you can see the following information were collected
Environment set up
This article has taken Mac as an example. For other systems, you can choose to install binary packages directly or download source code to compile and install
Install the Prometheus
brew install prometheus
Copy the code
Or from https://prometheus.io/download/ to choose the appropriate version to download and install
After the installation is successful, the configuration file is stored in the /usr/local/etc/ directory by default for simple configuration
vim /usr/local/etc/prometheus.yml
Copy the code
Note: If the page cannot be accessed after startup, please check whether the configuration format is wrong (space or Tab).
global:
scrape_interval: 15s
evaluation_interval: 15s
alerting:
alertmanagers:
- static_configs:
- targets:
rule_files:
scrape_configs:
- job_name: "prometheus"
static_configs:
- targets: ["localhost:9090"]
- job_name: "prometheus-demo"
scrape_interval: 5s
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['localhost:8074']
Copy the code
To access it, start Prometheus, default port 9090 (this can be modified to suit your needs)
brew services start prometheus
Copy the code
Open management page: http://localhost:9090/graph
Grafana installation
Install and start Grafana
brew install grafana
Copy the code
Or download and install the appropriate version from https://grafana.com/get
Configuration location: / usr/local/etc/grafana/grafana ini log position: / usr/local/var/log/grafana grafana. The log plug-in location: /usr/local/var/lib/grafana/plugins
Start the service
brew services start grafana
Copy the code
Default port: 3000 (can be changed according to their own situation) Default user name password: admin/admin (the first login will improve the password change)
Configuration Grafana
To create a Data source, select the source of Type Prometheus and name promethus-data
The easiest and direct way to add a Dashboard is to click on the second Tab page of the DataSource configuration, select a combination of Dashboard options, and then import, then there is a set of configured viewing pages. Go back to the home page and select a Dashboard to view the monitoring data
The sample code
spring-boot-prometheus