spring boot 组件之Actuator(监控)

spring boot 组件之Actuator(监控)

创建springboot的项目,添加pom依赖:

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

启动应用,访问:http://localhost:8080/actuator/health
在这里插入图片描述
通过这个可以查看应用的一些状态,这就是Actuator;
也可以查看系统信息:http://localhost:8080/actuator/info
springboot官方文档说明地址:
https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html#production-ready-endpoints
在这里插入图片描述
默认情况下只开放health和 info 这两个端点。
在这里插入图片描述将值设置为*,则会放开所有端点权限。(不安全的)

management.endpoints.web.exposure.include=*

health(健康监测)

management.endpoint.health.show-details=always
#表示可以看到除了springboot应用之外的包括redis等等的一些数据库和组件的健康状态

Sping boot Actuator配置json格式化

spring.jackson.serialization.indent_output=true

在这里插入图片描述
这样就可以很直观的看到项目启动时候,Redis的信息和健康状态。

在这里插入图片描述
通过官方文档我们可以搜索到这个Redis健康状态监测的类:RedisHealthIndicator然后去项目中搜索就可以找到,这个类
在这里插入图片描述
假如你想监测你自己的一个组件的健康状态,只需要继承AbstractHealthIndicator这个类,然后去实现你的监测的机制就行了。

Metrics(健康监测)

当前应用的各项数据指标:(jvm,cpu,tomcat.session等等)

http://localhost:8080/actuator/metrics

这个地址展示的是这些指标的名称集合:
在这里插入图片描述
主要有这几点信息:

  • JVM(垃圾收集器)
  • 系统(运行时间、平均负载、处理器信息)
  • 线程池信息
  • tomcat会话信息
Loggers(日志信息)

主要查看每个类的日志级别:

http://localhost:8080/actuator/loggers

在这里插入图片描述
这里显示ROOT的日志级别:用post请求将configuredLevel日志级别修改为"DEBUG"级别
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

info(项目信息标识展示)

在application.properties 配置中配置info.app.name = @project.name@就可以查看到你的项目名称了

info.app.name = @project.name@
在这里插入图片描述

举例Customer(自实现Actuator监控端点)

实现实例代码如下:

package com.myspringboot.myspringboot.service;

import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Endpoint(id = "customer")
@Component
public class CustomerMetricsIndicator {
    @ReadOperation
    public Map<String,Object> time(){
        Map<String,Object> result = new HashMap<>();
        result.put("当前时间",new Date());
        return result;
    }
}

然后启动项目,调用接口:http://localhost:8080/actuator/customer
在这里插入图片描述

Actuator的两种监控方式

  1. http (web)
  2. jmx (java management extensions)
    JMX(Java Management Extensions,即Java管理扩展)是一个为应用程序、设备、系统等植入管理功能的框架。JMX可以跨越一系列异构操作系统平台、系统体系结构和网络传输协议,灵活的开发无缝集成的系统、网络和服务管理应用。
    Actuator 中JMX应用:
    先在application.properties 配置中,配置如下信息:

#JMX 包含的所有端点:
management.endpoints.jmx.exposure.include=*
#spring里面包含的节点信息
spring.jmx.enabled=true

配置完之后重启应用:Win+R 运行 jconsole 命令 ,打开jdk自带的监控平台,链接你的项目进程:打开之后可以看到MBean 然后找到项目名称,点开可以看到一系列系统信息:

在这里插入图片描述
手写一个JMX的信息上报:

public interface SystemInfoMBean {
    int getCpuCore();
    long getTotalMemory();
    void shutdown();
}
public class SystemInfo implements SystemInfoMBean{
    @Override
    public int getCpuCore() {
        return Runtime.getRuntime().availableProcessors();
    }

    @Override
    public long getTotalMemory() {
        return Runtime.getRuntime().totalMemory();
    }

    @Override
    public void shutdown() {
        System.exit(0);
    }
}
public class JMXMain {
    public static void main(String[] args) throws MalformedObjectNameException, NotCompliantMBeanException, InstanceAlreadyExistsException, MBeanRegistrationException, IOException {
        MBeanServer mBeanServer= ManagementFactory.getPlatformMBeanServer();
        ObjectName objectName=new ObjectName("com.dxh.example.myspringboot:type=SystemInfo");
        SystemInfo systemInfo=new SystemInfo();
        mBeanServer.registerMBean(systemInfo,objectName);
        System.in.read();
    }
}

在这里插入图片描述

springboot信息发布到Prometheus+Grafana

  • Prometheus
    开源监控系统。
    (1)数据采集
    (2)time-series存储metrics
    (3)可视化
  • Grafana

具体安装和配置,请参考:https://www.jianshu.com/p/762fa72ffe79

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值