springBoot actuator 使用

spring-boot- starter- actuator模块的实现对于实施微服务的中小团队来说,可以有效地省去或大大减少监控系统在采集应用指标时的开发量。当然,它也并不是万能的,有时候也需要对其做一些简单的扩展来帮助我们实现自身系统个性化的监控需求。

首先在我们的spring boot项目的meven 的POM文件里面添加

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

然后启动项目


我们会发现在项目启动日志里面出现一对Mapped,到这里说明我们的actuator配置成功

上图显示了一批端点定义,这些端点并非我们自己在程序中创建的,而是由spring-boot- starter- actuator模块根据应用依赖和配置自动创建出来的监控和管理端点。

然后我们打开postman 或者浏览器访问我们的项目地址然后根据上图的端点进行相关访问

例如:访问actuator/health(我这里的项目地址是http://127.0.0.1:8082)

ps 我这里的项目输出的是这个actuator/health,看百度上一些项目是/health 这是应为: 2.0之前默认是/ 2.0默认是 /actuator 可以通过这个属性值修改


这里就可以看到我们项目的health信息了

我们可以在配置文件中这样详细配置actuator

#actuator端口
management.server.port=9001
#修改访问路径  2.0之前默认是/   2.0默认是 /actuator  可以通过这个属性值修改
management.endpoints.web.base-path=/monitor
#开放所有页面节点  默认只开启了health、info两个节点
management.endpoints.web.exposure.include=*
#显示健康具体信息  默认不会显示详细信息
management.endpoint.health.show-details=always

 

当我们修改之后就可以通过我们配置后的地址http://127.0.0.1:9001/monitor/env/进行访问了


在这里就能看到我们项目的环境变量了!


详情可以去https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html官方文档进行查看

Spring Boot ActuatorSpring Boot提供的一个管理和监控应用程序的工具集。它提供了一些有用的端点(Endpoints),可以让你查看和管理Spring Boot应用程序的运行状态,如健康状态、内存使用情况、线程信息等等。 下面是Spring Boot Actuator的正确使用方法: 1. 引入依赖 在pom.xml中添加以下依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> ``` 2. 配置端点 在application.properties中配置端点: ``` management.endpoints.web.exposure.include=* ``` 上述配置将所有的端点都暴露出来,如果你只想暴露特定端点,可以使用以下配置: ``` management.endpoints.web.exposure.include=health,info ``` 3. 查看端点 启动应用程序后,可以通过以下URL查看端点: ``` http://localhost:8080/actuator ``` 这将显示所有可用的端点。例如,要查看健康状态,可以使用以下URL: ``` http://localhost:8080/actuator/health ``` 4. 自定义端点 可以通过实现Endpoint接口来自定义端点,例如: ``` @Component public class CustomEndpoint implements Endpoint<Map<String, Object>> { @Override public String getId() { return "custom"; } @Override public boolean isEnabled() { return true; } @Override public boolean isSensitive() { return true; } @Override public Map<String, Object> invoke() { Map<String, Object> result = new HashMap<>(); result.put("message", "Hello, world!"); return result; } } ``` 在应用程序中注入该组件,然后可以通过以下URL访问自定义端点: ``` http://localhost:8080/actuator/custom ``` 以上就是Spring Boot Actuator的正确使用方法,它可以帮助你更好地管理和监控Spring Boot应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值