Spring Boot Actuator应用监控

介绍

Spring boot Actuator 提供了一系列的端点来监控和度量应用,包括应用上下文全部Bean信息,自动化配置报告(显示自动配置条件是否通过),配置属性信息,显示应用健康状态等。

环境

Spring Boot 2.0.3

测试

1.初始化一个Spring boot工程,添加actuator依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2.添加响应的配置信息(注意:Spring Boot 2和1.x版本相比,不少配置发生修改,废弃不少1.x版本中的属性)

server:
  port: 8080

# actuator监控
management:
  server:
    # 设置监控服务端口
    port: 8081
  endpoints:
    # 设置端点是否可用 默认只有shutdown可用
    enabled-by-default: true
    web:
      # 设置是否暴露端点 默认只有health和info可见
      exposure:
        # 包括所有端点
        include: "*" # 注意需要添加引号
        # 排除端点
        exclude: shutdown

Spring Boot内置端点(和1.x版本相比,不少端点名称发生改变)
Spring Boot内置端点

Web应用额外的端点:
Web应用额外端点

访问端点需添加前缀/actuator,例如访问beans端点地址为/actuator/beans

/beans:获取应用上下文所有的bean信息

{
    contexts: {
        application: {
            beans: {
                endpointCachingOperationInvokerAdvisor: {
                    aliases: [ ], 
                    scope: "singleton", 
                    type: "org.springframework.boot.actuate.endpoint.invoker.cache.CachingOperationInvokerAdvisor", 
                    resource: "class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/EndpointAutoConfiguration.class]", 
                    dependencies: [
                        "environment"
                    ]
                }, 
                defaultServletHandlerMapping: {
                    aliases: [ ], 
                    scope: "singleton", 
                    type: "org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport$EmptyHandlerMapping", 
                    resource: "class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]", 
                    dependencies: [ ]
                }, 
                org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter$FaviconConfiguration: {
                    aliases: [ ], 
                    scope: "singleton", 
                    type: "org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter$FaviconConfiguration$$EnhancerBySpringCGLIB$$4337b2d4", 
                    resource: null, 
                    dependencies: [
                        "spring.resources-org.springframework.boot.autoconfigure.web.ResourceProperties"
                    ]
                }
            }
        }
    }
}
  • aliases:Bean的别名
  • scope:Bean的作用域,默认单例
  • type:Bean的Java类型
  • reource:class文件的具体路径
  • dependencies:依赖的Bean列表

/health:显示应用程序健康信息

{
    status: "UP"
}

/conditions:获取自动化配置信息,以及是否满足条件的原因

{
    contexts: {
        application: {
            positiveMatches: {
                AuditAutoConfiguration#auditListener: [
                    {
                        condition: "OnBeanCondition",
                        message: "@ConditionalOnMissingBean (types: org.springframework.boot.actuate.audit.listener.AbstractAuditListener; SearchStrategy: all) did not find any beans"
                    }
                ],
                AuditAutoConfiguration.AuditEventRepositoryConfiguration: [
                    {
                        condition: "OnBeanCondition",
                        message: "@ConditionalOnMissingBean (types: org.springframework.boot.actuate.audit.AuditEventRepository;
                        SearchStrategy: all) did not find any beans"
                    }
                ]
            },
            negativeMatches: {
                RabbitHealthIndicatorAutoConfiguration: {
                    notMatched: [
                        {
                            condition: "OnClassCondition",
                            message: "@ConditionalOnClass did not find required class 'org.springframework.amqp.rabbit.core.RabbitTemplate'"
                        }
                    ],
                    matched: [ ]
                },
                AuditAutoConfiguration#authenticationAuditListener: {
                    notMatched: [
                        {
                            condition: "OnClassCondition",
                            message: "@ConditionalOnClass did not find required class 'org.springframework.security.authentication.event.AbstractAuthenticationEvent'"
                        }
                    ],
                    matched: [ ]
                }
            }
        }
    }
}
  • positiveMatches:满足条件的自动化配置
  • negativeMatches:不满足条件的自动化配置

