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”山沟起飞
-
华友钴业:预计2022年1-6月初盈利,净利润同比增49.86%至77.11%
华友钴业2022-07-12发布新闻利润预告,预计2022年1-6月利润22亿元至26亿元,工业产值上年增49.86%至77.11%。发函里理解本次利润增减的因素为:
-
藏格矿业:预计2022年1-6月盈利,净利润同比续427.55%至450%
藏格矿山2022-07-12发布净资产预告,预计2022年1-6月盈利23.5亿元至24.5亿元,去年同期一季度减427.55%至450%。核定中解释本次净资产变动的原因为:p
- 08-23心骑士:“气宗”如何解锁,神殿何处激活?老玩家为您答疑解惑
- 08-23克明药品:预计2022年1-6月盈利,净利润同比增80.89%至171.33%
- 08-23北斗星通:预计2022年1-6月盈利,净利润同比增10.9%至28.7%
- 08-23融资丨「PIX Moving」完成A1轮融资,东洋TIS株式会社独家战略投资
- 08-23中信证券:汽车零部件赛道挖掘 建议注目5G网络、遥控泊车等
- 08-23APP七天粥,真的有必要吗?
- 08-23《谭谈交通网络》全网下架,谭sir面临千万索赔?
- 08-23多次纠结后,我为什么还是备用了抖音
- 08-23不再挣扎?分析师随之“认清形势”纷纷下调美股收益预期
- 08-237月12日投资避雷针:10天9板电力蟠龙停牌核查