使用Spring Boot Actuator插件监控性能

Spring Boot Actuator提供了丰富的Web端点,用于监控和诊断应用的内部状态,如Bean组装、环境属性、运行时度量和请求跟踪。通过/beans、/conditions、/mappings等端点,开发者可以深入了解和调试应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring Boot Actuator的关键特性是在应用程序里提供众多Web端点,通过它们了解应用程序运行时的内部状况。

有了Actuator,你可以知道Bean在Spring应用程序上下文里是如何组装在一起的,掌握应用程序可以获取的环境属性信息,获取运行时度量信息的快照。

1. 简单使用

1. Maven依赖
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2. 修改配置文件application.yml
management:
  endpoints:
    web:
      exposure:
        include: "*"

3. 获取接口apis
http://localhost:8080/actuator/

2. /beans端点产生的报告能告诉你Spring应用程序上下文里都有哪些Bean

{
    "myTimerTask":{
        "aliases":[

        ],
        "scope":"singleton",
        "type":"com.test.spring.timer.controller.MyTimerTask",
        "resource":"file [/Users/didi/mytemps/timerDemo/target/classes/com/test/spring/timer/controller/MyTimerTask.class]",
        "dependencies":[
            "user"
        ]
    }
}

3. /conditions端点能告诉你为什么会有这个Bean,或者为什么没有这个Bean。

{
    "positiveMatches":{
        "AuditAutoConfiguration#auditListener":[
            {
                "condition":"OnBeanCondition",
                "message":"@ConditionalOnMissingBean (types: org.springframework.boot.actuate.audit.listener.AbstractAuditListener; 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'"
                }
            ]
        }
    }
}

4. /mappings罗列出应用程序发布的全部端点


{
    "handler":"public java.lang.String com.test.spring.timer.controller.MyTimerTask.timerTask()",
    "predicate":"{ /outer/timerTask}",
    "details":{
        "handlerMethod":{
            "className":"com.test.spring.timer.controller.MyTimerTask",
            "name":"timerTask",
            "descriptor":"()Ljava/lang/String;"
        },
        "requestMappingConditions":{
            "consumes":[

            ],
            "headers":[

            ],
            "methods":[

            ],
            "params":[

            ],
            "patterns":[
                "/outer/timerTask"
            ],
            "produces":[

            ]
        }
    }
}

5. Actuator提供了一系列端点,让你能在运行时快速检查应用程序

http://localhost:8080/actuator/metrics/jvm.memory.max
{
    "name":"jvm.memory.max",
    "description":"The maximum amount of memory in bytes that can be used for memory management",
    "baseUnit":"bytes",
    "measurements":[
        {
            "statistic":"VALUE",
            "value":3457679359
        }
    ],
    "availableTags":[
        {
            "tag":"area",
            "values":[
                "heap",
                "nonheap"
            ]
        },
        {
            "tag":"id",
            "values":[
                "Compressed Class Space",
                "PS Survivor Space",
                "PS Old Gen",
                "Metaspace",
                "PS Eden Space",
                "Code Cache"
            ]
        }
    ]
}

6. /trace端点能报告所有Web请求的详细信息,包括请求方法、路径、时间戳以及请求和响应的头信息

{
    "timestamp":"2018-12-22T09:10:33.925Z",
    "principal":null,
    "session":null,
    "request":{
        "method":"GET",
        "uri":"http://localhost:8080/actuator",
        "headers":{
            "accept-language":[
                "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7"
            ],
            "postman-token":[
                "9293704b-285a-061a-c0c3-92fe1f6291da"
            ],
            "host":[
                "localhost:8080"
            ],
            "connection":[
                "keep-alive"
            ],
            "cache-control":[
                "no-cache"
            ],
            "accept-encoding":[
                "gzip, deflate, br"
            ],
            "accept":[
                "*/*"
            ],
            "user-agent":[
                "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
            ]
        },
        "remoteAddress":null
    },
    "response":{
        "status":200,
        "headers":{
            "Transfer-Encoding":[
                "chunked"
            ],
            "Date":[
                "Sat, 22 Dec 2018 09:10:33 GMT"
            ],
            "Content-Type":[
                "application/vnd.spring-boot.actuator.v2+json;charset=UTF-8"
            ]
        }
    },
    "timeTaken":1
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值