/configprops:显示自动化配置属性信息

{
    contexts: {
        application: {
            beans: {
                server-org.springframework.boot.autoconfigure.web.ServerProperties: {
                    prefix: "server", 
                    properties: {
                        undertow: {
                            maxHttpPostSize: 0, 
                            eagerFilterInit: true, 
                            accesslog: {
                                enabled: false, 
                                pattern: "common", 
                                prefix: "access_log.", 
                                suffix: "log", 
                                dir: "E:\\IDEA_Project\\practice\\spring-boot-learning\\spring-boot-actuator\\logs", 
                                rotate: true
                            }
                        }, 
                        port: 8080, 
                        maxHttpHeaderSize: 0, 
                        tomcat: {
                            accesslog: {
                                enabled: false, 
                                pattern: "common", 
                                directory: "logs", 
                                prefix: "access_log", 
                                suffix: ".log", 
                                rotate: true, 
                                renameOnRotate: false, 
                                fileDateFormat: ".yyyy-MM-dd", 
                                requestAttributesEnabled: false, 
                                buffered: true
                            }, 
                            internalProxies: "10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|192\\.168\\.\\d{1,3}\\.\\d{1,3}|169\\.254\\.\\d{1,3}\\.\\d{1,3}|127\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|172\\.1[6-9]{1}\\.\\d{1,3}\\.\\d{1,3}|172\\.2[0-9]{1}\\.\\d{1,3}\\.\\d{1,3}|172\\.3[0-1]{1}\\.\\d{1,3}\\.\\d{1,3}", 
                            protocolHeaderHttpsValue: "https", 
                            portHeader: "X-Forwarded-Port", 
                            backgroundProcessorDelay: {
                                units: [
                                    "SECONDS", 
                                    "NANOS"
                                ]
                            }, 
                            maxThreads: 0, 
                            minSpareThreads: 0, 
                            maxHttpPostSize: 0, 
                            maxHttpHeaderSize: 0, 
                            maxConnections: 0, 
                            acceptCount: 0, 
                            additionalTldSkipPatterns: [ ], 
                            resource: { }
                        }, 
                        servlet: {
                            contextParameters: { }, 
                            applicationDisplayName: "application", 
                            path: "/"
                        }, 
                        jetty: {
                            accesslog: {
                                enabled: false, 
                                retentionPeriod: 31, 
                                append: false, 
                                extendedFormat: false, 
                                dateFormat: "dd/MMM/yyyy:HH:mm:ss Z", 
                                timeZone: "GMT", 
                                logCookies: false, 
                                logServer: false, 
                                logLatency: false
                            }, 
                            maxHttpPostSize: 0
                        }, 
                        error: {
                            path: "/error", 
                            includeException: false, 
                            includeStacktrace: "NEVER", 
                            whitelabel: {
                                enabled: true
                            }
                        }
                    }
                }
            }, 
            parentId: null
        }
    }
}

/env:显示系统环境配置信息

{
    activeProfiles: [ ], 
    propertySources: [
        {
            name: "server.ports", 
            properties: {
                local.management.port: {
                    value: 8081
                }, 
                local.server.port: {
                    value: 8080
                }
            }
        }, 
        {
            name: "servletContextInitParams", 
            properties: { }
        }, 
        {
            name: "systemProperties", 
            properties: {
                com.sun.management.jmxremote.authenticate: {
                    value: "false"
                }, 
                java.runtime.name: {
                    value: "Java(TM) SE Runtime Environment"
                }
            }
        }, 
        {
            name: "systemEnvironment", 
            properties: {
                USERDOMAIN_ROAMINGPROFILE: {
                    value: "SYLVIA-PC", 
                    origin: "System Environment Property \"USERDOMAIN_ROAMINGPROFILE\""
                }, 
                LOCALAPPDATA: {
                    value: "C:\\Users\\Sylvia\\AppData\\Local", 
                    origin: "System Environment Property \"LOCALAPPDATA\""
                }
            }
        }, 
        {
            name: "applicationConfig: [classpath:/application.yml]", 
            properties: {
                server.port: {
                    value: 8080, 
                    origin: "class path resource [application.yml]:2:9"
                }, 
                management.server.port: {
                    value: 8081, 
                    origin: "class path resource [application.yml]:8:11"
                }, 
                management.endpoints.enabled-by-default: {
                    value: true, 
                    origin: "class path resource [application.yml]:11:25"
                }, 
                management.endpoints.web.exposure.include: {
                    value: "*", 
                    origin: "class path resource [application.yml]:16:18"
                }, 
                management.endpoints.web.exposure.exclude: {
                    value: "shutdown", 
                    origin: "class path resource [application.yml]:18:18"
                }
            }
        }
    ]
}

