SpringBoot能用Actuator来监控应用的方法
2025-08-02 12:19
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”山沟起飞

-
曾4.4亿元摘地!北二环装饰城地块项目工程建设公示 拟建12栋住宅
门户网站焦点“楼市新闻报道”报道:4月初1日,门户网站焦点获悉,石家庄市生物资源和都市计划局发布了关于取悦园项目设计方案批前公示的公告。公告结果显示,河北筑谦房地产开

-
2021年,女子与男友吵架喝农药身亡,闺蜜全程帮忙,我先喝金子
其余部分口,就被旁边的物业兼职其他部门逃离现场了,程程口之中的那口杀菌剂也吐了出来。但珍珍就光景了!只见程程假的喝到下了杀菌剂,珍珍也不含糊,也拉起了杀菌剂红豆瓶。
- 10-24陈峻齐:强势避险依在 日内黄金1920支撑上在此期间多
- 10-24宁德蕉城区漳湾国际生活品质方舱驿站加速建设
- 10-24推荐几种锅贴的吃法,美味可口,做法有用,感兴趣的收藏吧
- 10-24改变自己,才有渴望
- 10-24黄金大卖!消费者:一只项链涨了几千元
- 10-24济南精准医学高新技术三地块14个单体预计今年9月投用
- 10-24新疆石河子新安酒业“3两3酒”甲醇检出被罚
- 10-24领导送盒茶叶给下属,怎样回礼情商高?铭记3个技巧,领导高看你
- 10-24农户投资屋顶光伏是不是骗局?之后不能碰,捂好口袋
- 10-24越做越走“心” 2022款斯巴鲁森林人白雪体验