当前位置:首页 >> 数码
数码

SpringBoot能用Actuator来监控应用的方法

2025-08-02 12:19

t..enabled的装配顺利已完成,以shutdown东南侧为例:

management:

endpoint:

shutdown:

enabled: true

如果你只想要顺利已完成某个东南侧,你可以向请注意这样:

management:

endpoints:

enabled-by-default: false # 不完工预设装配

endpoint:

info:

enabled: true

这个下例,只不会完工info东南侧。

曝露东南侧

东南侧可能不会包含铭感接收者,考虑曝露的时候应当遇到困难。

web软件包预设曝露的东南侧有且均两个:health和info。 JMX软件包预设曝露所有的东南侧。

我们可以用到include和exclude类型来控制东南侧是否是很难曝露。请注意是两个个数得注意

一、不让JMX曝露所有东南侧,只让它曝露health和info两个东南侧。

management:

endpoints:

jmx:

exposure:

include: "health,info"

二、曝露web除env和beans外的所有东南侧。

management:

endpoints:

web:

exposure:

include: "*"

exclude: "env,beans"

management.endpoints.web.exposure.include=*

management.endpoints.web.exposure.exclude=env,beans

装配东南侧

东南侧将不会定时缓存对不带任何表达式的读取转换的号召。你可以用到cache.time-to-live类型装配。

所列下例将Bean东南侧的缓存的生存时间设为为10秒。

management:

endpoint:

beans:

cache:

time-to-live: "10s"

发现页面

预设你可以到访/actuator页面授予所有的东南侧接收者:

{

_links: {

self: {

href: "",

templated: false

},

health: {

href: "",

templated: false

},

health-path: {

href: "{*path}",

templated: true

},

info: {

href: "",

templated: false

}

}

}

跨域支持者

预设情形,CORS支持者是禁用的,并且只能在设为了management.endpoints.web.cors.allowed-origins类型后才完工。所列装配必需来自example.com域的GET和POST加载:

management:

endpoints:

web:

cors:

allowed-origins: ""

allowed-methods: "GET,POST"

完整的选项可以检视:CorsEndpointProperties

意味着一个假定的东南侧

我们直接来看一个个数得注意吧:

@Component // 让Spring容器负责管理

@Endpoint(id = "myEndPoint") //web和jmx公用, @WebEndpoint坚称指明web

public class MyEndpoint {

Runtime runtime = Runtime.getRuntime();

@ReadOperation // 读转换

public Map getData() {

Map map = new HashMap<>();

map.put("available-processors", runtime.availableProcessors());

map.put("free-memory", runtime.freeMemory());

map.put("max-memory", runtime.maxMemory());

map.put("total-memory", runtime.totalMemory());

return map;

}

}

@Component表明让这个bean给Spring容器负责管理。 @Endpoint标示出这是个东南侧,类似的还有@WebEndpoint标示出的是特指web软件包。 id = "myEndPoint"限制到访东南侧的路径:/actuator/myEndPoint。 @ReadOperation规定HTTP请的作法为GET,@ReadOperation为POST,@DeleteOperation为DELETE。

每一次我们试验里面一下,于是又试验里面之前,不要就让yml里面顺利已完成设为:

server:

port: 8081

management:

endpoints:

web:

exposure:

include: '*' # 很难新开的东南侧。预设个数只锁上 health 和 info 两个东南侧。通过设为 *

enabled-by-default: false

endpoint:

myEndPoint:

enabled: true

我们因故废弃其他所有的东南侧,只瞩目myEndPoint东南侧,接着顺利已完成软件包,到访:,授予json接收者。

{

max-memory: 3787980800,

free-memory: 235775968,

available-processors: 4,

total-memory: 298844160

}

实际上如果是用于SpringMVC或Spring WebFlux,我们可以用到@RestControllerEndpoint或@ControllerEndpoint注解,假定一个更是符合我们经常web开发模式的东南侧,你可以看一下请注意这个个数得注意。

@Component

@RestControllerEndpoint(id = "web")

public class MyWebEndPoint {

@GetMapping("/")

public Map getData() {

// ...

return map;

}

}

