zoukankan      html  css  js  c++  java
  • 每天学点SpringCloud(十):SpringCloud监控

    今天我们来学习一下actuator这个组件,它不是SpringCloud之后才有的,而是SpringBoot的一个starter,Spring Boot Actuator。我们使用SpringCloud的时候需要使用这个组件对应用程序进行监控与管理

    在SpringBoot2.0版本中,actuator可以为我们提供以下端点:

    访问路径描述
    /actuator/auditevents 显示当前应用程序的审计事件信息
    /actuator/beans 显示一个应用中所有Spring Beans的完整列表
    /actuator/conditions 显示配置类和自动配置类的状态及它们被应用或未被应用的原因
    /actuator/configprops 显示一个所有@ConfigurationProperties的集合列表
    /actuator/env 显示来自Spring的 ConfigurableEnvironment的属性
    /actuator/features 显示系统启动的一些features
    /actuator/health 显示应用的健康信息
    /actuator/httptrace 最后100个HTTP请求
    /actuator/info 显示任意的应用信息
    /actuator/metrics 展示当前应用的metrics信息
    /actuator/mappings 显示一个所有@RequestMapping路径的集合列表
    /actuator/refresh 更新配置
    /actuator/scheduledtasks 显示应用程序中的定时任务
    /actuator/service-registry 当前应用在注册中心的状态
    /actuator/shutdown 允许应用以优雅的方式关闭
    /actuator/threaddump 执行一个线程dump
    /actuator/heapdump 返回一个GZip压缩的hprof堆dump文件
    /actuator/loggers 返回系统的一些日志

    虽然actuator默认给我们提供了这么多的端点供我们使用,但是为了安全起见,在SpringBoot2.0中它仅仅开放了health和info两个端口,如果想要使用其他的端口就需要我们增加一些配置了,一起来看一下如何使用actuator吧。

    1. 引入依赖

    1
    2
    3
    4
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    2. 修改配置文件

    1
    2
    3
    4
    5
    6
    management:
    endpoints:
    web:
    exposure:
    #exclude: shutdown,threaddump #此处控制的是不开放哪些端点
    include: "*" #此处控制的是开放哪些端点,如果需要开启少数端点可以这样配置:health,info。如果开启多个则使用*号开启除了exclude的端点

    这个时候我们使用postman等接口调用工具访问 ip:端口/actuator 这个路径时就会得到下图所示的这么一个json串,这个json串中就是对应的各个端点的地址信息。
    1

    3. 健康检查

    默认我们访问/actuator/health得到的只是一个状态值,其实它的详细信息里包含了很多有用的东西,比如说检查磁盘空间、DataSource的连接、Elasticsearch、Mongo、Rabbit、Redis等信息,我们可以通过如下配置来开启详细的健康检查:

    1
    2
    3
    4
    management:
    endpoint:
    health:
    show-details: always

    不仅如此,健康检查的指标我们还可以自定义,创建如下的一个bean提供健康检查的功能。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    @Component
    public class ConnectTimeHealthIndicator implements HealthIndicator {
    @Override
    public Health health() {
    long connectTime=(long)Math.random()*10;//模拟一个连接操作
    if(connectTime>3){
    //如果连接时间大于3则认为连接失败,返回状态为down
    return Health.down().withDetail("code", "504").withDetail("msg","xx应用连接超时").build();
    }
    return Health.up().build();
    }
    }

    此时我们访问 ip:端口/actuator/health 访问时可能就会根据连接时间呈现下方的两种状态
    2
    3

    GitHub地址:https://github.com/shiyujun/spring-cloud-demo。代码所在模块:cloud-demo-consumer-feign

    如果对您有所帮助,请记得帮忙点一个star哦

    本文出自http://zhixiang.org.cn,转载请保留。

  • 相关阅读:
    168. Excel Sheet Column Title
    171. Excel Sheet Column Number
    264. Ugly Number II java solutions
    152. Maximum Product Subarray java solutions
    309. Best Time to Buy and Sell Stock with Cooldown java solutions
    120. Triangle java solutions
    300. Longest Increasing Subsequence java solutions
    63. Unique Paths II java solutions
    221. Maximal Square java solutions
    279. Perfect Squares java solutions
  • 原文地址:https://www.cnblogs.com/zhixiang-org-cn/p/10048793.html
Copyright © 2011-2022 走看看