欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot四大神器之Actuator的使用小结

程序员文章站 2024-04-01 20:39:58
序 spring boot有四大神器,分别是auto-configuration、starters、cli、actuator,本文主要讲actuator。actua...


spring boot有四大神器,分别是auto-configuration、starters、cli、actuator,本文主要讲actuator。actuator是spring boot提供的对应用系统的自省和监控的集成功能,可以对应用系统进行配置查看、相关功能统计等。

spring-boot-starter-actuator模块的实现对于实施微服务的中小团队来说,可以有效地减少监控系统在采集应用指标时的开发量。当然,它也并不是万能的,有时候我们也需要对其做一些简单的扩展来帮助我们实现自身系统个性化的监控需求。下面,在本文中,我们将详解的介绍一些关于spring-boot-starter-actuator模块的内容,包括它的原生提供的端点以及一些常用的扩展和配置方式。

使用actuator

添加依赖

    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-actuator</artifactid>
    </dependency>

主要暴露的功能

http方法 路径 描述 鉴权
get /autoconfig 查看自动配置的使用情况 true
get /configprops 查看配置属性,包括默认配置 true
get /beans 查看bean及其关系列表 true
get /dump 打印线程栈 true
get /env 查看所有环境变量 true
get /env/{name} 查看具体变量值 true
get /health 查看应用健康指标 false
get /info 查看应用信息 false
get /mappings 查看所有url映射 true
get /metrics 查看应用基本指标 true
get /metrics/{name} 查看具体指标 true
post /shutdown 关闭应用 true
get /trace 查看基本追踪信息 true

/autoconfig

{
  "positivematches": {
    "auditautoconfiguration.auditeventrepositoryconfiguration": [
      {
        "condition": "onbeancondition", 
        "message": "@conditionalonmissingbean (types: org.springframework.boot.actuate.audit.auditeventrepository; searchstrategy: all) found no beans"
      }
    ]
  }, 
  "negativematches": {
    "cachestatisticsautoconfiguration": [
      {
        "condition": "onbeancondition", 
        "message": "@conditionalonbean (types: org.springframework.cache.cachemanager; searchstrategy: all) found no beans"
      }
    ]
  }
}

/configprops

{
  "management.health.status.configuration_properties": {
    "prefix": "management.health.status", 
    "properties": {
      "order": null
    }
  }, 
  "multipart.configuration_properties": {
    "prefix": "multipart", 
    "properties": {
      "enabled": false, 
      "maxrequestsize": "10mb", 
      "location": null, 
      "filesizethreshold": "0", 
      "maxfilesize": "1mb"
    }
  }, 
  "environmentendpoint": {
    "prefix": "endpoints.env", 
    "properties": {
      "id": "env", 
      "enabled": true, 
      "sensitive": true
    }
  }
}

/beans

[
  {
    "context": "application:8080", 
    "parent": null, 
    "beans": [
      {
        "bean": "appmain", 
        "scope": "singleton", 
        "type": "com.xixicat.appmain$$enhancerbyspringcglib$$29382b14", 
        "resource": "null", 
        "dependencies": [ ]
      }, 
      {
        "bean": "videoinfomapper", 
        "scope": "singleton", 
        "type": "com.xixicat.dao.videoinfomapper", 
        "resource": "file [/users/xixicat/workspace/video-uber/target/classes/com/xixicat/dao/videoinfomapper.class]", 
        "dependencies": [
          "sqlsessionfactory"
        ]
      }
    ]
  }
]

/dump

