使用Prometheus和Grafana监控Spring Boot应用

栏目: Java · 发布时间: 7年前

内容简介:Spring Boot应用程序包装在Docker容器中,Spring Boot应用程序向Prometheus公开指标,Grafana可以阅读这些指标,并允许从中进行很好的可视化。在在Docker容器中包装Spring Boot应用程序非常简单。按照以下方式创建Dockerfile(更改FROM条目可以获取不同的JDK)

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 [<font>"java"</font><font>,</font><font>"-Djava.security.egd=file:/dev/./urandom"</font><font>,</font><font>"-jar"</font><font>,</font><font>"/app.jar"</font><font>]
</font>

加入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'
    <b>static</b>_configs:
    - targets: ['spring-boot:8080']

合并在一起

把Spring Boot,Prometheus和Grafana放在一起,见: docker-compose.yml

version: '3'
services:
  spring-boot:
    image: <font>"springio/gs-reactive-rest-service"</font><font>
    ports:
     - </font><font>"8080:8080"</font><font>
    container_name: spring-boot
  prometheus:
    image: </font><font>"prom/prometheus"</font><font>
    ports:
     - </font><font>"9090:9090"</font><font>
    volumes:
     - ./prom.yml:/etc/prometheus/prometheus.yml
    container_name: prometheus
  grafana:
     image: </font><font>"grafana/grafana"</font><font>
     ports:
     - </font><font>"3000:3000"</font><font>
     container_name: grafana
</font>

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:<font><i>//github.com/MaartenSmeets/gs-reactive-rest-service</i></font><font>
cd gs-reactive-<b>rest</b>-service/complete
mvn clean <b>package</b>
mvn dockerfile:build
docker-compose up
</font>

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Clean Architecture

Clean Architecture

Robert C. Martin / Prentice Hall / 2017-9-20 / USD 34.99

Practical Software Architecture Solutions from the Legendary Robert C. Martin (“Uncle Bob”) By applying universal rules of software architecture, you can dramatically improve developer producti......一起来看看 《Clean Architecture》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具