When using Spring Boot as the development framework, you need to monitor the status of applications such as JVM and Spring MVC. TMP collects data such as JVM data based on the Spring Boot Actuator mechanism. With the Grafana dashboard that comes with TMP, you can conveniently monitor the status of Spring Boot applications.
This document uses deploying a Spring Boot application in TKE as an example to describe how to use TMP to monitor the application status.
Note:Spring Boot provides the Actuator component to monitor applications, which reduces the development costs. Therefore, Actuator is directly used in this document to track Spring Boot metrics. You should use Spring Boot v2.0 or above in the following steps, as lower versions may have different configurations.
If you use Spring Boot v1.5 for integration, the integration process will differ from that for v2.0, and you should note the following:
- The address for accessing
prometheus metrics
is different from that for v2.0. On v1.5, the default address is/prometheus
, i.e.,http://localhost:8080/prometheus
.- If error 401 is reported, it indicates no permissions (Whitelabel Error Page). On v1.5, security control is enabled for the
management
API by default, so you need to setmanagement.security.enabled=false
.- If
bootstrap.yml
is used to configure parameters in the project, modifyingmanagement
in it will not work, which should be modified inapplication.yml
due to the Spring Boot start and load sequence.- You cannot add
metric common tag
through YML; instead, you can add it only by adding abean
to the code.
If spring-boot-starter-web
is already imported in this project, add the actuator/prometheus
Maven dependency to the pom.xml
file.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
Edit the application.yml
file in the resources
directory and modify the actuator
configuration to expose the metric data in the Prometheus protocol.
management:
endpoints:
web:
exposure:
include: prometheus # Web access path for opening Prometheus
metrics:
# We recommend you enable the following options to monitor P99 and P95 latencies of HTTP requests. You can set the specific time distribution based on the actual conditions
distribution:
sla:
http:
server:
requests: 1ms,5ms,10ms,50ms,100ms,200ms,500ms,1s,5s
# Add special labels to Prometheus
tags:
# You must add the corresponding application name, as the corresponding monitoring information needs to be viewed by application
application: spring-boot-mvc-demo
In the current directory of the project, run mvn spring-boot:run
. If you can access the metric data of the Prometheus protocol through http://localhost:8080/actuator/prometheus
, the relevant dependency configuration is correct.
Note:The default configurations of the port and path are used in the same, which should be replaced with those in your actual project.
If you have already configured a Docker image environment locally, proceed to the next step; otherwise, configure one as instructed in Getting Started.
Add Dockerfile
in the root directory of the project. You can add it by referring to the following sample code and modify Dockerfile
based on your actual project:
FROM openjdk:8-jdk
WORKDIR /spring-boot-demo
ADD target/spring-boot-demo-*.jar /spring-boot-demo/spring-boot-demo.jar
CMD ["java","-jar","spring-boot-demo.jar"]
Package the image by running the following command in the project root directory. You need to replace namespace
, ImageName
, and image tag
as needed in your actual project.
mvn clean package
docker build . -t ccr.ccs.tencentyun.com/[namespace]/[ImageName]:[image tag]
docker push ccr.ccs.tencentyun.com/[namespace]/[ImageName]:[image tag]
For example:
mvn clean package
docker build . -t ccr.ccs.tencentyun.com/prom_spring_demo/spring-boot-demo:latest
docker push ccr.ccs.tencentyun.com/prom_spring_demo/spring-boot-demo:latest
apiVersion: v1
kind: Service
metadata:
labels: # Add the corresponding labels based on the actual conditions
k8sapp: spring-mvc-demo
name: spring-mvc-demo
namespace: spring-demo
spec:
ports:
- name: 8080-8080-tcp # Corresponding `port` value in the ServiceMonitor scrape task
port: 8080
protocol: TCP
targetPort: 8080
selector:
k8s-app: spring-mvc-demo
qcloud-app: spring-mvc-demo
sessionAffinity: None
type: ClusterIP
status:
loadBalancer: {}
Note:Here, note that the
port
value is thespec/ports/name
value in the Service YAML configuration file.
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: spring-mvc-demo # Enter a unique name
namespace: cm-prometheus # The namespace is fixed. Do not change it
spec:
endpoints:
- interval: 30s
port: 8080-8080-tcp # Enter the name of the corresponding port of the Prometheus exporter in the Service YAML configuration file
path: /actuator/prometheus # Enter the value of the corresponding path of the Prometheus exporter. If it is not specified, it will be `/metrics` by default
namespaceSelector: # Select the namespace where the Service to be monitored resides
matchNames:
- spring-demo
selector: # Enter the label value of the Service to be monitored to locate the target Service
matchLabels:
k8sapp: spring-mvc-demo
Access the Grafana address of your TMP instance to view the application monitoring dashboard in Dashboards > Manage > Application.
Was this page helpful?