[
  {
    "threadname": "signal dispatcher", 
    "threadid": 4, 
    "blockedtime": -1, 
    "blockedcount": 0, 
    "waitedtime": -1, 
    "waitedcount": 0, 
    "lockname": null, 
    "lockownerid": -1, 
    "lockownername": null, 
    "innative": false, 
    "suspended": false, 
    "threadstate": "runnable", 
    "stacktrace": [ ], 
    "lockedmonitors": [ ], 
    "lockedsynchronizers": [ ], 
    "lockinfo": null
  }, 
  {
    "threadname": "reference handler", 
    "threadid": 2, 
    "blockedtime": -1, 
    "blockedcount": 217, 
    "waitedtime": -1, 
    "waitedcount": 9, 
    "lockname": "java.lang.ref.reference$lock@45de945", 
    "lockownerid": -1, 
    "lockownername": null, 
    "innative": false, 
    "suspended": false, 
    "threadstate": "waiting", 
    "stacktrace": [
      {
        "methodname": "wait", 
        "filename": "object.java", 
        "linenumber": -2, 
        "classname": "java.lang.object", 
        "nativemethod": true
      }, 
      {
        "methodname": "wait", 
        "filename": "object.java", 
        "linenumber": 503, 
        "classname": "java.lang.object", 
        "nativemethod": false
      }, 
      {
        "methodname": "run", 
        "filename": "reference.java", 
        "linenumber": 133, 
        "classname": "java.lang.ref.reference$referencehandler", 
        "nativemethod": false
      }
    ], 
    "lockedmonitors": [ ], 
    "lockedsynchronizers": [ ], 
    "lockinfo": {
      "classname": "java.lang.ref.reference$lock", 
      "identityhashcode": 73263429
    }
  }
]

/env

