spring boot starter actuator(健康监控)配置和使用教程

2025-05-29 0 88

添加pom依赖:

?

1

2

3

4

5

6

7

8

9
<!-- spring-boot-监控-->

<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>

application.yml中指定监控的http端口(如果不指定,则使用和server相同的端口);指定去掉某项的检查(比如不监控health.mail):

?

1

2

3

4

5

6
server:

port: 8083

management:

port: 8083

security:

enabled: false #

监控和管理端点

端点名 描述
autoconfig 所有自动配置信息( positivematches :运行的, negativematches 未运行组件)
auditevents 审计事件
beans 所有bean的信息
configprops 所有配置属性
dump 线程状态信息
env 当前环境信息
health 应用健康状况
info 当前应用信息
metrics 应用的各项指标
mappings 应用@requestmapping映射路径
shutdown 关闭当前应用(默认关闭)
trace 追踪信息(最新的http请求)
heapdump 下载内存快照

读取配置文件application.properties的 info.*属性

在infoproperties 读取

application.properties :

?

1

2
info.app.version=v1.2.0

info.app.name=abc

在gitproperties 获取git.properties 的信息

?

1

2

3

4

5
info.app.version=v1.2.0

info.app.name=abc

#远程关闭开启

endpoints.shutdown.enabled=true

#访问:http://localhost:8083/shutdown 关闭服务

metrics

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35
{

mem: 573549, //内存大小

mem.free: 388198, //内存剩余大小

processors: 4, //处理器数量

instance.uptime: 338426,

uptime: 345091,

systemload.average: -1,

heap.committed: 489984,

heap.init: 131072,

heap.used: 101785,

heap: 1842688,

nonheap.committed: 85056,

nonheap.init: 2496,

nonheap.used: 83566,

nonheap: 0,

threads.peak: 46,

threads.daemon: 36,

threads.totalstarted: 72,

threads: 39, //线程

classes: 12109,

classes.loaded: 12109, //加载的类

classes.unloaded: 0, //没加载的类

gc.ps_scavenge.count: 10,

gc.ps_scavenge.time: 103,

gc.ps_marksweep.count: 3,

gc.ps_marksweep.time: 219,

httpsessions.max: -1,

httpsessions.active: 0,

gauge.response.mappings: 3,

gauge.response.autoconfig: 4,

gauge.response.trace: 167,

counter.status.200.mappings: 1,

counter.status.200.autoconfig: 2,

counter.status.200.trace: 1

}

自定义配置说明:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17
#关闭metrics功能

endpoints.metrics.enabled=false

#开启shutdown远程关闭功能

endpoints.shutdown.enabled=true

#设置beansid

endpoints.beans.id=mybean

#设置beans路径

endpoints.beans.path=/bean

#关闭beans 功能

endpoints.beans.enabled=false

#关闭所有的

endpoints.enabled=false

#开启单个beans功能

endpoints.beans.enabled=true

#所有访问添加根目录

management.context-path=/manage​

management.port=8181

org.springframework.boot.actuate.health 包下对于所有的健康状态检查例如:redishealthindicator ,当有redis的starter 时候就会检查

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22
{

status: "down", //状态

diskspace: {

status: "up",

total: 395243941888,

free: 367246643200,

threshold: 10485760

},

rabbit: {

status: "down",

error: "org.springframework.amqp.amqpconnectexception: java.net.connectexception: connection refused: connect"

},

redis: {

status: "up",

version: "4.0.9"

},

db: {

status: "up",

database: "mysql",

hello: 1

}

}

自定义health

•自定义健康状态指示器

•1、编写一个指示器 实现 healthindicator 接口

•2、指示器的名字 xxxxhealthindicator

•3、加入容器中

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14
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() {

//自定义的检查方法

//health.up().build()代表健康

return health.down().withdetail("msg","服务异常").build();

}

}

总结

以上所述是小编给大家介绍的spring boot starter actuator(健康监控)配置和使用教程,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!

原文链接:https://blog.csdn.net/yangliuhbhd/article/details/80655847

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 spring boot starter actuator(健康监控)配置和使用教程 https://www.kuaiidc.com/111345.html

相关文章

发表评论
暂无评论