pom.xml
文件中添加相关的 Maven 依赖项,试情况调整相应的版本,示例如下:<dependency><groupId>io.prometheus</groupId><artifactId>simpleclient</artifactId><version>0.9.0</version></dependency><dependency><groupId>io.micrometer</groupId><artifactId>micrometer-registry-prometheus</artifactId><version>1.1.7</version></dependency>
io.micrometer.core.instrument.binder
包下,可以按实际情况添加。示例如下:public class Application {// 作为全局变量,可以在自定义监控中使用public static final PrometheusMeterRegistry registry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);static {// 添加 Prometheus 全局 Label,建议加一上对应的应用名registry.config().commonTags("application", "java-demo");}public static void main(String[] args) throws Exception {// 添加 JVM 监控new ClassLoaderMetrics().bindTo(registry);new JvmMemoryMetrics().bindTo(registry);new JvmGcMetrics().bindTo(registry);new ProcessorMetrics().bindTo(registry);new JvmThreadMetrics().bindTo(registry);new UptimeMetrics().bindTo(registry);new FileDescriptorMetrics().bindTo(registry);System.gc(); // Test GCtry {// 暴露 Prometheus HTTP 服务,如果已经有,可以使用已有的 HTTP ServerHttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);server.createContext("/metrics", httpExchange -> {String response = registry.scrape();httpExchange.sendResponseHeaders(200, response.getBytes().length);try (OutputStream os = httpExchange.getResponseBody()) {os.write(response.getBytes());}});new Thread(server::start).start();} catch (IOException e) {throw new RuntimeException(e);}}}
System.gc()
。http://localhost:8080/metrics
访问到 Prometheus 协议的指标数据。Dockerfile
,请根据实际项目进行修改。示例如下:FROM openjdk:8-jdkWORKDIR /java-demoADD target/java-demo-*.jar /java-demo/java-demo.jarCMD ["java","-jar","java-demo.jar"]
namespace
/ImageName
/镜像版本号
。mvn clean packagedocker build . -t ccr.ccs.tencentyun.com/[namespace]/[ImageName]:[镜像版本号]docker push ccr.ccs.tencentyun.com/[namespace]/[ImageName]:[镜像版本号]
mvn clean packagedocker build . -t ccr.ccs.tencentyun.com/prom_spring_demo/java-demo:latestdocker push ccr.ccs.tencentyun.com/prom_spring_demo/-demo:latest
Deployment
管理页面,选择对应的 命名空间
来进行部署服务,通过 YAML 来创建对应的 Deployment
,YAML 配置如下。apiVersion: apps/v1kind: Deploymentmetadata:labels:k8s-app: java-demoname: java-demonamespace: spring-demospec:replicas: 1selector:matchLabels:k8s-app: java-demotemplate:metadata:labels:k8s-app: java-demospec:containers:- image: ccr.ccs.tencentyun.com/prom_spring_demo/java-demoimagePullPolicy: Alwaysname: java-demoports:- containerPort: 8080name: metric-portterminationMessagePath: /dev/termination-logterminationMessagePolicy: FilednsPolicy: ClusterFirstimagePullSecrets:- name: qcloudregistrykeyrestartPolicy: AlwaysschedulerName: default-schedulerterminationGracePeriodSeconds: 30
Pod Monitor
来定义 Prometheus 抓取任务,YAML 配置示例如下:apiVersion: monitoring.coreos.com/v1kind: PodMonitormetadata:name: java-demonamespace: cm-prometheusspec:namespaceSelector:matchNames:- java-demopodMetricsEndpoints:- interval: 30spath: /metricsport: metric-portselector:matchLabels:k8s-app: java-demo
JVM
监控,安装对应的 Grafana Dashboard 即可开启 JVM 监控大盘。Dashboards/Manage/Application
下查看应用相关的监控大屏。
本页内容是否解决了您的问题?