zoukankan      html  css  js  c++  java
  • 第十六章:Spring Boot 与监控管理

    一、监控管理

      通过引入spring-boot-starter-actuator,可以使用Spring Boot为我们提供的准生产环境下的应用监控和管理功能。我们可以通过HTTP, JMX, SSH协议来进行操作,自动得到审计、健康及指标信息等。

      步骤:

      1、引入 spring-boot-starter-actuator

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

      2、通过 http 方式访问监控端点

      创建项目,并引入 actuator,并启动项目

      项目 pom.xml 配置文件:

    <?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 https://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>1.5.12.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.njf</groupId>
        <artifactId>spring-boot-16-actuator</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>spring-boot-16-actuator</name>
        <description>Demo project for Spring Boot</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-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </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>

        启动项目:

       使用了 actuator 之后,会自动添加很多的映射,这些就是用于监控管理的请求信息。

    二、监控和管理端点

      监控和管理端点:

      

       默认这些访问都是受保护的,不可以直接访问,可以在 application.properties 中设置下面的属性,就可以访问了。

      

    management.security.enabled=false
    

      

      (1)health:监控应用健康状况

        

      (2)auditevents:审计事件

        

      (3)beans:所有 Bean的信息

        

      (4)info:当前应用信息

        

         可以在 application.properties 中配置 info 信息

    info.app.id=Hello
    info.app.verion=1.0.0
    

          还可以配置 git 的信息(可以参照GitProperties 类)

        创建 git.properties 并配置信息;

    git.branch=master
    git.commit.id=njf
    git.commit.time=2022-01-08 12:12:56

      (5)dump:应用中的线程状态信息

        

      (6)autoconfig:所有自动配置信息

        

        

      (7)heapdump:下载当前内存快照

        

      (8)trace:追踪信息(最新的http 请求)

        

      (9)mappings:应用 @RequestMapping 映射路径,由哪个Controller来处理

        

      (10)metrics:应用的各项指标

        

      (11)env:当前环境信息

        

      (12)loggers:日志相关信息

        

      (13)configprops:所有配置属性

        

      (14)shutdown:远程关闭当前应用(默认关闭)

        在application.properties 中开启此功能:(可进行 shutdown,POST 提交,此端点默认关闭)

    #远程关闭应用,需要发送post请求
    endpoints.shutdown.enabled=true
    

        可以使用 Postman 发送 POST  请求来测试:

    http://localhost:8080/shutdown
    

      

    三、定制端点信息

      定制端点一般通过endpoints+端点名+属性名来设置

    修改端点id(endpoints.beans.id=mybeans)
    开启远程应用关闭功能(endpoints.shutdown.enabled=true)
    关闭端点(endpoints.beans.enabled=false)
    

       示例:

    #定制端点信息,id 和 path 只能一个生效
    endpoints.beans.id=mybean
    endpoints.beans.path=/bean
    #禁用端点信息
    endpoints.beans.enabled=false

      定制端点访问根路径:

    #定制端点访问根路径
    management.context-path=/manage
    

        此时的端点访问路径就是 http://localhost:8080/manage/beans。

      这样可以结合 Spring Security 进行权限控制。

      定制端点的访问端口:

    #定制端点的访问端口,如果为 -1 表示关闭端点访问
    management.port=8181
    

      

      关闭所有端点,按需启动端点:

    #关闭所有端点,按需启动端点
    endpoints.enabled=false
    endpoints.beans.enabled=true
    

      

    四、 自定义 HealthIndicator

      1、health 端点

        

        默认只有当前应用中组件的健康状态,Spring Boot 也提供了一些默认提供好的组件健康状态。

        

      2、以 Redis 为例

      (1)加入 Redis 的场景启动器

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

      (2)配置 Redis 的信息

    #配置 redis 的主机地址(配置是错误的)
    spring.redis.host=localhost
    

      

      (3)启动服务

      

         可以看到 Redis 连接是异常的。

         SpringBoot 提供了 RedisHealthIndicator 类用来检查 Redis 的连接状态。

     

      3、自定义健康状态指示器

      (1)自定义健康状态指示器步骤:

    1、编写一个指示器,必须实现 HealthIndicator 接口
    2、指示器的名字  xxxHealthIndicator
    3、加入容器中
    

      

      (2)自定义健康状态指示器:

    @Component
    public class MyAppHealthIndicator implements HealthIndicator {
    
    
        @Override
        public Health health() {
            //自定义的检查方法
    
            //Health.up().build(); 代表应用健康
            Health build = Health.down().withDetail("msg", "服务异常").build();
    
            return build;
        }
    }

       测试:

      

      

  • 相关阅读:
    二、Cocos2dx概念介绍(游戏开发中不同的坐标系,cocos2dx锚点)
    (2)入门指南——(7)添加jquery代码(Adding our jQuery code)
    Citrix 服务器虚拟化之三十一 XenApp 6.5负载均衡
    CSS——inline-block属性
    VMware Workstation(虚拟机软件) V10.0 简体中文版可以安装了
    [.NET MVC4 入门系列01]Helloworld MVC 4 第一个MVC4程序
    ElasticSearch NEST笔记
    什么是REST API?
    ArrayList与List<T>笔记
    C# Socket SSL通讯笔记
  • 原文地址:https://www.cnblogs.com/niujifei/p/15779893.html
Copyright © 2011-2022 走看看