java框架之SpringBoot(17)-监控管理

介绍

SpringBoot 提供了监控管理功能的场景启动器,它能够为咱们提供准生产环境下的应用监控和管理功能。咱们能够经过HTTP、JMX、SSH协议来进行操做,自动获得审计、健康及指标信息等。git

使用

简单测试

一、使用 maven 建立 SpringBoot 项目,选中以下场景启动器:redis

二、修改配置,关闭安全管理:spring

# 关闭安全管理
management.security.enabled=false 
application.properties

三、测试:安全

启动项目,访问 localhost:8080/beans,能够看到应用中 IoC 容器的实例信息:

test

更多

除了上面示例的 bean 端点信息,SpringBoot 监控管理还提供了如下端点供咱们查看使用:springboot

端点名 描述
autoconfig 全部自动配置信息
auditevents 审计事件
beans 全部 bean 信息
configprops 全部配置信息
dump 线程状态信息
env 当前环境信息
health 应用健康情况
info 当前应用信息
metrics 应用的各项指标
mappings 应用 @RequestMapping 映射路径
shutdown 关闭当前应用
trace 追踪信息(最新的 http 请求)

补充

info

能够配置当前的应用信息:app

info.appName=myApp
info.appVerson=1.0.0
application.properties

还能够配置 git 相关信息:maven

git.branch=master
git.commit.id=eraqedfaed
git.commit.time=2018-2-4 12:23:34
git.properties

configprops

该端点能够查看当前全部配置信息:ide

若是想要关闭或开启某个端点,只须要在该配置信息中找到对应端点属性配置便可,好比要关闭 info 端点,先找到 info 端点配置:spring-boot

修改配置文件添加以下配置:测试

endpoints.info.enabled=false

shutdown

该端点可让咱们远程关闭应用,不过它默认是关闭的,咱们须要启用它,添加以下配置:

endpoints.shutdown.enabled=true

以 POST 方式请求该端点应用就会被远程关闭:

定制端点信息

好比咱们要定制 info 端点名称,能够添加以下配置:

endpoints.info.id=appInfo

还能够定制它的访问路径:

endpoints.info.path=/path/info

若是只想开启指定端点,能够添加以下配置:

# 关闭全部端点
endpoints.enabled=false
# 仅开启后续配置端点
endpoints.info.enabled=true

还能够定制全部端点的访问根路径,如:

management.context-path=/myapp

定制监控管理端口:

# 为 -1 时表示禁用管理端点
management.port=8801

健康状态检查

默认健康状态指示器

监控管理默认给咱们提供了查看当期应用健康状态的功能,查看:

除了能查看默认的磁盘信息,还能够配置查看其它第三方组件的健康状态信息,好比 Redis、RabbitMQ 等,默认提供的健康状态指示器类在 org.springframework.boot.actuate 包下:

这些类在引入相应组件依赖后就会自动生效,好比引入 redis 依赖:

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

自定义健康状态指示器

编写一个监控状态指示器,注册到 IoC 容器:

package zze.springboot.actuatortest.health;

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class MyAppHealthIndicator implements HealthIndicator {
    @Override
    public Health health() {
        // 自定义响应信息

        // return Health.up().build(); // 返回健康状态
        return Health.down().withDetail("msg","服务异常").build();
    }
}
zze.springboot.actuatortest.health.MyAppHealthIndicator

查看:

相关文章
相关标签/搜索