• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

09-指标监控

开发技术 开发技术 4小时前 2次浏览

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>

09-指标监控

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 个)。

09-指标监控

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.

09-指标监控

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

09-指标监控

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

09-指标监控

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

09-指标监控

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

09-指标监控

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

09-指标监控

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

09-指标监控

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

09-指标监控

09-指标监控

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

09-指标监控

(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

09-指标监控


程序员灯塔
转载请注明原文链接:09-指标监控
喜欢 (0)