[023-1].第1节:SpringBoot中的应用监控

我的后端学习大纲

SpringBoot学习大纲


1.SpringBoot Actuator

1.1.什么是指标监控:

  • 1.将来我们每一个微服务在云上部署以后,我们都需要对其进行监控、追踪、审计、控制等
  • 2.SpringBoot就抽取了Actuator场景,这样就可以对每个微服务快速引用即可获得生产级别的应用监控、审计等功能

1.2.对SpringBoot的1.x与2.x版本对比:

在这里插入图片描述


1.3.监控的意义:

在这里插入图片描述

1.4.监控的实施模型方式:

在这里插入图片描述


2.如何使用指标监控:

2.1.引入场景:

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

在这里插入图片描述

在引入依赖后,可以看到引入了spring-boot-starter-actuator的自动配置,同时引入了底层框架,micrometer.可以实现自定义一些指标监控功能


2.2.访问测试:

  • 所有的监控指标都在actuator下面;测试方式就是发送请求:http://localhost:8080/actuator/**
    在这里插入图片描述

2.3.更改监控端点暴露方式:

a.配置端点暴露方式为HTTP方式:

1.在.yml中暴露所有监控信息为HTTP,方便测试,同时这样可以对接前端,开发监控大面板

management:
  endpoints:
    enabled-by-default: true #暴露所有端点信息
    web:
      exposure:
        include: '*'  #以web方式全部暴露

b.为何要配置暴露端点的方式:

1.端点的暴露方式:

  • HTTP:默认只暴露health和info Endpoint
  • JMX:默认暴露所有Endpoint

2.端点监控说明:

  • 除过health和info,剩下的Endpoint都应该进行保护访问,默认的暴露方式是JMX方式。如果引入SpringSecurity,则会默认配置安全访问规则
    在这里插入图片描述
    在这里插入图片描述

3.JMX方式访问端点:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


2.4.测试:

  • 1.http://localhost:8080/actuator/beans:监控查看当前应用的所有组件
    在这里插入图片描述
  • 2.http://localhost:8080/actuator/conditions:监控查看当前应用哪些条件是自动开启,哪些没开启,可以拿到一个自动配置的报告
    在这里插入图片描述
  • 3.http://localhost:8080/actuator/configprops:监控查看当前应用是配置了哪些属性
    在这里插入图片描述
  • 4.http://localhost:8080/actuator/metrics:监控查看当前应用的指标:
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

3.Actuator Endpoints介绍:

3.1.各端点的含义:

在这里插入图片描述

  • 2.如果应用程序是Web应用程序(Spring MVC,Spring WebFlux或Jersey),则可以使用以下附加端点:
    在这里插入图片描述

3.2.最常用的Endpoint

  • Health:监控状况
  • Metrics:运行时指标
  • Loggers:日志记录
  • info:当前应用的信息

3.3.配置单个的Endpoint显示详细信息:

在这里插入图片描述


3.4.Health Endpoint:

  • 1.健康检查端点,我们一般用于在云平台,平台会定时的检查应用的健康状况,我们就需要Health Endpoint可以为平台返回当前应用的一系列组件健康状况的集合
  • 2.重要的几点:
    • health endpoint返回的结果,应该是一系列健康检查后的一个汇总报告
    • 很多的健康检查默认已经自动配置好了,比如:数据库、redis等
    • 可以很容易的添加自定义的健康检查机制
      在这里插入图片描述

