• SpringBoot实用开发篇复习4(实用开发篇完)


    在上面一节,我们学习了SpringBoot整合第三方技术,这次我们主要学习监控技术,主要包含四个部分,分别为监控的意义,可视化监控平台,监控的原理,自定义监控指标等,下面一起来学习吧。

    目录

    一、监控

    1.1、监控的意义

    1.2、可视化监控平台

    1.3、监控的原理

    1.4、自定义监控指标


    一、监控

    1.1、监控的意义

    监控的意义如下:总结一句话就是监控就是观察当前的的设备具体的情况,观察到底发生了什么.

    那么监控具体的实施方式是什么样的呢,一般来说,运行的服务需要主动上报自己是否需要收到监控,一旦需要被监控,则监控信息的服务器主动监控,获取并显示服务信息。

    1.2、可视化监控平台

    启动监控服务器,步骤如下:
    首先在pom.xml文件中配置坐标依赖,具体如下:

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <parent>
    6. <groupId>org.springframework.bootgroupId>
    7. <artifactId>spring-boot-starter-parentartifactId>
    8. <version>2.5.4version>
    9. <relativePath/>
    10. parent>
    11. <groupId>com.wanggroupId>
    12. <artifactId>admin_serverartifactId>
    13. <version>0.0.1-SNAPSHOTversion>
    14. <name>admin_servername>
    15. <description>Demo project for Spring Bootdescription>
    16. <properties>
    17. <java.version>1.8java.version>
    18. properties>
    19. <dependencies>
    20. <dependency>
    21. <groupId>de.codecentricgroupId>
    22. <artifactId>spring-boot-admin-starter-serverartifactId>
    23. <version>2.5.4version>
    24. dependency>
    25. <dependency>
    26. <groupId>org.springframework.bootgroupId>
    27. <artifactId>spring-boot-starter-webartifactId>
    28. dependency>
    29. <dependency>
    30. <groupId>org.springframework.bootgroupId>
    31. <artifactId>spring-boot-starter-testartifactId>
    32. <scope>testscope>
    33. dependency>
    34. dependencies>
    35. <dependencyManagement>
    36. <dependencies>
    37. dependencies>
    38. dependencyManagement>
    39. <build>
    40. <plugins>
    41. <plugin>
    42. <groupId>org.springframework.bootgroupId>
    43. <artifactId>spring-boot-maven-pluginartifactId>
    44. plugin>
    45. plugins>
    46. build>
    47. project>

    在.yml文件中配置服务器端口号,默认端口号。

    1. server:
    2. port: 8080

    在启动类中使用注解@EnableAdminServer开启监控服务,具体如下:

    1. import de.codecentric.boot.admin.server.config.EnableAdminServer;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. @SpringBootApplication
    5. @EnableAdminServer //开启监控服务
    6. public class AdminServerApplication {
    7. public static void main(String[] args) {
    8. SpringApplication.run(AdminServerApplication.class, args);
    9. }
    10. }

    在浏览器访问,如下所示,则说明监控服务启动成功。

    下面搭建一个客户端的web应用,在客户端进行配置,让服务器端对其进行监控,如下:

    首先,需要在pom.xml配置相应的坐标依赖,如下:
     

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <parent>
    6. <groupId>org.springframework.bootgroupId>
    7. <artifactId>spring-boot-starter-parentartifactId>
    8. <version>2.5.4version>
    9. <relativePath/>
    10. parent>
    11. <groupId>com.clientgroupId>
    12. <artifactId>admin_clientartifactId>
    13. <version>0.0.1-SNAPSHOTversion>
    14. <name>admin_clientname>
    15. <description>Demo project for Spring Bootdescription>
    16. <properties>
    17. <java.version>1.8java.version>
    18. properties>
    19. <dependencies>
    20. <dependency>
    21. <groupId>de.codecentricgroupId>
    22. <artifactId>spring-boot-admin-starter-clientartifactId>
    23. <version>2.5.4version>
    24. dependency>
    25. <dependency>
    26. <groupId>org.springframework.bootgroupId>
    27. <artifactId>spring-boot-starter-testartifactId>
    28. <scope>testscope>
    29. dependency>
    30. <dependency>
    31. <groupId>org.springframework.bootgroupId>
    32. <artifactId>spring-boot-starter-webartifactId>
    33. dependency>
    34. <dependency>
    35. <groupId>org.springframework.bootgroupId>
    36. <artifactId>spring-boot-autoconfigureartifactId>
    37. dependency>
    38. <dependency>
    39. <groupId>org.springframework.bootgroupId>
    40. <artifactId>spring-boot-autoconfigureartifactId>
    41. dependency>
    42. dependencies>
    43. <dependencyManagement>
    44. <dependencies>
    45. dependencies>
    46. dependencyManagement>
    47. <build>
    48. <plugins>
    49. <plugin>
    50. <groupId>org.springframework.bootgroupId>
    51. <artifactId>spring-boot-maven-pluginartifactId>
    52. plugin>
    53. plugins>
    54. build>
    55. project>

    然后,设置应用被服务端监控,并配置要监控的项,如下:

    1. server:
    2. port: 81
    3. #设置应用被监控
    4. spring:
    5. boot:
    6. admin:
    7. client:
    8. url: http://localhost:8080
    9. #配置需要被监控的项
    10. management:
    11. endpoint:
    12. health:
    13. show-details: always
    14. endpoints:
    15. web:
    16. exposure:
    17. include: "*"

    启动客户端,在浏览器的服务端可以看到被监控的应用,如下:

     

    1.3、监控的原理

    1)Actuator

    监控的方式是通过端点的配置与访问实现的,SpringBoot内置了多个端点,根据端点可以访问当前应用的相应信息。

    端点的类型有很多种,常用的有health用于显示当前应用的健康信息,loggers用于显示应用程序中日志相关信息,metrics用于显示应用程序中指标度量信息。

     对于端点是否开放,需要在.yml文件中对其进行配置,具体如下, 使用enable属性开启端点。

    除了开启端点,也需要在配置文件手动指定对外暴露端点,具体如下:

    2)info端点控制

    对于静态的info信息的配置,只需要在.yml文件进行相应配置即可,如下:

    1. #配置info信息
    2. info:
    3. appName: @project.artifactId@
    4. author: wang
    5. version: @project.version@
    6. company: alibaba

    可以在服务端的信息栏看到相应的配置信息。

     

    对于动态info信息的添加,需要在自定义的配置类中编程实现,具体如下:

    1. import org.springframework.boot.actuate.info.Info;
    2. import org.springframework.boot.actuate.info.InfoContributor;
    3. import org.springframework.stereotype.Component;
    4. import java.util.HashMap;
    5. import java.util.Map;
    6. @Component
    7. public class InfoConfig implements InfoContributor {
    8. @Override
    9. public void contribute(Info.Builder builder) {
    10. builder.withDetail("runTime",System.currentTimeMillis()) ;
    11. Map info = new HashMap() ;
    12. info.put("buildTime","2022") ;
    13. builder.withDetails(info) ;
    14. }
    15. }

    现在去浏览器的监控服务端可以观察到添加的动态info数据,如下:

    3)health端点指标控制

    为health自定义状态信息,在配置类继承AbstractHealthIndicator并重写相应的方法,在方法内区设置health的自定义信息。

    1. import org.springframework.boot.actuate.health.AbstractHealthIndicator;
    2. import org.springframework.boot.actuate.health.Health;
    3. import org.springframework.boot.actuate.health.Status;
    4. import org.springframework.stereotype.Component;
    5. import java.util.HashMap;
    6. import java.util.Map;
    7. @Component
    8. public class HealthInfo extends AbstractHealthIndicator {
    9. @Override
    10. protected void doHealthCheck(Health.Builder builder) throws Exception {
    11. builder.status(Status.UP) ;
    12. builder.withDetail("runTime",System.currentTimeMillis()) ;
    13. Map info = new HashMap() ;
    14. info.put("buildTime","2022") ;
    15. builder.withDetails(info) ;
    16. }
    17. }

    可以在服务端看到监控的health信息如下:

    4)metrics端点指标控制

    直接在业务层添加性能监控指标,如下:

     

    1.4、自定义监控指标

    需要自己手写一个配置类,在配置类中定义端点,并定义在读取端点时候调用的方法,在该方法中可以填写需要监控的相关指标信息。

  • 相关阅读:
    Mysql8关键字
    Mac 公证失败问题排查
    力扣每日一题50:Pow(x,n)
    react 初级基础
    SpringBoot+SpringMVC+MybatisPlus
    数据仓库理论篇
    230. Kth Smallest Element in a BST
    uniapp项目实践总结(十三)封装文件操作方法
    Golang 的锁机制
    15. 三数之和
  • 原文地址:https://blog.csdn.net/nuist_NJUPT/article/details/127929614