pom.xml file and adjust the version as needed as follows:<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 package and can be added as needed as follows:public class Application {// It can be used in custom monitoring as a global variablepublic static final PrometheusMeterRegistry registry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);static {// Add a global Prometheus label. We recommend you add the corresponding application name to itregistry.config().commonTags("application", "java-demo");}public static void main(String[] args) throws Exception {// Add JVM monitoringnew 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 {// Expose the Prometheus HTTP service. If it already exists, you can use the existing 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() to make the test more straightforward.http://localhost:8080/metrics.Dockerfile in the root directory of the project. Please modify it based on your actual project conditions as follows:FROM openjdk:8-jdkWORKDIR /java-demoADD target/java-demo-*.jar /java-demo/java-demo.jarCMD ["java","-jar","java-demo.jar"]
namespace, ImageName, and image tag as needed.mvn clean packagedocker build . -t ccr.ccs.tencentyun.com/[namespace]/[ImageName]:[image tag]docker push ccr.ccs.tencentyun.com/[namespace]/[ImageName]:[image tag]
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
namespace to deploy the service. Use the following YAML configuration to create the corresponding Deployment: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 to define a Prometheus scrape task. Below is a sample YAML configuration: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


Feedback