3.5.Metrics Endpoint:

  • 1.提供详细的、层级的、空间指标信息,这些信息可以被pull(主动推送)或者push(被动获取)方式得到;

    • 通过Metrics对接多种监控系统
      ![外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传](https://img-home.csdnimg.cn/images/20230724024159.png?origin_url=https%3A%2F%2Fi-blog.csd7nimg.cn%2Fblog_migrate%2Fd5d0a9ab89bb0b78bb93106cc8b7eb66.png&pos_id=img-zZJUZsEq5d0a9ab89bb0b76bb93106cc8b7eb66.png8
    • 简化核心Metrics开发
    • 添加自定义Metrics或者扩展已有Metrics
  • 2.想要看某个指标的完整信息,需要再发个二次请求:
    在这里插入图片描述


3.6.管理Endpoints:

a.开启与禁用Endpoints

  • 1.默认所有的Endpoint除过shutdown都是开启的
  • 2.需要开启或者禁用某个Endpoint。配置模式为 management.endpoint.<endpointName>.enabled = true
management:
  endpoint:
    beans:
      enabled: true
  • 3.禁用所有的Endpoint然后手动开启指定的Endpoint:
management:
  endpoints:
    enabled-by-default: false  #关闭所有端点,
 
  # 开启某个端点  
  endpoint:
    beans:
      enabled: true
    health:
      enabled: true

4.定制 Endpoint

4.1.定制 Health 信息:

定义化的组件健康信息的监控实现可以实现HealthIndicator ,也可以继承AbstructHealthIndicator

a.implements 方式实现自定义健康检查:

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class MyHealthIndicator implements HealthIndicator {

    @Override
    public Health health() {
        int errorCode = check(); // perform some specific health check
        if (errorCode != 0) {
            return Health.down().withDetail("Error Code", errorCode).build();
        }else{
          return Health.up().build();
		}
    }

}

	//构建Health
	Health build = Health.down()
             .withDetail("msg", "error service")
             .withDetail("code", "500")
             .withException(new RuntimeException())
             .build();
  • 2.配置yml:
management:
    health:
      enabled: true
      show-details: always #总是显示详细信息。可显示每个模块的状态信息

b.使用extends 方式实现健康检查:

@Component
public class MyComHealthIndicator extends AbstractHealthIndicator {

    /**
     * 真实的检查方法
     * @param builder
     * @throws Exception
     */
    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {
        //mongodb。  获取连接进行测试
        Map<String,Object> map = new HashMap<>();
        // 检查完成
        if(1 == 2){
//            builder.up(); //健康
            builder.status(Status.UP);
            map.put("count",1);
            map.put("ms",100);
        }else {
//            builder.down();
            builder.status(Status.OUT_OF_SERVICE);
            map.put("err","连接超时");
            map.put("ms",3000);
        }
        builder.withDetail("code",100)
                .withDetails(map);

    }
}

在这里插入图片描述


4.2.定制info信息

a.方式1:编写配置文件

info:
  appName: boot-admin
  version: 2.0.1
  mavenProjectName: @project.artifactId@  #使用@@可以获取maven的pom文件值
  mavenProjectVersion: @project.version@

使用 @符号可获取配置文件中值

b.方式2:编写InfoContributor:

在这里插入图片描述
在这里插入图片描述

http://localhost:8080/actuator/info 会输出以上方式返回的所有info信息


4.3.定制Metrics信息

a.增加定制Metrics:

如增加统计如下接口的调用次数:

在这里插入图片描述

b.编码实现:

class MyService{
    Counter counter;
    public MyService(MeterRegistry meterRegistry){
         counter = meterRegistry.counter("cityservice.method.running.counter");
    }

    public void hello() {
        counter.increment();
    }
}
//也可以使用下面的方式
@Bean
MeterBinder queueSize(Queue queue) {
    return (registry) -> Gauge.builder("queueSize", queue::size).register(registry);
}

4.4.定制自己的Endpoint:

@Component
@Endpoint(id = "container") // 端点名称是:container
public class DockerEndpoint {


    @ReadOperation
    public Map getDockerInfo(){
        return Collections.singletonMap("info","docker started...");
    }

    @WriteOperation
    private void restartDocker(){
        System.out.println("docker restarted....");
    }

}

场景:开发ReadinessEndpoint来管理程序是否就绪,或者LivenessEndpoint来管理程序是否存活;
当然,这个也可以直接使用


5.可视化:

5.1.访问地址:

  • 1.https://github.com/codecentric/spring-boot-admin进行可视化查看

5.2.监控可视化项目编码实现:

a.新建项目:

在这里插入图片描述

b.引入依赖:

在这里插入图片描述

c.更改主启动类:

在这里插入图片描述

d.启动服务:

在这里插入图片描述


5.3.更改微服务项目:

1.改善pom文件:

在这里插入图片描述

b.改配置文件:

  • 1.填写admin-serve的地址:
    在这里插入图片描述

c.启动客户端应用,查看admin-serve的变化:

在这里插入图片描述

在这里插入图片描述


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值