{
 profiles: [],
 server.ports: {
  local.server.port: 8080
 },
 servletcontextinitparams: {},
 systemproperties: {
  java.runtime.name: "java(tm) se runtime environment",
   sun.boot.library.path: "/library/java/javavirtualmachines/jdk1.7.0_79.jdk/contents/home/jre/lib",
   java.vm.version: "24.79-b02",
   gopherproxyset: "false",
   maven.multimoduleprojectdirectory: "/users/xixicat/workspace/video-uber",
   java.vm.vendor: "oracle corporation",
   java.vendor.url: "http://java.oracle.com/",
   guice.disable.misplaced.annotation.check: "true",
   path.separator: ":",
   java.vm.name: "java hotspot(tm) 64-bit server vm",
   file.encoding.pkg: "sun.io",
   user.country: "cn",
   sun.java.launcher: "sun_standard",
   sun.os.patch.level: "unknown",
   pid: "763",
   java.vm.specification.name: "java virtual machine specification",
   user.dir: "/users/xixicat/workspace/video-uber",
   java.runtime.version: "1.7.0_79-b15",
   java.awt.graphicsenv: "sun.awt.cgraphicsenvironment",
   java.endorsed.dirs: "/library/java/javavirtualmachines/jdk1.7.0_79.jdk/contents/home/jre/lib/endorsed",
   os.arch: "x86_64",
   java.io.tmpdir: "/var/folders/tl/xkf4nr61033gd6lk5d3llz080000gn/t/",
   line.separator: " ",
   java.vm.specification.vendor: "oracle corporation",
   os.name: "mac os x",
   classworlds.conf: "/users/xixicat/devtool/maven-3.3.3/bin/m2.conf",
   sun.jnu.encoding: "utf-8",
   spring.beaninfo.ignore: "true",
   java.library.path: "/users/xixicat/library/java/extensions:/library/java/extensions:/network/library/java/extensions:/system/library/java/extensions:/usr/lib/java:.",
   java.specification.name: "java platform api specification",
   java.class.version: "51.0",
   sun.management.compiler: "hotspot 64-bit tiered compilers",
   os.version: "10.10.5",
   user.home: "/users/xixicat",
   user.timezone: "asia/shanghai",
   java.awt.printerjob: "sun.lwawt.macosx.cprinterjob",
   file.encoding: "utf-8",
   java.specification.version: "1.7",
   java.class.path: "/users/xixicat/devtool/maven-3.3.3/boot/plexus-classworlds-2.5.2.jar",
   user.name: "xixicat",
   java.vm.specification.version: "1.7",
   sun.java.command: "org.codehaus.plexus.classworlds.launcher.launcher spring-boot:run",
   java.home: "/library/java/javavirtualmachines/jdk1.7.0_79.jdk/contents/home/jre",
   sun.arch.data.model: "64",
   user.language: "zh",
   java.specification.vendor: "oracle corporation",
   awt.toolkit: "sun.lwawt.macosx.lwctoolkit",
   java.vm.info: "mixed mode",
   java.version: "1.7.0_79",
   java.ext.dirs: "/users/xixicat/library/java/extensions:/library/java/javavirtualmachines/jdk1.7.0_79.jdk/contents/home/jre/lib/ext:/library/java/extensions:/network/library/java/extensions:/system/library/java/extensions:/usr/lib/java",
   sun.boot.class.path: "/library/java/javavirtualmachines/jdk1.7.0_79.jdk/contents/home/jre/lib/resources.jar:/library/java/javavirtualmachines/jdk1.7.0_79.jdk/contents/home/jre/lib/rt.jar:/library/java/javavirtualmachines/jdk1.7.0_79.jdk/contents/home/jre/lib/sunrsasign.jar:/library/java/javavirtualmachines/jdk1.7.0_79.jdk/contents/home/jre/lib/jsse.jar:/library/java/javavirtualmachines/jdk1.7.0_79.jdk/contents/home/jre/lib/jce.jar:/library/java/javavirtualmachines/jdk1.7.0_79.jdk/contents/home/jre/lib/charsets.jar:/library/java/javavirtualmachines/jdk1.7.0_79.jdk/contents/home/jre/lib/jfr.jar:/library/java/javavirtualmachines/jdk1.7.0_79.jdk/contents/home/jre/classes",
   java.awt.headless: "true",
   java.vendor: "oracle corporation",
   maven.home: "/users/xixicat/devtool/maven-3.3.3",
   file.separator: "/",
   log_exception_conversion_word: "%wex",
   java.vendor.url.bug: "http://bugreport.sun.com/bugreport/",
   sun.io.unicode.encoding: "unicodebig",
   sun.cpu.endian: "little",
   sun.cpu.isalist: ""
 },
 systemenvironment: {
  term: "xterm-256color",
  zsh: "/users/xixicat/.oh-my-zsh",
  gvm_broker_service: "http://release.gvm.io",
  griffon_home: "/users/xixicat/.gvm/griffon/current",
  java_main_class_763: "org.codehaus.plexus.classworlds.launcher.launcher",
  java_home: "/library/java/javavirtualmachines/jdk1.7.0_79.jdk/contents/home",
  shlvl: "1",
  maven_cmd_line_args: " spring-boot:run",
  __cf_user_text_encoding: "0x1f5:0x19:0x34",
  groovy_home: "/users/xixicat/.gvm/groovy/current",
  xpc_flags: "0x0",
  gvm_init: "true",
  jbake_home: "/users/xixicat/.gvm/jbake/current",
  pwd: "/users/xixicat/workspace/video-uber",
  gvm_dir: "/users/xixicat/.gvm",
  gvm_version: "2.4.3",
  maven_projectbasedir: "/users/xixicat/workspace/video-uber",
  logname: "xixicat",
  ssh_auth_sock: "/private/tmp/com.apple.launchd.93xr1duecq/listeners",
  springboot_home: "/users/xixicat/.gvm/springboot/current",
  gaiden_home: "/users/xixicat/.gvm/gaiden/current",
  lazybones_home: "/users/xixicat/.gvm/lazybones/current",
  oldpwd: "/users/xixicat/workspace/video-uber",
  shell: "/bin/zsh",
  jbossforge_home: "/users/xixicat/.gvm/jbossforge/current",
  lc_ctype: "zh_cn.utf-8",
  tmpdir: "/var/folders/tl/xkf4nr61033gd6lk5d3llz080000gn/t/",
  gvm_service: "http://api.gvmtool.net",
  gvm_platform: "darwin",
  classpath: ".:/users/xixicat/.m2/repository/co/paralleluniverse/quasar-core/0.7.2/quasar-core-0.7.2.jar",
  glide_home: "/users/xixicat/.gvm/glide/current",
  path: "/users/xixicat/.gvm/vertx/current/bin:/users/xixicat/.gvm/springboot/current/bin:/users/xixicat/.gvm/lazybones/current/bin:/users/xixicat/.gvm/jbossforge/current/bin:/users/xixicat/.gvm/jbake/current/bin:/users/xixicat/.gvm/groovyserv/current/bin:/users/xixicat/.gvm/groovy/current/bin:/users/xixicat/.gvm/griffon/current/bin:/users/xixicat/.gvm/grails/current/bin:/users/xixicat/.gvm/gradle/current/bin:/users/xixicat/.gvm/glide/current/bin:/users/xixicat/.gvm/gaiden/current/bin:/users/xixicat/.gvm/crash/current/bin:/users/xixicat/.gvm/asciidoctorj/current/bin:/users/xixicat/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/git/bin:/users/xixicat/devtool/maven-3.3.3/bin:/users/xixicat/devtool/gradle-2.6/bin:/users/xixicat/devtool/android-sdk/platform-tools",
  gradle_home: "/users/xixicat/.gvm/gradle/current",
  groovyserv_home: "/users/xixicat/.gvm/groovyserv/current",
  grails_home: "/users/xixicat/.gvm/grails/current",
  user: "xixicat",
  less: "-r",
  pager: "less",
  home: "/users/xixicat",
  crash_home: "/users/xixicat/.gvm/crash/current",
  xpc_service_name: "0",
  vertx_home: "/users/xixicat/.gvm/vertx/current",
  gvm_broadcast_service: "http://cast.gvm.io",
  apple_pubsub_socket_render: "/private/tmp/com.apple.launchd.y6fnwp8sk6/render",
  lscolors: "gxfxcxdxbxegedabagacad",
  asciidoctorj_home: "/users/xixicat/.gvm/asciidoctorj/current"
 },
 applicationconfig: [classpath: /application.properties]: {
  pool.acquireincrement: "1",
  pool.minpoolsize: "5",
  pool.initialpoolsize: "1",
  database.username: "root",
  pool.maxidletime: "60",
  database.url: "jdbc:mysql://127.0.0.1:3307/video_uber?autoreconnect=true&useunicode=true&characterencoding=utf-8&zerodatetimebehavior=converttonull",
  spring.jackson.dateformat: "yyyy-mm-dd't'hh:mm:ss",
  database.slave.username: "root",
  spring.jackson.serialization.write - dates - as - timestamps: "false",
  pool.idletimeout: "30000",
  database.slave.url: "jdbc:mysql://127.0.0.1:3307/demo?autoreconnect=true&useunicode=true&characterencoding=utf-8&zerodatetimebehavior=converttonull",
  server.port: "8080",
  database.slave.password: "******",
  database.password: "******",
  database.driverclassname: "com.mysql.jdbc.driver",
  pool.maxpoolsize: "50",
  database.datasourceclassname: "com.mysql.jdbc.jdbc2.optional.mysqldatasource"
 }
}

