springboot actuator_Spring Boot执行器的魔力

5529acb15bfc6313cdd8f6dd2899160f.png

Spring Boot提供了Spring Boot执行器模块,用于在应用程序投入生产时对其进行监视和管理。它提供的一些可用于生产的功能包括对应用程序的运行状况监视,事件审核以及从生产环境中收集指标。

为了启用Spring Boot执行器,我们需要在pom.xml中添加以下Spring Boot启动器Maven依赖项。


<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-actuator</artifactId>

</dependency>

</dependencies>

如果您使用的是Gradle,请在build.gradle中添加以下依赖项:


dependencies {

compile("org.springframework.boot:spring-boot-starter-actuator")

}

魔术#1:基本路径可以改变

执行器提供了各种内置的端点,还允许我们添加自己的端点。端点的ID,与前缀一起 /actuator, 被映射到URL。例如, /info 端点将映射到 /actuator/info。

的 /actuator 基本路径可以通过配置在application.properties的management.endpoints.web.base路径属性来改变。例如:


management.endpoints.web.base-path=/mypath

以上属性将端点URL从更改 /actuator/{ID} 为 /mypath/{ID}。例如,运行状况端点将变为/mypath/health。

魔术#2:应用程序的健康

如果我们想了解应用程序的运行状况,则可以使用运行状况端点。为了获取健康信息,我们只需要向发出GET请求 /actuator/health,如下所示。

$ curl'http :// localhost:8080 / actuator / health'-i -X GET

默认情况下,结果为:


$ curl 'http://localhost:8080/actuator/health' -i -X GET

如果要获取应用程序运行状况的完整详细信息,可以在application.properties中添加以下属性。

{

"status" : "UP"

}

结果响应如下所示。例如,如果您的应用程序具有MongoDB,Redis或MySQL之类的数据库,则运行状况终结点将显示其状态,也可以在下面看到(这是Mongo的示例):


{

"status":"UP",

"details":{

"db":{

"status":"UP",

"details":{

"database":"MONGO",

"message": "Hello I am UP"

}

},

"diskSpace":{

"status":"UP",

"details":{

"total":250790436864,

"free":100330897408,

"threshold":10485760

}

}

}

}

魔力三:自定义健康指标

执行器提供 HealthIndicator 和 AbstractHealthIndicator 接口,我们可以使用该接口来提供自定义运行状况指示器,如下所示。


@Component

public class MyHealthIndicator extends AbstractHealthIndicator {

@Override

protected void doHealthCheck(Health.Builder builder) throws Exception {

// Use the builder to build the health status details that should be reported.

// If you throw an exception, the status will be DOWN with the exception message.

builder.up()

.withDetail("app", "I am Alive")

.withDetail("error", "Nothing!");

}

}

现在,我们的健康指标如下所示。

{

"status":"UP",

"details":{

"custom":{

"status":"UP",

"details":{

"app":"I am Alive",

"error":"Nothing!"

}

},

"diskSpace":{

"status":"UP",

"details":{

"total":250790436864,

"free":97949245440,

"threshold":10485760

}

}

}

}

最后,开发这么多年我也总结了一套学习Java的资料与面试题,如果你在技术上面想提升自己的话,可以关注我,私信发送领取资料或者在评论区留下自己的联系方式,有时间记得帮我点下转发让跟多的人看到哦。

26db5e7f12947af0b6207e60a95e52b0.png

a4392e02b854d3f4746d5d5654bbc050.png
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值