Introduction to the

Spring Boot has monitoring functions that can help monitor the internal operation of programs, such as monitoring status, Bean loading, environment variables, log information, thread information, etc. This section combines Prometheus and Grafana to show this information more visually.

The experiment

instructions

The service name address port
Prometheus 172.16.2.101 9090
Grafana 172.16.2.101 3000
Spring Boot Demo 172.16.2.204 8080

Create a project

Create a Spring Boot project for testing with the following main code.

pom.xml

<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>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
Copy the code

application.yml

management:
  endpoints:
    web:
      exposure:
        include: The '*'

  endpoint:
    health:
      show-details: always

  metrics:
    tags:
      application: actuator-demo
Copy the code
  • Management. Endpoints. Web. Exposure. Include: most of the physical port will not publicly via HTTP, * on behalf of the public all these endpoints. For a production environment, carefully select the endpoints to expose.
  • Management. The metrics. Tags. Application: set the tag for application, easy to distinguish the different applications.

Start the class

@SpringBootApplication
@RestController
public class SpringbootActuatorPrometheusDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootActuatorPrometheusDemoApplication.class, args);
    }

    @RequestMapping(value = "/hello")
    public String  sayHello(a) {
        for (int i = 1 ; i <= 10 ; i++) {
            Thread t = new Thread(() -> {
                try {
                    Thread.sleep(5000);
                } catch(InterruptedException e) { e.printStackTrace(); }},"HelloThread - " + i);
            t.start();
        }
        return "ok";
    }

    / * *@BeanMeterRegistryCustomizer<MeterRegistry> metricsCommonTags() { return registry -> registry.config().commonTags("application", "springboot-actuator-prometheus-demo"); } * /

}
Copy the code

Configure Prometheus and Grafana

Add a monitoring job for the Spring Boot application in Promethes. yml

- job_name: 'actuator-demo'
    metrics_path: '/prometheus'
    static_configs:
    - targets: ['172.16.2.204:8080']
Copy the code

Run Prometheus and Grafana:

docker start prometheus grafana
Copy the code

Log in to Prometheus UI http://172.16.2.101:9090 and check targets. If the job is in UP state, the configuration is successful.

Grafana UI http://172.16.2.101:3000, Import JVM (Micrometer) Dashboard via Grafana + icon:

  • grafana id = 4701
  • Note that the Prometheus data source is selected

See JVM (Micormeter) Dashboard:

You can see the stack, thread, IO, and so on of the applied JVM.

The source code

Github.com/gf-huanchup…

reference

Micrometer. IO/docs/regist… Prometheus. IO/docs/promet…

Content of the past

  • Introduction to Prometheus
  • Prometheus integrates with Grafana
  • Prometheus integration AlertManager
  • Prometheus integrates with Node Exporter

Pay attention to my