赞
踩
日常springboot应用中,我们可以使用Actuator来做Springboot项目的监控
他的功能就是帮我们实现springboot项目监控的
比如说有些问题在线上环境出现的 比如说系统响应速度变慢 同时经常无法访问
或者程序的cpu的资源 以及内存 或者io资源,这些都需要有一个良好的认知 这个时候监控就很有必要,这个时候我们就需要Actuator来帮助我们实现微服务的状态监控
我先创建一个springboot应用 1.pom <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> 2, 启动类 @SpringBootApplication public class SpringbootApplication { public static void main(String[] args) { SpringApplication.run(SpringbootApplication.class, args); } 3. controller--------->只是一个Test的测试类 @RestController public class TestController { @GetMapping("/") public String getTest() { return "111"; } } 4.server.port=8080 端口号8080
4. 当我们引入 这个pom之后
<!-- actuator start-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- actuator end-->
他其实帮我们暴露了2个端点
# 当我们访问 http://localhost:8080/actuator { "_links": { "self": { "href": "http://localhost:8080/actuator", "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 }, "health": { -->健康信息(微服务的健康状态) "href": "http://localhost:8080/actuator/health", "templated": false }, "info": { ------>微服务的信息 "href": "http://localhost:8080/actuator/info", "templated": false } } }
info信息我们要做后续处理 因为这些信息不是随便给的
虽然可以提供一系列的监控信息 但是这个数量比较少 因为默认情况下为了安全并没有开启次类的全部数据
我们可以 在yaml中开启全部
# 开启全部
# 开启全部
management.endpoints.web.exposure.include= *
management.endpoints.enabled-by-default=true
# 开启所有的端点 { "_links": { "self": { "href": "http://localhost:8080/actuator", "templated": false }, "auditevents": { "href": "http://localhost:8080/actuator/auditevents", "templated": false }, "beans": { ------->springboot中的bean信息 "href": "http://localhost:8080/actuator/beans", "templated": false }, "caches-cache": { --》缓存信息 "href": "http://localhost:8080/actuator/caches/{cache}", "templated": true }, "caches": { "href": "http://localhost:8080/actuator/caches", "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 }, "conditions": { "href": "http://localhost:8080/actuator/conditions", "templated": false }, "shutdown": { "href": "http://localhost:8080/actuator/shutdown", "templated": false }, "configprops": { "href": "http://localhost:8080/actuator/configprops", "templated": false }, "env": {----------环境信息 "href": "http://localhost:8080/actuator/env", "templated": false }, "env-toMatch": { "href": "http://localhost:8080/actuator/env/{toMatch}", "templated": true }, "info": { "href": "http://localhost:8080/actuator/info", "templated": false }, "loggers": { 日志信息 "href": "http://localhost:8080/actuator/loggers", "templated": false }, "loggers-name": { "href": "http://localhost:8080/actuator/loggers/{name}", "templated": true }, "heapdump": { 堆内存信息 "href": "http://localhost:8080/actuator/heapdump", "templated": false }, "threaddump": { 线程信息 "href": "http://localhost:8080/actuator/threaddump", "templated": false }, "metrics": { 统计信息 "href": "http://localhost:8080/actuator/metrics", "templated": false }, "metrics-requiredMetricName": { "href": "http://localhost:8080/actuator/metrics/{requiredMetricName}", "templated": true }, "scheduledtasks": { "href": "http://localhost:8080/actuator/scheduledtasks", "templated": false }, "httptrace": { "href": "http://localhost:8080/actuator/httptrace", "templated": false }, "mappings": { "href": "http://localhost:8080/actuator/mappings", "templated": false } } }
整个springboot项目中 所有与环境有关的状态都是可以进行处理的 这些就是我们的Actuator带来的优势所在,包括可以得到一些堆内存信息,以及线程内存信息,仅仅是某一个时刻监控.随着访问量 包括你程序处理的加深 获取的状态是不一样的,所以要想真正获取良好的监控,做些完整的数据记录--------》服务监控.先清楚的认知整个Actuator的 操作特点.以后我们会把这些信息收集到 然后再以图示的方式
当访问我们的http://localhost:8080/actuator/beans 的时候 我们
可以看到spring容器中目前存在的bean信息,包括他的依赖, 包括他的scope 是否是单例
http://localhost:8080/actuator/heapdump 我们也可以看到他此时的堆内存信息 他回生成一个heapdump的文件
https://www.cnblogs.com/icez/p/Actuator_heapdump_exploit.html
看这个文章可以用visualvm 打开这个文件进行查看 具体的堆内存信息
可以基于这个heapdump的文件,我们就可以发现问题 比如说发现某一段时间 资源飙升什么对象产生过多
http://localhost:8080/actuator/info 这个接口主要记录了微服务的信息,但是需要自己定义
比如说我配置了
# 记录微服务的信息
info.app.name=helloSpringboot
info.app.version=2.4
也就是说 http://localhost:8080/actuator/info
这个接口的数据可以由开发者自行配置
但是有一个问题就是说这个 info信息 只能通过yaml 来配置,最终很难进行修改
因为一旦修改了之后要重新打包进行部署,所以这些配置都是固定的,最好的解决方案是 将info 数据通过数据库来进行读取
在application中加入一些组织信息,可以在类中加入一些自己独特信息, 为每一个微服务配置一个详细信息
mappings
查看所有 URL 映射,即所有 @RequestMapping 路径的整理列表。
http://localhost:8080/actuator/health 我们访问health的时候
出现的是up 代表此时的微服务处于健康状态
我们可以加入 这个配置来看更详细的信息
management.endpoint.health.show-details=always
我们的springboot项目随着引入的组件过多,如果此时redis挂了 的话
我们的微服务就是不健康了 ,所以我们需要扩展这个health
微服务整合 引入redis mysql mq, xx服务做整合
这些服务如果都是正常状态 这些都是正常状态
如果在整个微服务中 有一个出现问题 这个应该是个故障状态
@Component public class CustomerServiceHealthIndicator implements HealthIndicator { // 我们可以注入 各种template 如果发现template为空 即为 down @Override public Health health() { try { URL url = new URL("http://localhost:8080/health/"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); int statusCode = conn.getResponseCode(); if (statusCode >= 200 && statusCode < 300) { return Health.up().build(); } else { return Health.down().withDetail("HTTP Status Code", statusCode).build(); } } catch (Exception e) { return Health.down(e).build(); } } }
我们也可以通过Actuator来关闭springboot容器,
通过发送 post请求 http:localhost//actuator/shutdown 发送一个 shutdown 的请求
自定义endPoint端点 https://www.jb51.net/article/224284.htm
这样就可以关闭我们的微服务了
我们也可以单独给actuator 设置一个端口 这样可以和我们的服务端口进行区分开
我们都知道java中的日志体系优先级从高到低分别是 ERROR、WARN、INFO、DEBUG TRACE 高到低,在我们Springboot中的日志级别默认是Info
如果在springboot项目中日志级别设置低的话 可能会出现日志体积过大,如果设置高的话 可能会损失一些重要的日志消息,比如说我现在这个项目中的日志级别是INFO,此时访问localhost:8080的时候 打印日志
ERROR,INFO,WARN级别的日志 我们也可以为当前这个类设置专属的日志级别
这样可以更方便的排查出更隐蔽的问题,比如说我现在给我这个包设置为warn
日志级别
# 对某个包指定单独的日志级别
logging.level.com.lvhao.controller=warn
当访问http://localhost:8080/ 的时候 只会有 warn和error的日志级别输出了
我们吧我们的management 端口改成10098
http://localhost:10098/actuator/loggers 我们可以通过这个actuator 来查看我们的每一个类的日志情况
在这里插入代码片
我们也可以动态更改某个类的日志信息 看这里
https://blog.csdn.net/u013202238/article/details/120037252
https://www.jb51.net/article/245347.html 我们也可以通过springboot+ logback.xml文件以及拦截器 实施MDC全链路日志
因为只有做到mdc全链路日志之后 后续就会有 数据采集 数据分析
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。