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

查看:

相關文章
相關標籤/搜索