Health东南侧

东南侧这么多,我们挑几个学习一下,health作为预设新开的东南侧之一,还是很难好好探究一下的。

设为何时看出接收者

你可以用到受益的心理健康接收者去检查和你的分析作法软件包,曝露的health接收者依赖性于请注意两个类型装配:

management.endpoint.health.show-details

management.endpoint.health.show-components

这两个类型可以设为的个数如下:

health东南侧通过心理健康收发器HealthIndicator获取不同的水资源的心理健康接收者,且Autuator移动设备了多个HealthIndicator的意味着,或许啦或许啦,如果很难可以看看官网 Auto-configured HealthIndicators。

另外,如果你打算完工或者禁用指明的收发器,你可以装配management.health.key.enabled,key很难被换成,官网表格上有。

如果你打算设为全部的,你可以装配management.health.defaults.enabled

设为依次

你可以通过所列设为看出依次:

management:

endpoint:

health:

status:

order: "fatal,down,out-of-service,unknown,up"

设为号召编码方式

号召编码方式中间体了总体心理健康精神状态。预设情形,OUT_OF_SERVICE和DOWN的给定为503,其他精神状态为200。你可以按照请注意的手段设为给定:

management:

endpoint:

health:

status:

http-mapping:

down: 503

fatal: 503

out-of-service: 503

自假定心理健康接收者

纵使,Actuator现在获取了许多移动设备意味着,总不会满足不了我们的所需,那如何去自假定呢?

注册意味着HealthIndicator上端口的Spring Bean。 获取health()作法的意味着并返回心理健康号召,以外精神状态和其他很难看出的简略接收者。

所列代编码方式展现了一个案例:

import org.springframework.boot.actuate.health.Health;

import org.springframework.boot.actuate.health.HealthIndicator;

import org.springframework.stereotype.Component;

import java.util.Random;

@Component

public class MyHealthIndicator implements HealthIndicator {

@Override

public Health health() {

// perform some specific health check

boolean check = check();

if (!check) {

return Health.down().withDetail("Error Code", 0).build();

}

return Health.up().build();

}

private boolean check() {

return new Random().nextBoolean();

}

}

id就是bena的名称改成HealthIndicator冠词,这里id就是my。

看看特别的yml怎么装配?

management:

endpoints:

web:

exposure:

include: '*' # 很难新开的东南侧。预设个数只锁上 health 和 info 两个东南侧。通过设为 * ,可以新开所有东南侧。

enabled-by-default: false

endpoint:

health:

enabled: true

show-details: always # 何时看出完整的心理健康接收者

show-components: always

status:

http-mapping: # 设为不同心理健康精神状态相同的号召精神状态编码方式

DOWN: 503

order: FATAL, DOWN, OUT_OF_SERVICE, UP, UNKNOWN # 精神状态顺序排列

试验里面一下,到访:/actuator/health,可以无论如何多点几次,不会出现UP和DOWN的情况,受益所列接收者:

{

status: "DOWN",

components: {

diskSpace: {

status: "UP",

details: {

total: 267117391872,

free: 130840469504,

threshold: 10485760,

exists: true

}

},

my: {

status: "DOWN",

details: {

Error Code: 0

}

},

ping: {

status: "UP"

}

}

}

my相同的就是我们自假定的MyHealthIndicator,里面简略的接收者有:精神状态status,接收者details。 diskSpace相同DiskSpaceHealthIndicator,ping相同的是PingHealthIndicator。

其他的东南侧,热衷的小伙伴可以去到官网一一试验里面,总之,通过这些Actuator获取的东南侧,我们很容易就很难跟踪负责管理我们的分析作法软件包。

来源互联网后上端架构

推荐学习者:

Springboot如何用到Aspect来意味着六角形会话

java开发之SpringBoot发送和重定向

前上端开发SpringBoot之上端口软件包的生成

哪家白癜风医院较好
吉林哪家治疗白癜风医院好
鸡西治白癜风哪里最好

上一篇: 物联网云平台将成为IT能力一种,成为企业数字化转型的助力者

下一篇: 他在“5G”山沟起飞

友情链接