使用Prometheus和Grafana監控Spring Boot應用
Spring Boot應用程式包裝在Docker容器中,Spring Boot應用程式向Prometheus公開指標,Grafana可以閱讀這些指標,並允許從中進行很好的視覺化。在此處下載程式碼。
在Docker容器中包裝Spring Boot
在Docker容器中包裝Spring Boot應用程式非常簡單。按照以下方式建立Dockerfile(更改FROM條目可以獲取不同的JDK)
FROM oracle/graalvm-ce:1.0.0-rc8 VOLUME /tmp ARG JAR_FILE COPY ${JAR_FILE} app.jar ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"] |
加入Maven的pom.xml構建映象,定義使用的屬性:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>com.spotify</groupId> <artifactId>dockerfile-maven-plugin</artifactId> <version>1.4.8</version> <configuration> <repository>${docker.image.prefix}/${project.artifactId}</repository> <buildArgs> <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE> </buildArgs> </configuration> </plugin> </plugins> </build> |
現在你可以執行:
mvn clean package dockerfile:build
它會為你建立Docker映象:springio/gs-reactive-rest-service:latest
鍵入以下命令執行:
docker run -p 8080:8080 -t springio/gs-reactive-rest-service:latest
從Spring Boot獲取Prometheus指標
為了從String Boot應用程式中提供Prometheus指標,需要新增一些依賴項:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- Micormeter core dependecy --> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-core</artifactId> </dependency> <!-- Micrometer Prometheus registry --> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> </dependency> |
現在您可以執行Docker容器並轉到以下URL:http://localhost:8080/actuator/prometheus
可以看到各種輸出。
提供Prometheus配置
提供了一個小配置檔案,讓Prometheus檢視Spring Boot中的指標URL(參見此處)
Global configurations global: scrape_interval: 5s # Set the scrape interval to every 5 seconds. evaluation_interval: 5s # Evaluate rules every 5 seconds. scrape_configs: - job_name: 'reactive-spring-boot-app' metrics_path: '/actuator/prometheus' static_configs: - targets: ['spring-boot:8080'] |
合併在一起
把Spring Boot,Prometheus和Grafana放在一起,見:docker-compose.yml
version: '3' services: spring-boot: image: "springio/gs-reactive-rest-service" ports: - "8080:8080" container_name: spring-boot prometheus: image: "prom/prometheus" ports: - "9090:9090" volumes: - ./prom.yml:/etc/prometheus/prometheus.yml container_name: prometheus grafana: image: "grafana/grafana" ports: - "3000:3000" container_name: grafana |
Grafana和Prometheus是官方Docker映象。這個配置啟動Spring Boot(localhost:8080),帶有配置檔案Prometheus(localhost:9090)和Grafana(localhost:3000)。它們將被置於相同的Docker網路中,並可透過主機名'prometheus','grafana'和spring-boot'互相訪問。
執行下面命令啟動:
docker-compose up
配置Grafana
在Grafana中,可以輕鬆新增Prometheus作為資料來源。完成此操作後,您可以新增儀表板。一種簡單的方法是在Prometheus中建立一個簡單的查詢並將其複製到Grafana以從中建立圖形。
透過下面方式下載啟動:
git clone https://github.com/MaartenSmeets/gs-reactive-rest-service cd gs-reactive-rest-service/complete mvn clean package mvn dockerfile:build docker-compose up |
相關文章
- Spring Boot Actuator詳解與深入應用(三):Prometheus+Grafana應用監控Spring BootPrometheusGrafana
- Spring Boot中使用Prometheus監控教程Spring BootPrometheus
- 使用Prometheus、Grafana監控Artifactory實踐PrometheusGrafana
- Prometheus+Grafana實現服務效能監控:windows主機監控、Spring Boot監控、Spring Cloud Alibaba Seata監控PrometheusGrafanaWindowsSpring BootCloud
- 基於Prometheus+Grafana監控Laravel+Swoole應用PrometheusGrafanaLaravel
- Spring Boot (十九):使用 Spring Boot Actuator 監控應用Spring Boot
- Prometheus MySQL監控+grafana展示PrometheusMySqlGrafana
- prometheus+grafana 監控nginxPrometheusGrafanaNginx
- docker部署監控Prometheus+GrafanaDockerPrometheusGrafana
- Prometheus + Grafana 監控平臺搭建PrometheusGrafana
- 配置 Prometheus 伺服器監控和 Grafana 看板Prometheus伺服器Grafana
- Spring Boot中如何使用Ostara監控應用?Spring Boot
- 使用 Prometheus 監控 SAP ABAP 應用程式Prometheus
- 使用Prometheus和Grafana進行系統監控和預測 - flightawarePrometheusGrafana
- Grafana、Prometheus、mtail-日誌監控GrafanaPrometheusAI
- Grafana+Prometheus 監控 MySql服務GrafanaPrometheusMySql
- Spring Boot應用監控實戰Spring Boot
- Prometheus監控實戰應用Prometheus
- prometheus+grafana監控mysql最佳實踐PrometheusGrafanaMySql
- 效能監控工具之Grafana+Prometheus+ExportersGrafanaPrometheusExport
- [Hyperf] 在 Hyperf 框架中使用 prometheus + grafana 部署基本的監控框架PrometheusGrafana
- 使用Prometheus+Grafana快速打造高逼格監控平臺PrometheusGrafana
- 03 . Prometheus監控容器和HTTP探針應用PrometheusHTTP
- Spring Boot 揭祕與實戰(九) 應用監控篇 - HTTP 應用監控Spring BootHTTP
- Spring Boot使用Druid和監控配置Spring BootUI
- Spring Boot Admin2.1應用監控Spring Boot
- spring-boot-route(十八)spring-boot-adtuator監控應用Springboot
- 備忘錄二:Spring Boot Actuator+Prometheus+GrafanaSpring BootPrometheusGrafana
- spring-boot-plus1.1.0.釋出-整合Spring Boot Admin管理和監控應用Spring Boot
- 圖文詳解Prometheus監控+Grafana+Alertmanager告警安裝使用PrometheusGrafana
- K8S的Kafka監控(Prometheus+Grafana)K8SKafkaPrometheusGrafana
- 最強效能監控工具之Grafana+Prometheus+ExportersGrafanaPrometheusExport
- Java服務端監控:Prometheus與Grafana的整合Java服務端PrometheusGrafana
- 【DB寶36】使用Docker分分鐘搭建漂亮的prometheus+grafana監控DockerPrometheusGrafana
- grafana+prometheus快速搭建MySql監控系統實踐GrafanaPrometheusMySql
- Prometheus 監控Mysql伺服器及Grafana視覺化PrometheusMySql伺服器Grafana視覺化
- docker-compose 搭建 Prometheus+Grafana監控系統DockerPrometheusGrafana
- linux視覺化監控:Grafana+Prometheus+node_exporterLinux視覺化GrafanaPrometheusExport