prometheus和pushgateway docker-compose.yml及java版简易推送demo

public static void main(String[] args) {
try{
String url = "172.30.12.167:9091";
CollectorRegistry registry = new CollectorRegistry();
Gauge guage = Gauge.build("my_custom_metric", "This is my custom metric.").labelNames("app", "date").create();
String date = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss").format(new Date());
guage.labels("my-pushgateway-test-0", date).set(25);
guage.labels("my-pushgateway-test-1", date).dec();
guage.labels("my-pushgateway-test-2", date).dec(2);
guage.labels("my-pushgateway-test-3", date).inc();
guage.labels("my-pushgateway-test-4", date).inc(5);
guage.register(registry);
PushGateway pg = new PushGateway(url);
Map<String, String> groupingKey = new HashMap<String, String>();
groupingKey.put("instance", "my_instance");
pg.pushAdd(registry, "my_job", groupingKey);
} catch (Exception e){
e.printStackTrace();
}
}
<dependency>  
      <groupId>org.springframework.boot<\/groupId>  
      <artifactId>spring-boot-starter-web<\/artifactId>  
  <\/dependency>  
  
  <dependency>  
      <groupId>io.micrometer<\/groupId>  
      <artifactId>micrometer-registry-prometheus<\/artifactId>  
  <\/dependency>  
  
  <dependency>  
      <groupId>org.springframework.boot<\/groupId>  
      <artifactId>spring-boot-starter-actuator<\/artifactId>  
  <\/dependency>  

 docker-compose如下:

version: '3'
services:
  # prometheus
  prometheus:
    image: prom/prometheus:v2.18.0
    hostname: prometheus
    container_name: prometheus
    restart: always
   user: root ports:
- "9090:9090" volumes: - ./prometheus:/etc/prometheus - ./prometheus/data:/prometheus privileged: true command: - '--config.file=/etc/prometheus/prometheus.yml' - '--web.console.libraries=/etc/prometheus/console_libraries' - '--web.console.templates=/etc/prometheus/consoles' - '--storage.tsdb.path=/prometheus' - '--web.enable-lifecycle' pushgateway: image: prom/pushgateway hostname: pushgateway container_name: pushgateway restart: always ports: - "9091:9091"

目录结构:

 

posted @ 2023-08-10 18:49  KeepSmiling_me  阅读(226)  评论(0)    收藏  举报