The preparatory work

  • Prometheus v2.14.0
  • Grafana v6.5.0

Use Docker to deploy the above base environment, docker-comemage. yml configuration file, enter the following command to deploy the one click:

Docker-compose up -d: docker-compose down docker-compose up -d: docker-compose downCopy the code

Adding dependencies

pom.xml

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <! <dependency> <groupId> IO. Micrometer </groupId> <artifactId>micrometer-registry-prometheus</artifactId> </dependency> </dependencies>Copy the code

application.yml

spring: application: name: monitoring-prometheus-grafana management: endpoints: web: exposure: # Opensource/opensource/opensource include: 'Prometheus' metrics: Tags: application: ${spring.application.name} logging: level: io.github.y0ngb1n.*: debugCopy the code

use

Step 1: Start the service

$ mvn spring-boot:run ... The 2019-12-08 22:28:11. 36157-916 the INFO [main] O.S.B.A.E.W eb. EndpointLinksResolver: Exposing 1 to the Exposing 1 endpoint(S) : Drive '/ RING '-- [main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (HTTP) with the context path '22:28:12 2019-12-08. 36157-050 the INFO [main] I.G.Y.M.P.P rometheusGrafanaApplication: Started PrometheusGrafanaApplication in 20.638 seconds (JVM running for 27.154)Copy the code

Step 2: Access /actuator/ Prometheus ports

$curl - sS # http://127.0.0.1:8080/actuator/prometheus HELP jvm_gc_memory_promoted_bytes_total Count of positive increases in the size of the old generation memory pool before GC to after GC # TYPE jvm_gc_memory_promoted_bytes_total Counter jvm_gc_memorY_promoted_bytes_total {application="monitoring- Prometheus -grafana",} 9986992.0 # HELP jvm_threads_daemon_threads The current number of live daemon threads # TYPE jvm_threads_daemon_threads gauge Jvm_threads_daemon_threads {application="monitoring- Prometheus -grafana",} 30.0 # HELP process_uptime_seconds The uptime of the Java virtual machine # TYPE process_uptime_seconds gauge Process_uptime_seconds {application = "monitoring - Prometheus - grafana",} 75.133...Copy the code

Connect the application to Prometheus

Add Prometheus configuration file prometheus.yml

- job_name: 'spring-boot-app' # scrape_interval: 15s # scrape_timeout: 10s '/ skeletonoid/Prometheus' # Configure the IP address of the server using Spring Boot. Static_configs: -targets: ['192.168.31.44:8080']Copy the code

Can use ifconfig the IP address of the host machine, Prometheus server will automatically request http://your-ip:8080/actuator/prometheus every 15 seconds. For more configurations, see 👉 official documents

Test visit Prometheus

STEP 1: Visit http://localhost:9090 in your browser

STEP 2: View monitoring data

  • Click on theInsert metric at cursor, you can select monitoring indicators;
  • Click on theGraph, so that indicators can be displayed in the form of charts;
  • Click on theExecuteButton to see a result similar to the following image.

Integrate Grafana visualization

You’ve already started Grafana with Docker-compose, so configure it.

STEP 1: Open a browser and log in to http://localhost:3000 using the initial account admin:admin

Click Add Data Source to Add Prometheus Data Source

STEP 3: Create a Dashboard

  • Setting Monitoring Indicators

  • Setting Dashboard Information

  • Save the Dashboard

You can add different indicators. For details, see /actuator/ Prometheus endpoints in Spring Boot. Repeat the preceding steps to add a new chart to the Dashboard.

If you think you customize a Dashboard too complex, that you can go to the Dashboard of Grafana market (https://grafana.com/grafana/dashboards), This makes it easy and quick to reuse someone else’s fancy Dashboard.

The resources

  • https://www.callicoder.com/spring-boot-actuator-metrics-monitoring-dashboard-prometheus-grafana/
  • http://www.itmuch.com/spring-boot/actuator-prometheus-grafana/, by Zhou Li
  • https://www.baeldung.com/spring-boot-self-hosted-monitoring
  • https://yunlzheng.gitbook.io/prometheus-book/
  • https://micrometer.io/docs/registry/prometheus
  • https://prometheus.io/docs/introduction/overview/
  • https://hub.docker.com/r/prom/prometheus/
  • https://grafana.com/docs/grafana/latest/

Thank you for reading. This article is copyrighted by Yang Bin. If reprinted, please indicate the source: Yang Bin’s blog ()

The project has been hosted on GitHub: y0ngb1n/spring-boot-samples. Welcome to Star, Fork 😘