/mappings:查看控制器映射关系

{
    contexts: {
        application: {
            mappings: {
                dispatcherServlets: {
                    dispatcherServlet: [
                        {
                            handler: "ResourceHttpRequestHandler [locations=[class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/], ServletContext resource [/], class path resource []], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@2edb52aa]]", 
                            predicate: "/**/favicon.ico", 
                            details: null
                        }, 
                        {
                            handler: "public java.lang.String com.laravelshao.springboot.ActuatorApplication.demo()", 
                            predicate: "{[/demo]}", 
                            details: {
                                handlerMethod: {
                                    className: "com.laravelshao.springboot.ActuatorApplication", 
                                    name: "demo", 
                                    descriptor: "()Ljava/lang/String;"
                                }, 
                                requestMappingConditions: {
                                    consumes: [ ], 
                                    headers: [ ], 
                                    methods: [ ], 
                                    params: [ ], 
                                    patterns: [
                                        "/demo"
                                    ], 
                                    produces: [ ]
                                }
                            }
                        }, 
                        {
                            handler: "public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)", 
                            predicate: "{[/error]}", 
                            details: {
                                handlerMethod: {
                                    className: "org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController", 
                                    name: "error", 
                                    descriptor: "(Ljavax/servlet/http/HttpServletRequest;)Lorg/springframework/http/ResponseEntity;"
                                }, 
                                requestMappingConditions: {
                                    consumes: [ ], 
                                    headers: [ ], 
                                    methods: [ ], 
                                    params: [ ], 
                                    patterns: [
                                        "/error"
                                    ], 
                                    produces: [ ]
                                }
                            }
                        }, 
                        {
                            handler: "public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)", 
                            predicate: "{[/error],produces=[text/html]}", 
                            details: {
                                handlerMethod: {
                                    className: "org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController", 
                                    name: "errorHtml", 
                                    descriptor: "(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;)Lorg/springframework/web/servlet/ModelAndView;"
                                }, 
                                requestMappingConditions: {
                                    consumes: [ ], 
                                    headers: [ ], 
                                    methods: [ ], 
                                    params: [ ], 
                                    patterns: [
                                        "/error"
                                    ], 
                                    produces: [
                                        {
                                            mediaType: "text/html", 
                                            negated: false
                                        }
                                    ]
                                }
                            }
                        }, 
                        {
                            handler: "ResourceHttpRequestHandler [locations=[class path resource [META-INF/resources/webjars/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@7cc74785]]", 
                            predicate: "/webjars/**", 
                            details: null
                        }, 
                        {
                            handler: "ResourceHttpRequestHandler [locations=[class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/], ServletContext resource [/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@2a09788d]]", 
                            predicate: "/**", 
                            details: null
                        }
                    ]
                }, 
                servletFilters: [
                    {
                        servletNameMappings: [ ], 
                        urlPatternMappings: [
                            "/*"
                        ], 
                        name: "requestContextFilter", 
                        className: "org.springframework.boot.web.servlet.filter.OrderedRequestContextFilter"
                    }, 
                    {
                        servletNameMappings: [ ], 
                        urlPatternMappings: [
                            "/*"
                        ], 
                        name: "characterEncodingFilter", 
                        className: "org.springframework.boot.web.servlet.filter.OrderedCharacterEncodingFilter"
                    }
                ], 
                servlets: [
                    {
                        mappings: [ ], 
                        name: "default", 
                        className: "org.apache.catalina.servlets.DefaultServlet"
                    }, 
                    {
                        mappings: [
                            "/"
                        ], 
                        name: "dispatcherServlet", 
                        className: "org.springframework.web.servlet.DispatcherServlet"
                    }
                ]
            }, 
            parentId: null
        }
    }
}

