This is the 26th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

1. Download the visual template

Grafana is a display page, so this article provides a JSON page monitoring SpringBoot for you to download.

Link: pan.baidu.com/s/1h5yrTsqU… Extraction code: EHBV

2. Configuration SpringBoot

1. The modified 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 http://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.3. RELEASE</version>
		<relativePath/> <! -- lookup parent from repository -->
	</parent>
	<groupId>com.dalaoyang</groupId>
	<artifactId>springboot2_prometheus</artifactId>
	<version>0.0.1 - the SNAPSHOT</version>
	<name>springboot2_prometheus</name>
	<description>springboot2_prometheus</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>io.micrometer</groupId>
			<artifactId>micrometer-registry-prometheus</artifactId>
			<version>1.1.3</version>
		</dependency>
	</dependencies>


	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>
Copy the code

2. Modify application. Yml

management:
  endpoints:
    web:
      exposure:
        include: The '*'
  metrics:
    tags:
      application: ${spring.application.name}
  health:
    redis:
      enabled: false
Copy the code

3. Set the application

Add the following code to the launch query.

    @Bean
    MeterRegistryCustomizer<MeterRegistry> configurer(@Value("${spring.application.name}") String applicationName) {
        return (registry) -> registry.config().commonTags("application", applicationName);
    }
Copy the code

SpringBoot project is configured here, start the project, accesshttp://ip:8080/actuator/prometheus can see return values, these values are springBoot returns the information on the foreground page.

3. Prometheus configuration

Monitor our SpringBoot application in Prometheus configuration, complete as shown below. Find the configuration file to replace.

# 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=
      
       ` to any timeseries scraped from this config. - job_name: '
      prometheus' static_configs: - targets: ['localhost:9090'] - job_name: 'springboot_system'scrape_interval: 5s metrics_path:'/actuator/prometheus' static_configs: - targets: ['localhost:8088'] #springboot port numberCopy the code

Start Prometheus at IP :9090, and follow the following steps to see the monitoring tasks that have been registered with Prometheus.

4. Grafana configuration

Enter IP :3000 to enter the grafana visual interface.

1. Configure Prometheus data source (IP :9090).

Fill in your Prometheus address, port 9090, and click Save to be prompted if you fail.

2. Import a visual template

Then import the visual interface template downloaded above.

Select the data source configured above, which is the Prometheus option.

3. Verify

The above screen configuration is complete!