/health

{
 status: "up",
 diskspace: {
  status: "up",
  total: 249779191808,
  free: 193741590528,
  threshold: 10485760
 },
 db: {
  status: "up",
  database: "mysql",
  hello: 1
 }
}

/info

需要自己在application.properties里头添加信息,比如

info:
 contact:
  email: xixicat@gmail.com
  phone: 0755-82890987

然后请求就可以出来了

{
 "contact": {
   "phone": "0755-82890987",
   "email": "xixicat@gmail.com"
 }
}

/mappings

   {
    {
     [/metrics || /metrics.json], methods = [get], produces = [application / json]
    }: {
     bean: "endpointhandlermapping",
     method: "public java.lang.object org.springframework.boot.actuate.endpoint.mvc.endpointmvcadapter.invoke()"
    }, {
     [/beans || /beans.json], methods = [get], produces = [application / json]
    }: {
     bean: "endpointhandlermapping",
     method: "public java.lang.object org.springframework.boot.actuate.endpoint.mvc.endpointmvcadapter.invoke()"
    }, {
     [/health || /health.json], produces = [application / json]
    }: {
     bean: "endpointhandlermapping",
     method: "public java.lang.object org.springframework.boot.actuate.endpoint.mvc.healthmvcendpoint.invoke(java.security.principal)"
    }, {
     [/info || /info.json], methods = [get], produces = [application / json]
    }: {
     bean: "endpointhandlermapping",
     method: "public java.lang.object org.springframework.boot.actuate.endpoint.mvc.endpointmvcadapter.invoke()"
    }, {
     [/trace || /trace.json], methods = [get], produces = [application / json]
    }: {
     bean: "endpointhandlermapping",
     method: "public java.lang.object org.springframework.boot.actuate.endpoint.mvc.endpointmvcadapter.invoke()"
    }, {
     [/autoconfig || /autoconfig.json], methods = [get], produces = [application / json]
    }: {
     bean: "endpointhandlermapping",
     method: "public java.lang.object org.springframework.boot.actuate.endpoint.mvc.endpointmvcadapter.invoke()"
    }
   }

