zoukankan      html  css  js  c++  java
  • 09-指标监控

    https://docs.spring.io/spring-boot/docs/2.4.3/reference/html/production-ready-features.html#production-ready

    1. 简述

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

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

    You can choose to manage and monitor your application by using HTTP endpoints or with JMX (Java Management Extensions).

    运行起 SpringBoot 程序后,查看二者的默认情况:① 通过在 CMD 输入 jconsole 选择正在运行的 SpringBoot 的主程序类(好多 Endpoint);② 在 Firefox 输入 <SpringBoot访问路径>/actuator(就能看到 4 个)。

    2. Endpoints

    Actuator endpoints let you monitor and interact with your application. Spring Boot includes a number of built-in endpoints and lets you add your own. For example, the health endpoint provides basic application health information.

    Each individual endpoint can be enabled or disabled and exposed (made remotely accessible) over HTTP or JMX. An endpoint is considered to be available when it is both enabled and exposed. The built-in endpoints will only be auto-configured when they are available. Most applications choose exposure via HTTP, where the ID of the endpoint along with a prefix of /actuator is mapped to a URL. For example, by default, the health endpoint is mapped to /actuator/health.

    2.1 Enable

    所有 Endpoint(除了 shutdown)默认都是开启的:management.endpoints.enabled-by-default=true,只是有没有暴露而已(API-2.2. Exposing Endpoints 可以查看 JMX、Web 分别默认暴露的 Endpoint),如果有特殊需求,可以将其设置为 false 后手动开启指定的 Endpoint。

    management:
      endpoints:
        enabled-by-default: false
      endpoint:
        beans:
          enabled: true
        health:
          enabled: true
        shutdown:
          enabled: true
        ...
    

    2.2 Expose

    配置上“以 web 方式暴露所有端点”后,访问 http://localhost:8080/admin/actuator

    3. 定制 Endpoint

    3.1 定制 health 信息

    /**
     * @author 6x7
     * @Description Actuator-健康检查
     */
    @Component
    public class MyAppHealthIndicator extends AbstractHealthIndicator {
        /**
         * 检查逻辑
         * @param builder
         * @throws Exception
         */
        @Override
        protected void doHealthCheck(Health.Builder builder) throws Exception {
            Map<String, Object> config = new HashMap<>();
            builder.status(Status.UP);
            config.put("controller", 1);
            config.put("service", 1);
            config.put("mapper", 1);
            builder.withDetail("code", 1101).withDetails(config);
        }
    }
    

    访问 http://localhost:8080/admin/actuator/health

    3.2 定制 info 信息

    (1)在 application.yml 中配置

    info:
      appName: boot-admin
      appVersion: @project.version@     # 引用 pom.xml 的内容
    

    (2)注册一个实现 InfoContributor 接口的组件

    @Component
    public class AppInfoContributor implements InfoContributor {
        @Override
        public void contribute(Info.Builder builder) {
            builder
                .withDetail("msg0", "Can you hear me?")
                .withDetail("msg1", "You are being watched.");
        }
    }
    

    访问 http://localhost:8080/admin/actuator/info

    3.3 定制 metrics 信息

    @Service
    public class UserServiceImpl implements UserService {
        @Autowired
        UserMapper userMapper;
    
        Counter counter;
    
        public UserServiceImpl(MeterRegistry meterRegistry) {
            counter = meterRegistry.counter("userService.getUserById.count");
        }
    
        @Override
        public User getUserById(Long id) {
            counter.increment();
            return userMapper.getUserById(id);
        }
    }
    

    访问 http://localhost:8080/admin/actuator/metrics

    访问处理逻辑中会调用到 userService#getUserById 的 handler 方法后,再访问 http://localhost:8080/admin/actuator/metrics/userService.getUserById.count

    3.4 定制 Endpoint

    @Slf4j
    @Component
    @Endpoint(id="my-secret-service")
    public class MyEndpoint {
    
        /**
         * http://localhost:8080/admin/actuator/my-secret-service
         * @return
         */
        @ReadOperation
        public Map getServiceInfo() {
            return Collections.singletonMap("serviceInfo", "service started...");
        }
    
        /**
         * jconsole: boot -> EndPoint -> my-secret-service -> 操作 -> click [stopService]
         */
        @WriteOperation
        public void stopService() {
            log.info("service stopped...");
        }
    }
    

    访问 http://localhost:8080/admin/actuator

    4. 可视化

    https://github.com/codecentric/spring-boot-admin
    https://codecentric.github.io/spring-boot-admin/current/

    4.1 新建监控可视化微服务

    (1)添加如下依赖

    <dependencies>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    

    (2)主启动类上添加 @EnableAdminServer 注解

    (3)将服务注册到 8888 端口

    4.2 注册客户端微服务

    (1)我们写的微服务的监控数据是要被上面新建的 SpringBoot Admin Server 收集的,所以要在我们的微服务程序中添加客户端依赖:

    <dependency>
        <groupId>de.codecentric</groupId>
        <artifactId>spring-boot-admin-starter-client</artifactId>
        <version>2.3.1</version>
    </dependency>
    

    访问 localhost:8888

    (2)Enable the SBA Client by configuring the URL of the Spring Boot Admin Server :

    spring:
      application:
        name: boot_03_admin              # 注册时微服务的名字
      boot:
        admin:
          client:
            url: http://localhost:8888
            instance:
              prefer-ip: true            # 注册方式是 hostname 还是 ip
    
    management:
      endpoints:
        enabled-by-default: true
        web:
          exposure:
            include: '*'
    

    此时再查看 http://localhost:8888

  • 相关阅读:
    学习笔记CB002:词干提取、词性标注、中文切词、文档分类
    学习笔记CB001:NLTK库、语料库、词概率、双连词、词典
    从零开始在iPhone上运行视频流实时预测模型应用,只需10步
    学习笔记DL008:概率论,随机变量,概率分布,边缘概率,条件概率,期望、方差、协方差
    学习笔记DL007:Moore-Penrose伪逆,迹运算,行列式,主成分分析PCA
    学习笔记DL005:线性相关、生成子空间,范数,特殊类型矩阵、向量
    学习笔记DL004:标量、向量、矩阵、张量,矩阵、向量相乘,单位矩阵、逆矩阵
    学习笔记DL003:神经网络第二、三次浪潮,数据量、模型规模,精度、复杂度,对现实世界冲击
    学习笔记DL002:AI、机器学习、表示学习、深度学习,第一次大衰退
    学习笔记TF067:TensorFlow Serving、Flod、计算加速,机器学习评测体系,公开数据集
  • 原文地址:https://www.cnblogs.com/liujiaqi1101/p/15269858.html
Copyright © 2011-2022 走看看