栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot整合Actuator监控

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot整合Actuator监控

springboot整合Actuator监控。

1.简要说明:

Actuator提供了对springboot应用程序监视和管理的能力,可以选择通过使用HTTP Endpoint或者使用JMX来管理和监控springboot应用程序。

Actuator 允许通过Endpoints对springboot进行监控和交互。springboot内置的Endpoint包括(两种Endpoint: WEB和JMX, web方式考虑到安全性默认只开启了/health):

ID

JMX

Web

Endpoint功能描述

auditevents

Yes

No

暴露当前应用的audit events (依赖AuditEventRepository)

beans

Yes

No

Spring中所有Beans

caches

Yes

No

暴露可用的缓存

conditions

Yes

No

展示configuration 和auto-configuration类中解析的condition,并展示是否匹配的信息.

configprops

Yes

No

展示所有的@ConfigurationProperties

env

Yes

No

展示环境变量,来源于ConfigurableEnvironment

flyway

Yes

No

flyway数据迁移信息(依赖Flyway)

health

Yes

Yes

展示应用的健康信息

heapdump

N/A

No

(web应用时)hprof 堆的dump文件(依赖HotSpot JVM)

httptrace

Yes

No

展示HTTP trace信息, 默认展示前100个(依赖HttpTraceRepository)

info

Yes

No

应用信息

integrationgraph

Yes

No

展示spring集成信息(依赖spring-integration-core)

jolokia

N/A

No

(web应用时)通过HTTP暴露JMX beans(依赖jolokia-core)

logfile

N/A

No

(web应用时)如果配置了logging.file.name 或者 logging.file.path,展示logfile内容

loggers

Yes

No

展示或者配置loggers,比如修改日志的等级

liquibase

Yes

No

Liquibase 数据迁移信息(依赖Liquibase)

metrics

Yes

No

指标信息

mappings

Yes

No

@RequestMapping映射路径

prometheus

N/A

No

(web应用时)向prometheus暴露监控信息(依赖micrometer-registry-prometheus)

quartz

Yes

No

展示 quartz任务信息

scheduledtasks

Yes

No

展示Spring Scheduled 任务信息

sessions

Yes

No

session信息

shutdown

Yes

No

关闭应用

startup

Yes

No

展示ApplicationStartup的startup步骤的数据(依赖通在SpringApplication配置BufferingApplicationStartup)

threaddump

Yes

No

线程dump

当然也可以自己定义暴露哪些endpoint。

2.代码实现:

创建项目actuator

添加依赖

 
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.springframework.boot
            spring-boot-starter-actuator
        
    

yml配置

server:
  port: 8080

management:
  endpoints:
    enabled-by-default: false
    web:
      base-path: /cxh
      exposure:
        include: 'info,health,env,beans,date'
  endpoint:
    info:
      enabled: true
    health:
      enabled: true
    env:
      enabled: true
    beans:
      enabled: true
    date:
      enabled: true

自定义Endpoint

​通过@JmxEndpoint or @WebEndpoint注解来定义自己的endpoint, 然后通过@ReadOperation, @WriteOperation或者@DeleteOperation来暴露操作,比如添加系统时间date的endpoint

import java.time.LocalDateTime;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.web.annotation.WebEndpoint;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;


@RestController("custom")
@WebEndpoint(id = "date")
public class CustomEndpointController {

    @ReadOperation
    public ResponseEntity currentDate() {
        return ResponseEntity.ok(LocalDateTime.now().toString());
    }
}

3.实现效果:

运行项目actuator,浏览器打开http://localhost:8080/cxh

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/733063.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号