zoukankan      html  css  js  c++  java
  • Spring Boot学习笔记(六):Spring Boot 应用监控

    转自:https://www.jianshu.com/p/f540d6e6b264

    Spring Boot 提供了运行时的应用监控和管理功能,我们可以通过http、JMX进行操作。

    全部章节传送门:
    Spring Boot学习笔记(一):Spring Boot 入门基础
    Spring Boot学习笔记(二):Spring Boot 运行原理
    Spring Boot学习笔记(三):Spring Boot Web开发
    Spring Boot学习笔记(四):Spring Boot 数据访问
    Spring Boot学习笔记(五):Spring Boot 企业级开发
    Spring Boot学习笔记(六):Spring Boot 应用监控

    常见端点介绍

    Spring Boot 2.0提供的常用端点如下所示。

    序号端点名描述默认开启(Web)默认开启(JMX)
    1 actuator 所有端点的列表,需加入spring HATEOAS支持
    2 auditevents 显示应用暴露的审计事件 (比如认证进入、订单失败)
    3 info 显示应用的基本信息
    4 health 显示应用的健康状态
    5 metrics 显示应用多样的度量信息
    6 loggers 显示和修改配置的loggers
    7 logfile 返回log file中的内容(如果logging.file或者logging.path被设置) 不适用
    8 httptrace 显示HTTP足迹,最近100个HTTP request/repsponse
    9 env 显示当前的环境特性
    10 flyway 显示数据库迁移路径的详细信息
    11 shutdown 让你逐步关闭应用
    12 mappings 显示所有的@RequestMapping路径
    13 scheduledtasks 显示应用中的调度任务
    14 threaddump 执行一个线程dump 不适用
    15 heapdump 返回一个GZip压缩的JVM堆dump 不适用

    配置端点

    默认情况下,所有的端点都是打开的,除了 shutdown 端点。可以通过 management.endpoint.<id>.enabled的值设置为true或者false

    例如,想要打开 shutdown 端点,可以在application.properties中配置:

    management.endpoint.shutdown.enabled=true
    

    默认情况下,所有的端点都可以通过JMX查看,而只有health端点和info端点可以通过HTTP查看。我们可以通过配置进行修改。

    management.endpoints.web.exposure.include=health,info 
    management.endpoints.web.exposure.exclude=
    management.endpoints.jmx.exposure.include=*
    management.endpoints.jmx.exposure.exclude=
    

    查看端点

    创建 Spring Boot 项目,添加 Actuator、Web和HATEOAS依赖。

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.3.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.wyk</groupId>
        <artifactId>actuatordemo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>actuatordemo</name>
        <description>Demo project for Spring Boot Actustor</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-hateoas</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

    在application.properties中添加配置信息。

    # 开启所有端点允许HTTP查看
    management.endpoints.web.exposure.include=*
    # 显示详细健康信息
    management.endpoint.health.show-details=always
    

    然后运行程序,即可查看端点信息。

    登录 http://localhost:8080/actuator

     
    actuator端点查看.png

    登录 http://localhost:8080/actuator/health 查看端点信息。

     
    health端点查看.png

    除了shutdown端点需要POST方式查看,其他端点均可直接查看。

    还可以通过JMX对应用进行管理和监控。在控制台输入 jconsole即可进入JMX。

    然后选择我们的程序进入界面,然后在MBean标签的org.springframework.boot域下可以对程序进行监控和管理。

     
    jmx界面.png

    自定义端点

    自定义端点需要使用如下注释。

    • @Endpoint 是构建 rest 的唯一路径。不同请求的操作,调用时缺少必需参数,或者使用无法转换为所需类型的参数,则不会调用操作方法,响应状态将为400(错误请求)
    • @ReadOperation = GET 响应状态为 200 如果没有返回值响应 404(资源未找到)。
    • @WriteOperation = POST 响应状态为 200 如果没有返回值响应 204(无响应内容)
    • @DeleteOperation = DELETE 响应状态为 200 如果没有返回值响应 204(无响应内容)

    依旧使用上面的工程,添加一个实体类。

    package com.wyk.actuatordemo;
    
    import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
    import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
    import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;
    import org.springframework.stereotype.Component;
    
    @Endpoint(id="status")
    @Component
    public class StatusEndpoint {
        private String status;
    
        @ReadOperation
        public String getStatus() {
            return status;
        }
    
        @WriteOperation
        public void setStatus(String status) {
            this.status = status;
        }
    }
    

    然后修改运行类。

    package com.wyk.actuatordemo;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @SpringBootApplication
    @RestController
    public class ActuatordemoApplication {
    
        @Autowired
        StatusEndpoint statusEndpoint;
    
        public static void main(String[] args) {
            SpringApplication.run(ActuatordemoApplication.class, args);
        }
    
        @RequestMapping("/change")
        public String changeStatus(String status) {
            //StatusEndpoint statusEndpoint = new StatusEndpoint();
            statusEndpoint.setStatus(status);
            return "OK";
        }
    }
    

    运行程序,打开 http://localhost:8080/actuator/status, 这时候会报404,是因为status没有值。

     
    自定义端点状态1.png

    再打开一个窗口,输入 http://localhost:8080/change?status=123 , 这时候再打开 http://localhost:8080/actuator/status ,就可以看到了。

     
    自定义端点状态2.png

    自定义 HealthIndicator

    Health 信息是从 AppliciationContext 中所有的 HealthIndicator 的 Bean 中收集的, Spring 内置的HealthIndicator 如下所示。

    名称描述
    CassandraHealthIndicator 检查 Cassandra 数据库是否启动。
    DiskSpaceHealthIndicator 检查磁盘空间不足。
    DataSourceHealthIndicator 检查是否可以获得连接 DataSource。
    ElasticsearchHealthIndicator 检查 Elasticsearch 集群是否启动。
    InfluxDbHealthIndicator 检查 InfluxDB 服务器是否启动。
    JmsHealthIndicator 检查 JMS 代理是否启动。
    MailHealthIndicator 检查邮件服务器是否启动。
    MongoHealthIndicator 检查 Mongo 数据库是否启动。
    Neo4jHealthIndicator 检查 Neo4j 服务器是否启动。
    RabbitHealthIndicator 检查 Rabbit 服务器是否启动。
    RedisHealthIndicator 检查 Redis 服务器是否启动。
    SolrHealthIndicator 检查 Solr 服务器是否已启动。

    要实现自己的 HealthIndicator 只需要实现 HealthIndicator 类。

    package com.wyk.actuatordemo;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.actuate.health.Health;
    import org.springframework.boot.actuate.health.HealthIndicator;
    
    public class StatusHealth implements HealthIndicator {
        @Autowired
        StatusEndpoint statusEndpoint;
    
        @Override
        public Health health() {
            String status = statusEndpoint.getStatus();
    
            if(status == null || !status.equals("123")) {
                return Health.down().withDetail("Error", "Not Running").build();
            }
            return  Health.up().build();
    
    
        }
    }
    

    运行程序,打开 http://localhost:8080/actuator/health 查看。

     



  • 相关阅读:
    【入门】WebRTC知识点概览 | 内有技术干货免费下载
    mysql数据库运维方案
    前端报504错误如何定位
    Python数据分析之双色球高频数据统计
    利用Dapper ORM搭建三层架构
    ASP.NET和IIS工作原理
    C#中的new修饰符
    .NET 三层架构的简单规划
    Docker Hub中拉取镜像时出现超时问题该如何做?
    项目组件:分页(pagination)
  • 原文地址:https://www.cnblogs.com/sharpest/p/13709702.html
Copyright © 2011-2022 走看看