Spring Boot Actuator监控及图形化监控

Actuator监控介绍

Actuator是Spring Boot自带的监控功能,可以帮助实现对程序内部运行情况的监控,如监控状况、Bean加载情况,配置属性,日志信息等。

Actuator监控

快速使用

  1. 首先使用idea快速搭建一个web项目
  2. pom.xml文件中应该至少有这两个依赖
	<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>
  1. 启动项目
  2. 因为在不指定端口x号的话,默认是8080端口,所以直接访问http://localhost:8080/actuator就可以了
  3. 运行结果是一堆json字符串,经过浏览器插件整理后这样展示
{
    "_links": {
        "self": {
            "href": "http://localhost:8080/actuator",
            "templated": false
        },
        "health-component": {
            "href": "http://localhost:8080/actuator/health/{component}",
            "templated": true
        },
        "health-component-instance": {
            "href": "http://localhost:8080/actuator/health/{component}/{instance}",
            "templated": true
        },
        "health": {
            "href": "http://localhost:8080/actuator/health",
            "templated": false
        },
        "info": {
            "href": "http://localhost:8080/actuator/info",
            "templated": false
        }
    }
}

这个json字符串显示的每一个对象都是一个地址,我们可以通过访问去看所对应的详细信息

比如http://localhost:8080/actuator/health可以看当前程序的状态

up是程序正常运行
down是程序出现异常

{
    "status": "UP"
}

虽然展示的数据比较简单,甚至可以说得不到什么具体信息,但是我们可以通过配置达到详细信息的展示

打开Spring Boot的配置文件在里面写入这两句话

#显示详细信息
management.endpoint.health.show-details=always
#加载所有信息
management.endpoints.web.exposure.include=*

重启项目后的运行结果

{
    "_links": {
        "self": {
            "href": "http://localhost:8080/actuator",
            "templated": false
        },
        "auditevents": {
            "href": "http://localhost:8080/actuator/auditevents",
            "templated": false
        },
        "beans": {
            "href": "http://localhost:8080/actuator/beans",
            "templated": false
        },
        "caches-cache": {
            "href": "http://localhost:8080/actuator/caches/{cache}",
            "templated": true
        },
        "caches": {
            "href": "http://localhost:8080/actuator/caches",
            "templated": false
        },
        "health": {
            "href": "http://localhost:8080/actuator/health",
            "templated": false
        },
        "health-component": {
            "href": "http://localhost:8080/actuator/health/{component}",
            "templated": true
        },
        "health-component-instance": {
            "href": "http://localhost:8080/actuator/health/{component}/{instance}",
            "templated": true
        },
        "conditions": {
            "href": "http://localhost:8080/actuator/conditions",
            "templated": false
        },
        "configprops": {
            "href": "http://localhost:8080/actuator/configprops",
            "templated": false
        },
        "env": {
            "href": "http://localhost:8080/actuator/env",
            "templated": false
        },
        "env-toMatch": {
            "href": "http://localhost:8080/actuator/env/{toMatch}",
            "templated": true
        },
        "info": {
            "href": "http://localhost:8080/actuator/info",
            "templated": false
        },
        "loggers": {
            "href": "http://localhost:8080/actuator/loggers",
            "templated": false
        },
        "loggers-name": {
            "href": "http://localhost:8080/actuator/loggers/{name}",
            "templated": true
        },
        "heapdump": {
            "href": "http://localhost:8080/actuator/heapdump",
            "templated": false
        },
        "threaddump": {
            "href": "http://localhost:8080/actuator/threaddump",
            "templated": false
        },
        "metrics": {
            "href": "http://localhost:8080/actuator/metrics",

此时项目的详细信息都会展现出来,再看项目健康程度就可以看见更详细的信息

{
    "status": "UP",
    "details": {
        "diskSpace": {
            "status": "UP",
            "details": {
                "total": 154149056512,
                "free": 97539268608,
                "threshold": 10485760
            }
        }
    }
}

常用内置端点解释

端点解释
/health显示应用程序健康信息
/conditions获取自动化配置信息
/beans显示程序目前所有bean和相互依赖的关系
/env显示系统环境配置信息
/mappings查看控制器映射关系
/heapdump下载堆dump文件,可以进行内存解析
/metrics显示运行时的信息,包括内存,堆栈,线程等
/httptrace获取追踪web请求的详细信息
/threaddump查看线程应用信息
/loggers查看日志详信息

可以通过访问以上端点查看所对应的详细信息

是不是觉得json字符串比较难整理,比较难看。所以我们可以引入一款可视化的图形化监控

Actuator图形化监控

创建服务端

  1. 使用idea快速搭建一个项目
  2. 保证至少存在这几个依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>
  1. 在启动类上加上@EnableAdminServer注解
package com.dyh;

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableAdminServer
@SpringBootApplication
public class SpringbootServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootServerApplication.class, args);
    }

}


创建客户端

  1. 使用idea快速创建SpringBoot项目
  2. 保证至少存在这几个依赖
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
  1. 在配置文件中进行相关配置
server.port=8081
spring.boot.admin.client.url=http://localhost:8080

management.endpoint.health.show-details=always
management.endpoints.web.exposure.include=*
  1. 启动服务端项目,再启动客户端项目
  2. 访问http://localhost:8080

访问成功后进入以下界面
在这里插入图片描述
可以看见客户端已经被检测出来,并进行监控

点击客户端进入程序详情
可以观察项目详情、日志、jvm等各种动态详情。
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值