Spring Boot & Actuator

SpringBoot自带监控功能Actuator,可以帮助实现对程序内部运行情况监控,比如监控状况、Bean加载情况、环境变量、日志信息、线程信息等

  • Spring Boot七分钟快速实践
  • Spring Boot & MyBatis
  • Spring Boot & Redis
  • Spring Boot & Swagger
  • Spring Boot & 单元测试
  • Spring Boot & Actuator
  • Spring Boot Admin

配置Actuator

  • pom.xml

    org.springframework.boot
    spring-boot-starter-actuator

  • 浏览器打开链接http://localhost:8080/actuator/
    可以看到所有支持的连接,默认只有
/actuator
/actuator/health
/actuator/health/{component}
/actuator/health/{component}/{instance}
/actuator/info
  • bean加载情况http://localhost:8080/actuator/beans
{
    "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"
                    ]
                }
            }
        }
    }
}
  • 环境变量http://localhost:8080/actuator/env
{
    "activeProfiles":[

    ],
    "propertySources":[
        {
            "name":"server.ports",
            "properties":{
                "local.server.port":{
                    "value":8080
                }
            }
        },
        {
            "name":"servletContextInitParams",
            "properties":{

            }
        },
        {
            "name":"systemProperties",
            "properties":{
                "java.vendor":{
                    "value":"Oracle Corporation"
                },
                "sun.java.launcher":{
                    "value":"SUN_STANDARD"
                },
                "catalina.base":{
                    "value":"C:%users\timxia\AppData\Local\Temp\tomcat.2979281870254394426.8080"
                }
            }
        }
    ]
}

常用配置

  • 如果要看到所有支持的状态查询,需要配置
management.endpoints.web.exposure.include=*
  • 显示所有健康状态,需要加配置
management.endpoint.health.show-details=always

结果

{
    "status":"UP",
    "details":{
        "db":{
            "status":"UP",
            "details":{
                "database":"MySQL",
                "hello":1
            }
        },
        "diskSpace":{
            "status":"UP",
            "details":{
                "total":335067213824,
                "free":241971175424,
                "threshold":10485760
            }
        },
        "redis":{
            "status":"UP",
            "details":{
                "version":"3.2.12"
            }
        }
    }
}

启用端点

默认情况下,除shutdown以外的所有端点均已启用。要配置单个端点的启用,请使用management.endpoint..enabled属性。以下示例启用shutdown端点:

management.endpoint.shutdown.enabled=true

另外可以通过management.endpoints.enabled-by-default来修改全局端口默认配置,以下示例启用info端点并禁用所有其他端点:

management.endpoints.enabled-by-default=false
management.endpoint.info.enabled=true

注意
禁用的端点将从应用程序上下文中完全删除。如果您只想更改端点公开(对外暴露)的技术,请改为使用include和exclude属性,详情见下文

暴露端点

要更改公开哪些端点,请使用以下技术特定的include和exclude属性:

Property Default
management.endpoints.jmx.exposure.exclude *
management.endpoints.jmx.exposure.include *
management.endpoints.web.exposure.exclude *
management.endpoints.web.exposure.include info, health

include属性列出了公开的端点的ID,exclude属性列出了不应该公开的端点的ID
exclude属性优先于include属性。包含和排除属性都可以使用端点ID列表进行配置。

注意
这里的优先级是指同一端点ID,同时出现在include属性表和exclude属性表里,exclude属性优先于include属性,即此端点没有暴露

  • 例如,要停止通过JMX公开所有端点并仅公开healthinfo端点,请使用以下属性:
management.endpoints.jmx.exposure.include=health,info

*可以用来选择所有端点。例如,要通过HTTP公开除envbeans端点之外的所有内容,请使用以下属性:

management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env,beans

参考

  • spring boot 2 使用 actuator 404的问题
  • Spring Boot 2.0官方文档之 Actuator

你可能感兴趣的:(Spring Boot & Actuator)