1.首先加入maven配置:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
2.在配置文件中写入:
management: health: mail: enabled: false #意思是邮件发送不是核心功能,所以它从健康检查中排出,避免造成他挂机了导致整个服务不可用。 endpoint: web: exposure: include: "health" #默认级别为info,health,beans,env。选择暴露,不暴露include换成exlcude即可 health: show-details: always #详细信息显示给所有用户。级别还有never级别不显示,when-authorized,指得是详细信息给特别用户。
3.如果不需要额外的配置这些即可,在网站上输出:http://localhost:8011/actuator/health。即可看到自己服务的健康情况。status为up为成功,为down失败。
4.如果你想要想要验证一些特殊业务加入进去验证,比如在ck查询的时候看看服务器有没有宕机。
继承AbstractHealthIndictor类,重写doHealthCheck方法。
@Slf4j public class DbHealthIndicator extends AbstractHealthIndicator { @Override protected void doHealthCheck(Health.Builder builder) throws Exception { try { int mysql = 1;//需要验证的东西 builder.up().withDetail("验证:", mysql).withDetail("mysql result", mysqlAlive).build(); } catch (Exception e) { builder.down().withDetail("验证", mysql).withDetail("error msg", e.getMessage()).build(); } }
成品如下图所示:
当然也可以实现healthIndicator,实现health方法,有兴趣可以看看源码。以上这样可以方便的检测一些主要的问题。