jmxtrans docker-compose 运行

以下是一个简单的demo,使用jmxtrans 进行jmx 指标的处理,项目使用docker-compose 运行
同时写入数据到graphite

环境准备

  • 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="[email protected]"
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:

#!/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-jmxtrans

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

jmxtrans 配置json 文件jmxtrans.json

   {
  "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 中运行多个容器的方式解决,还是比较方便的

参考资料

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

猜你喜欢

转载自www.cnblogs.com/rongfengliang/p/11223629.html