jmxtrans docker-compose 運行

如下是一個簡單的demo,使用jmxtrans 進行jmx 指標的處理,項目使用docker-compose 運行
同時寫入數據到graphitejava

環境準備

  • docker-compose文件
 
version: "3"
services: 
  graphite:
    image: graphiteapp/graphite-statsd
    ports:
    - "80:80"
    - "2003-2004:2003-2004"
    - "2023-2024:2023-2024"
    - "8125:8125/udp"
    - "8126:8126"
  jmxtrans:
    build: 
      context: ./
      dockerfile: Dockerfile-jmxtrans
  app:
    build: ./
    image: dalongrong/java-jmx-openjdk
    container_name: app
    ports: 
    - "8080:8080"
    - "30384:30384"
  • dockerfile 說明
    包含了一個spring boot 的項目app
    Dockerfile
 
FROM openjdk:8u222-jdk
LABEL AUTHOR="dalongrong"
LABEL EMAIL="1141591465@qq.com"
WORKDIR /
COPY webapi-0.0.1-SNAPSHOT.jar /webapi-0.0.1-SNAPSHOT.jar
COPY docker-entrypiont.sh /docker-entrypiont.sh
RUN chmod +x /docker-entrypiont.sh
EXPOSE 30384 8080
ENTRYPOINT [ "/docker-entrypiont.sh" ]

docker-entrypiont.sh:git

#!/bin/sh
java \
-Dcom.sun.management.jmxremote \
-Dcom.sun.management.jmxremote.port=30384 \
-Dcom.sun.management.jmxremote.ssl=false \
-Dcom.sun.management.jmxremote.authenticate=false \
-Dcom.sun.management.jmxremote.rmi.port=30384 \
-Dcom.sun.management.jmxremote.local.only=false \
-Dcom.sun.management.jmxremote.host=app \
-Djava.rmi.server.hostname=app \
-jar /webapi-0.0.1-SNAPSHOT.jar

jmxtrans Dockerfile-jmxtransgithub

#!/bin/sh
java -jar /jmxtrans-270-all.jar -f /jmxtrans.json

jmxtrans 配置json 文件jmxtrans.jsonweb

   {
  "servers": [{
    "port": "30384",
    "host": "app",
    "numQueryThreads" : 4,
    "queries": [
      {
        "obj": "Tomcat:type=ThreadPool,name=\"http-nio-8080\"",
        "resultAlias":"http-nio-8080",
        "attr": ["acceptCount","currentThreadsBusy","currentThreadCount"],
        "outputWriters": [{
          "@class": "com.googlecode.jmxtrans.model.output.GraphiteWriterFactory",
          "port": 2003,
          "host": "graphite",
          "rootPrefix":"webapi",
          "typeNames" : ["name"],
          "flushStrategy" :"always",
          "poolSize" : 10
        }]
      },
      {
        "obj": "java.lang:type=Memory",
        "resultAlias":"Memory",
        "attr" : [ "HeapMemoryUsage", "NonHeapMemoryUsage" ],
        "outputWriters": [{
          "@class": "com.googlecode.jmxtrans.model.output.GraphiteWriterFactory",
          "port": 2003,
          "host": "graphite",
          "rootPrefix":"webapi",
          "typeNames" : ["name"],
          "flushStrategy" :"always",
          "poolSize" : 10
        }]
      }
      ,
      {
        "obj": "java.lang:type=ClassLoading",
        "resultAlias":"ClassLoading",
        "attr": ["TotalLoadedClassCount","LoadedClassCount","UnloadedClassCount"],
        "outputWriters": [{
          "@class": "com.googlecode.jmxtrans.model.output.GraphiteWriterFactory",
          "port": 2003,
          "rootPrefix":"webapi",
          "typeNames" : ["name"],
          "flushStrategy" :"always",
          "poolSize" : 10,
          "host": "graphite"
        }]
      }
    ]
  }]
}

配置說明

  • jmx 服務app
    主要是經過運行的時候啓動jmx 配置,具體能夠參考docker-entrypiont.sh
  • jmxtrans
    經過docker 運行,主要是配置文件jmxtrans.json 添加了幾個查詢,同時經過GraphiteWriter
    寫入數據到Graphite
    幾個參數的說明,若是看了官方的wiki 以及代碼就會發現,有幾個參數是必須寫的,可是wiki 不是很清楚
    主要是rootPrefix,flushStrategy,typeNames

啓動&&測試

  • 啓動
 
docker-compose up -d
  • 效果

 

說明

以上是一個簡單的docker-compose 運行,實際上咱們能夠進行擴展,添加更多的jmx 指標,對於kuberntes 的運行方式,能夠參考相似的方式,只是
咱們能夠使用localhost 在pod 中運行多個容器的方式解決,仍是比較方便的spring

參考資料

https://github.com/jmxtrans/jmxtrans/wiki/GraphiteWriter 
https://github.com/jmxtrans/jmxtrans/wiki/Queries 
https://github.com/rongfengliang/openjdk-docker-jmx/tree/jmxtrans 
https://github.com/rongfengliang/jmxtrans-docker-composedocker

相關文章
相關標籤/搜索