springboot整合actuator-1

本文介绍SpringBoot中Actuator模块的应用,通过整合Actuator,可以有效减少监控系统的开发工作量,适用于微服务架构下的应用监控与管理。文章详细讲解了如何在SpringBoot项目中引入并使用Actuator。

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

springboot整合actuator-1

书库链接: http://know.mobile.himygirl.cn/topic/spring/springboot-actuator/springboot-actuator1.html

一 简介

在Spring Boot的众多模块中,actuator(spring-boot-starter-actuator) 不同于其他模块,它完全是一个用于暴露自身信息的模块,主要作用是用于监控与管理

对于实施微服务的中小团队来说,可以有效地减少监控系统在采集应用指标时的开发量。
当然,它也并不是万能的,有时候需要对其做一些简单的扩展来实现个性化的监控需求。

[!info] 总结:actuator主要应用在监控与管理,极大减少监控系统的开发

二 springboot整合actutor

1 示例环境

工具版本或描述
JDK1.8
IDEIntelliJ IDEA
Maven3.x
spring-boot-starter2.1.6.RELEASE

2 maven依赖

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

引入依赖启动应用后台,会看到控制台打印出如下信息

... o.s.b.a.e.web.EndpointLinksResolver      : Exposing 20 endpoint(s) beneath base path '/actuator'

3 验证请求

[!NOTE]
Springboot2.x所有endpoints默认情况下都已移至/actuator
Springboot1.x所有的endpoints直接访问

# 请求
curl -X GET "http://localhost:8080/actuator"

**响应示例:**
```json
{
    "_links": {
        "self": {
            "href": "http://localhost:8080/actuator",
            "templated": false
        },
        "health": {
            "href": "http://localhost:8080/actuator/health",
            "templated": false
        },
        "health-component-instance": {
            "href": "http://localhost:8080/actuator/health/{component}/{instance}",
            "templated": true
        },
        "health-component": {
            "href": "http://localhost:8080/actuator/health/{component}",
            "templated": true
        },
        "info": {
            "href": "http://localhost:8080/actuator/info",
            "templated": false
        }
    }
}

[!NOTE] 上面是默认暴露的接口,如果你想修改,更改配置application.yml如下:

management:
  endpoints:
    web:
      exposure:
        include: "*"

配置之后重新启动访问,即可看到所有的配置,也可以选择性暴露,示例:"info,env"

4 常见暴露端口释义:

endpoint描述
autoconfig显示一个auto-configuration的报告,可以分析候选者有没有应用的原因
beans显示所有spring Beans的完整列表
configprops显示一个@ConfigurationProperties的列表
dump执行一个线程存储env暴露来自spring ConfigurableEnvironment的属性
health应用健康检查接口info显示应用的配置信息metrics展示当前应用的指标信息
mappings显示一个@RequestMapping路径的相关信息
shutdown允许应用优雅的方式关闭,默认不开启
trace显示trace信息(默认为最新一些http请求)

FAQ:

There was an unexpected error (type=Unauthorized, status=401). Full authentication is required to access this resource

解决方案:

配置文件中添加参数 management.security.enabled=false

源码

https://github.com/dean-coding/java-framework-started-awesome/blob/master/springboot-actuator/README.md

### Spring Boot 集成 `dynamic-tp-spring-boot-starter` 实现 Web 服务器线程池动态配置和监控 #### 添加依赖项 为了在 Spring Boot 中集成 `dynamic-tp-spring-boot-starter` 并实现 Tomcat 线程池的动态管理,需先引入必要的 Maven 或 Gradle 依赖。 对于 Maven 用户,在项目的 `pom.xml` 文件中加入如下依赖: ```xml <dependency> <groupId>com.github.lihengming</groupId> <artifactId>dynamic-tp-spring-boot-starter</artifactId> <version>1.0.7.RELEASE</version> </dependency> ``` 对于 Gradle 用户,则应在 `build.gradle` 文件内添加此行代码: ```groovy implementation 'com.github.lihengming:dynamic-tp-spring-boot-starter:1.0.7.RELEASE' ``` #### 应用程序属性设置 接下来通过修改 `application.properties` 或者 `application.yml` 来定义初始参数。以下是 YAML 格式的示例配置文件片段: ```yaml spring: main: allow-bean-definition-overriding: true dynamicTp: enabled: true tomcat: corePoolSize: 10 # 初始核心线程数 maxPoolSize: 20 # 最大允许创建的线程数量 queueCapacity: 50 # 请求队列容量 keepAliveSeconds: 60 # 空闲线程存活时间(秒) ``` #### 动态调整线程池大小 一旦上述配置完成并启动应用程序之后,就可以利用 Actuator 的 HTTP 接口来实时更改这些值而无需重启服务。例如发送 PUT 请求至 `/actuator/dynamic-thread-pool/tomcat/maxPoolSize?value=30` 可以在线更新最大线程数目为 30[^1]。 #### 监控指标暴露 该组件还支持与 Micrometer 和 Prometheus 整合以便更好地跟踪性能数据。只需确保已安装相应的监控工具,并按照官方文档指导启用相关特性即可获取详细的统计信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值