/metrics

{
mem: 499404,
mem.free: 257591,
processors: 8,
instance.uptime: 4284997,
uptime: 4294909,
systemload.average: 1.84521484375,
heap.committed: 437248,
heap.init: 262144,
heap.used: 179656,
heap: 3728384,
nonheap.committed: 62848,
nonheap.init: 24000,
nonheap.used: 62156,
nonheap: 133120,
threads.peak: 18,
threads.daemon: 6,
threads.totalstarted: 176,
threads: 16,
classes: 10294,
classes.loaded: 10294,
classes.unloaded: 0,
gc.ps_scavenge.count: 11,
gc.ps_scavenge.time: 405,
gc.ps_marksweep.count: 0,
gc.ps_marksweep.time: 0,
datasource.primary.active: 0,
datasource.primary.usage: 0,
counter.status.200.autoconfig: 1,
counter.status.200.beans: 1,
counter.status.200.configprops: 1,
counter.status.200.dump: 1,
counter.status.200.env: 1,
counter.status.200.health: 1,
counter.status.200.info: 1,
counter.status.200.mappings: 1,
gauge.response.autoconfig: 81,
gauge.response.beans: 15,
gauge.response.configprops: 105,
gauge.response.dump: 76,
gauge.response.env: 4,
gauge.response.health: 43,
gauge.response.info: 1,
gauge.response.mappings: 4
}

/shutdown

要真正生效,得配置文件开启

endpoints.shutdown.enabled: true

/trace

记录最近100个请求的信息

[{
 "timestamp": 1452955704922,
 "info": {
  "method": "get",
  "path": "/metrics",
  "headers": {
   "request": {
    "accept - encoding": "gzip, deflate, sdch",
     "upgrade - insecure - requests": "1",
     "accept - language": "zh-cn,zh;q=0.8,en;q=0.6",
     "user - agent": "mozilla/5.0 (macintosh; intel mac os x 10_10_5) applewebkit/537.36 (khtml, like gecko) chrome/47.0.2526.111 safari/537.36",
     "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
     "connection": "keep-alive",
     "host": "localhost:8080"
   },
   "response": {
    "content - type": "application/json; charset=utf-8",
     "x - application - context": "application:8080",
     "date": "sat, 16 jan 2016 14:48:24 gmt",
     "status": "200"
   }
  }
 }
}, {
 "timestamp": 1452951489549,
 "info": {
  "method": "get",
  "path": "/autoconfig",
  "headers": {
   "request": {
    "accept - encoding": "gzip, deflate, sdch",
     "upgrade - insecure - requests": "1",
     "accept - language": "zh-cn,zh;q=0.8,en;q=0.6",
     "user - agent": "mozilla/5.0 (macintosh; intel mac os x 10_10_5) applewebkit/537.36 (khtml, like gecko) chrome/47.0.2526.111 safari/537.36",
     "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
     "connection": "keep-alive",
     "host": "localhost:8080"
   },
   "response": {
    "content - type": "application/json; charset=utf-8",
     "x - application - context": "application:8080",
     "date": "sat, 16 jan 2016 13:38:09 gmt",
     "status": "200"
   }
  }
 }
}]

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。