如下是一個簡單的demo,使用jmxtrans 進行jmx 指標的處理,項目使用docker-compose 運行
同時寫入數據到graphitejava
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"
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"
}]
}
]
}]
}
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