/metrics:提供运行时度量信息,包括内存,堆栈,线程等信息

{
    names: [
        "jvm.memory.max", 
        "jvm.gc.memory.promoted", 
        "tomcat.cache.hit", 
        "tomcat.cache.access", 
        "jvm.memory.used", 
        "jvm.gc.max.data.size", 
        "jvm.gc.pause", 
        "jvm.memory.committed", 
        "system.cpu.count", 
        "logback.events", 
        "tomcat.global.sent", 
        "jvm.buffer.memory.used", 
        "tomcat.sessions.created", 
        "jvm.threads.daemon", 
        "system.cpu.usage", 
        "jvm.gc.memory.allocated", 
        "tomcat.global.request.max", 
        "tomcat.global.request", 
        "tomcat.sessions.expired", 
        "jvm.threads.live", 
        "jvm.threads.peak", 
        "tomcat.global.received", 
        "process.uptime", 
        "tomcat.sessions.rejected", 
        "process.cpu.usage", 
        "tomcat.threads.config.max", 
        "jvm.classes.loaded", 
        "jvm.classes.unloaded", 
        "tomcat.global.error", 
        "tomcat.sessions.active.current", 
        "tomcat.sessions.alive.max", 
        "jvm.gc.live.data.size", 
        "tomcat.servlet.request.max", 
        "tomcat.threads.current", 
        "tomcat.servlet.request", 
        "jvm.buffer.count", 
        "jvm.buffer.total.capacity", 
        "tomcat.sessions.active.max", 
        "tomcat.threads.busy", 
        "process.start.time", 
        "tomcat.servlet.error"
    ]
}

/httptrace:追踪web请求的详细信息,包括请求方法、路径、时间戳一级请求和响应的头信息等

{
    traces: [
        {
            timestamp: "2018-06-17T06:54:07.813Z", 
            principal: null, 
            session: null, 
            request: {
                method: "GET", 
                uri: "http://localhost:8080/demo", 
                headers: {
                    host: [
                        "localhost:8080"
                    ], 
                    connection: [
                        "keep-alive"
                    ], 
                    upgrade-insecure-requests: [
                        "1"
                    ], 
                    user-agent: [
                        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36"
                    ], 
                    accept: [
                        "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"
                    ], 
                    accept-encoding: [
                        "gzip, deflate, br"
                    ], 
                    accept-language: [
                        "zh-CN,zh;q=0.9,ja;q=0.8,ru;q=0.7,en;q=0.6"
                    ]
                }, 
                remoteAddress: null
            }, 
            response: {
                status: 200, 
                headers: {
                    Content-Type: [
                        "text/html;charset=UTF-8"
                    ], 
                    Content-Length: [
                        "14"
                    ], 
                    Date: [
                        "Sun, 17 Jun 2018 06:54:07 GMT"
                    ]
                }
            }, 
            timeTaken: 35
        }
    ]
}

/threaddump:查看应用线程信息

{
    threads: [
        {
            threadName: "DestroyJavaVM",
            threadId: 73,
            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
        }
    ]
}

/shutdown:关闭应用程序(需使用POST请求)

参考资料

Spring Boot 实战
https://docs.spring.io/spring-boot/docs/2.0.3.RELEASE/reference/htmlsingle/#production-ready
http://blog.didispace.com/spring-boot-actuator-1/

猜你喜欢

转载自blog.csdn.net/laravelshao/article/details/80718846