在上面一节,我们学习了SpringBoot整合第三方技术,这次我们主要学习监控技术,主要包含四个部分,分别为监控的意义,可视化监控平台,监控的原理,自定义监控指标等,下面一起来学习吧。
目录
一、监控
1.1、监控的意义
1.2、可视化监控平台
1.3、监控的原理
1.4、自定义监控指标
监控的意义如下:总结一句话就是监控就是观察当前的的设备具体的情况,观察到底发生了什么.
那么监控具体的实施方式是什么样的呢,一般来说,运行的服务需要主动上报自己是否需要收到监控,一旦需要被监控,则监控信息的服务器主动监控,获取并显示服务信息。
启动监控服务器,步骤如下:
首先在pom.xml文件中配置坐标依赖,具体如下:
4.0.0 org.springframework.boot spring-boot-starter-parent 2.5.4 com.wang admin_server 0.0.1-SNAPSHOT admin_server Demo project for Spring Boot 1.8 de.codecentric spring-boot-admin-starter-server 2.5.4 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
在.yml文件中配置服务器端口号,默认端口号。
server:port: 8080
在启动类中使用注解@EnableAdminServer开启监控服务,具体如下:
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@EnableAdminServer //开启监控服务
public class AdminServerApplication {public static void main(String[] args) {SpringApplication.run(AdminServerApplication.class, args);}}
在浏览器访问,如下所示,则说明监控服务启动成功。
下面搭建一个客户端的web应用,在客户端进行配置,让服务器端对其进行监控,如下:
首先,需要在pom.xml配置相应的坐标依赖,如下:
4.0.0 org.springframework.boot spring-boot-starter-parent 2.5.4 com.client admin_client 0.0.1-SNAPSHOT admin_client Demo project for Spring Boot 1.8 de.codecentric spring-boot-admin-starter-client 2.5.4 org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-autoconfigure org.springframework.boot spring-boot-autoconfigure org.springframework.boot spring-boot-maven-plugin
然后,设置应用被服务端监控,并配置要监控的项,如下:
server:port: 81#设置应用被监控
spring:boot:admin:client:url: http://localhost:8080
#配置需要被监控的项
management:endpoint:health:show-details: alwaysendpoints:web:exposure:include: "*"
启动客户端,在浏览器的服务端可以看到被监控的应用,如下:
1)Actuator
监控的方式是通过端点的配置与访问实现的,SpringBoot内置了多个端点,根据端点可以访问当前应用的相应信息。
端点的类型有很多种,常用的有health用于显示当前应用的健康信息,loggers用于显示应用程序中日志相关信息,metrics用于显示应用程序中指标度量信息。
对于端点是否开放,需要在.yml文件中对其进行配置,具体如下, 使用enable属性开启端点。
除了开启端点,也需要在配置文件手动指定对外暴露端点,具体如下:
2)info端点控制
对于静态的info信息的配置,只需要在.yml文件进行相应配置即可,如下:
#配置info信息
info:appName: @project.artifactId@author: wangversion: @project.version@company: alibaba
可以在服务端的信息栏看到相应的配置信息。
对于动态info信息的添加,需要在自定义的配置类中编程实现,具体如下:
import org.springframework.boot.actuate.info.Info;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.stereotype.Component;import java.util.HashMap;
import java.util.Map;@Component
public class InfoConfig implements InfoContributor {@Overridepublic void contribute(Info.Builder builder) {builder.withDetail("runTime",System.currentTimeMillis()) ;Map info = new HashMap() ;info.put("buildTime","2022") ;builder.withDetails(info) ;}
}
现在去浏览器的监控服务端可以观察到添加的动态info数据,如下:
3)health端点指标控制
为health自定义状态信息,在配置类继承AbstractHealthIndicator并重写相应的方法,在方法内区设置health的自定义信息。
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.Status;
import org.springframework.stereotype.Component;import java.util.HashMap;
import java.util.Map;@Component
public class HealthInfo extends AbstractHealthIndicator {@Overrideprotected void doHealthCheck(Health.Builder builder) throws Exception {builder.status(Status.UP) ;builder.withDetail("runTime",System.currentTimeMillis()) ;Map info = new HashMap() ;info.put("buildTime","2022") ;builder.withDetails(info) ;}
}
可以在服务端看到监控的health信息如下:
4)metrics端点指标控制
直接在业务层添加性能监控指标,如下:
需要自己手写一个配置类,在配置类中定义端点,并定义在读取端点时候调用的方法,在该方法中可以填写需要监控的相关指标信息。