SpringBoot 监控(非普罗米修斯)

在这种情况下,微服务的监控显得尤为重要。springboot作为微服务框架,除了它强大的快速开
发功能外,还有就是它提供了actuator模块,引入该模块能够自动为springboot应用提供一系列
用于监控的端点

Acturator

Actuator是spring boot的一个附加功能,可帮助你在应用程序生产环境时监视和管理应用程序。
可以使用HTTP的各种请求来监管,审计,收集应用的运行情况。Spring Boot Actuator提供了对单个
Spring Boot的监控,信息包含:应用状态、内存、线程、堆栈等等,比较全面的监控了Spring
Boot应用的整个生命周期。特别对于微服务管理十分有意义。

Actuator 的 REST 接口

Actuator 监控分成两类:原生端点和用户自定义端点;自定义端点主要是指扩展性,用户可以根
据自己的实际应用,定义一些比较关心的指标,在运行期进行监控。
原生端点是在应用程序里提供众多 Web 接口,通过它们了解应用程序运行时的内部状况。原生端
点又可以分成三类:
应用配置类:可以查看应用在运行期的静态信息:例如自动配置信息、加载的 springbean
信息、yml 文件配置信息、环境信息、请求映射信息;
度量指标类:主要是运行期的动态信息,例如堆栈、请求链、一些健康指标、metrics 信息
等;
操作控制类:主要是指 shutdown,用户可以发送一个请求将应用的监控功能关闭。
Actuator 提供了 13 个接口,具体如下表所示。


image.png
依赖引入
 
org.springframework.boot 
spring-boot-starter-actuator 
 

为了保证 actuator 暴露的监控接口的安全性,需要添加安全控制的依赖 spring-boot-start- security 依赖,访问应用监控端点时,都需要输入验证信息。Security 依赖,可以选择不加,不
进行安全管理

info.app.name=spring-boot-actuator 
info.app.version= 1.0.0 
info.app.test=test 
management.endpoints.web.exposure.include=* 
#展示细节,除了always之外还有when-authorized、never,默认值是never 
management.endpoint.health.show-details=always 
#management.endpoints.web.base-path=/monitor 
management.endpoint.shutdown.enabled=true 

management.endpoints.web.base-path=/monitor 代表启用单独的url地址来监控 Spring
Boot 应用,为了安全一般都启用独立的端口来访问后端的监控信息
management.endpoint.shutdown.enabled=true 启用接口关闭 Spring Boot
配置完成之后,启动项目就可以继续验证各个监控功能了。

属性解析

在 Spring Boot 2.x 中为了安全期间,Actuator 只开放了两个端点 /actuator/health 和 /actuator/info 。可以在配置文件中设置打开。
可以打开所有的监控点
management.endpoints.web.exposure.include=*
也可以选择打开部分
management.endpoints.web.exposure.include=beans,trace
Actuator 默认所有的监控点路径都在 /actuator/* ,当然如果有需要这个路径也支持定制。
management.endpoints.web.base-path=/manage

设置完重启后,再次访问地址就会变成 /manage/*
Actuator 几乎监控了应用涉及的方方面面,我们重点讲述一些经常在项目中常用的属性。

health

health 主要用来检查应用的运行状态,这是我们使用最高频的一个监控点。通常使用此接口提醒
我们应用实例的运行状态,以及应用不”健康“的原因,比如数据库连接、磁盘空间不够等。
默认情况下 health 的状态是开放的,添加依赖后启动项目,访问:
http://localhost:8080/actuator/health 即可看到应用的状态。

{ 
"status" : "UP" 
}

health 通过合并几个健康指数检查应用的健康情况。Spring Boot Actuator 有几个预定义的健康
指标比如 DataSourceHealthIndicator , DiskSpaceHealthIndicator , MongoHealthIndicator , RedisHealthIndicator 等,它使用这些健康指标作为健康检查的一
部分。
举个例子,如果你的应用使用 Redis, RedisHealthindicator 将被当作检查的一部分;如果使
用 MongoDB,那么 MongoHealthIndicator 将被当作检查的一部分。
可以在配置文件中关闭特定的健康检查指标,比如关闭 redis 的健康检查:

management.health.redise.enabled=false

默认,所有的这些健康指标被当作健康检查的一部分。

info

info 就是我们自己配置在配置文件中以 info 开头的配置信息,比如我们在示例项目中的配置是:

info.app.name=spring-boot-actuator 
info.app.version= 1.0.0 
info.app.test= test

2. Spring Boot Admin

对于spring actuator而言,最大的缺点在于是以json形式来进行展示,为了更好的进行监控
显示,我们来介绍一个更加方便的工具:spring boot admin。
Spring Boot Admin:可视化后台管理系统

Spring Boot Admin 是一个针对spring-boot的actuator接口进行UI美化封装的监控工具。他可以
返回在列表中浏览所有被监控spring-boot项目的基本信息比如:Spring容器管理的所有的bean、
详细的Health信息、内存信息、JVM信息、垃圾回收信息、各种配置信息(比如数据源、缓存列表
和命中率)等,Threads 线程管理,Environment 管理等。
利用springbootadmin进行监控的架构图如下:


image.png

通俗点,就是我们如果有n个springboot业务系统需要监控的话,那么需要一个额外的
springbootadmin应用来进行监控这些client,client和server之间需要做一点配置即可。

搭建Server端


org.springframework.boot 
spring-boot-starter-web 
 
 
org.springframework.boot 
spring-boot-starter-actuator 
 
 
de.codecentric 
spring-boot-admin-starter-server 
 

applicaiton.yml

server: 
port: 8081

@EnableAdminServer

@EnableAdminServer 
@SpringBootApplication 
public class App { 
public static void main(String[] args) { 
SpringApplication.run(App.class, args); 
} 

启动服务端后


image.png

搭建client端


org.springframework.boot 
spring-boot-starter-web 
 
 
org.springframework.boot 
spring-boot-starter-actuator 
 
 
de.codecentric 
spring-boot-admin-starter-client 
2.1.0 
 

application.yml

server: 
  port: 8080 
#自定义配置信息用于"/actuator/info"读取 
info: 
   name: 老王 
age: 100 
   phone: 110 
#通过下面的配置启用所有的监控端点,默认情况下,这些端点是禁用的; 
management: 
   endpoints:
    web:
       exposure: 
           include: "*" 
    endpoint: 
       health: 
          show-details: always 
## 将Client作为服务注册到Server,通过Server来监听项目的运行情况 
spring: 
   boot: 
     admin: 
       client: 
          url: http://localhost:8081 
##application实例名 
application: 
name : spring-boot-admin-client 

启动 client……
可以看到 client 端已注册到 server。


image.png

image.png

你可能感兴趣的:(SpringBoot 监控(非普罗米修斯))