spring-boot actuator(监控)配置和使用

来源:互联网 发布:网络综艺尺度大被叫停 编辑:程序博客网 时间:2024/06/05 15:52

转自Clement-Xu的csdn博客:http://blog.csdn.net/clementad/article/details/52045495

在生产环境中,需要实时或定期监控服务的可用性。spring-boot 的actuator(监控)功能提供了很多监控所需的接口。简单的配置和使用如下:


1、引入依赖:
[html] view plain copy
print?
  1. <dependency>  
  2.     <groupId>org.springframework.boot</groupId>  
  3.     <artifactId>spring-boot-starter-actuator</artifactId>  
  4. </dependency>  

如果使用http调用的方式,还需要这个依赖:
[html] view plain copy
print?
  1. <dependency>  
  2.     <groupId>org.springframework.boot</groupId>  
  3.     <artifactId>spring-boot-starter-web</artifactId>  
  4. </dependency>  

2、配置:

application.yml中指定监控的HTTP端口(如果不指定,则使用和server相同的端口);指定去掉某项的检查(比如不监控health.mail):

[plain] view plain copy
print?
  1. server:  
  2.   port: 8082  
  3. management:  
  4.   port: 54001  
  5.   health:  
  6.     mail:  
  7.       enabled: false  

3、使用:

查看health指标:http://localhost:54001/health
[plain] view plain copy
print?
  1. {"status":"UP","diskSpace":{"status":"UP","total":120031539200,"free":33554337792,"threshold":10485760},"db":{"status":"UP","dataSource1":{"status":"UP","database":"MySQL","hello":1},"dataSource2":{"status":"UP","database":"MySQL","hello":1}}}  

4、自定义指标:
4.1 /health:在某个类中implements HealthIndicator接口,然后实现其中的health()方法即可:

代码:

[java] view plain copy
print?
  1. @SpringBootApplication  
  2. @EnableScheduling  
  3. public class MySpringBootApplication implements HealthIndicator{  
  4.     private static Logger logger = LoggerFactory.getLogger(MySpringBootApplication.class);  
  5.       
  6.     public static void main(String[] args) {  
  7.         SpringApplication.run(MySpringBootApplication.class, args);  
  8.         logger.info("My Spring Boot Application Started");  
  9.     }  
  10.   
  11.     /** 
  12.      * 在/health接口调用的时候,返回多一个属性:"mySpringBootApplication":{"status":"UP","hello":"world"} 
  13.      */  
  14.     @Override  
  15.     public Health health() {  
  16.         return Health.up().withDetail("hello""world").build();  
  17.     }  
  18. }  

/health 运行结果(注意第二个指标):

{"status":"UP","mySpringBootApplication":{"status":"UP","hello":"world"},"diskSpace":{"status":"UP","total":120031539200,"free":33554337792,"threshold":10485760},"db":{"status":"UP","dataSource1":{"status":"UP","database":"MySQL","hello":1},"dataSource2":{"status":"UP","database":"MySQL","hello":1}}}


4.2 /info:配置如下,可以直接给一个字符串,也可以从pom.xml配置中获取

[plain] view plain copy
print?
  1. info:  
  2.   app:  
  3.     name: "@project.name@" #从pom.xml中获取  
  4.     description: "@project.description@"  
  5.     version: "@project.version@"  
  6.     spring-boot-version: "@project.parent.version@"  
/info的结果如下:

{"app":{"name":"my-spring-boot","description":"Test Project for Spring Boot","version":"1.0","spring-boot-version":"1.3.6.RELEASE"}}


官网:http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready

源代码参考:https://github.com/xujijun/my-spring-boot

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 厨房太阳对着晒怎么办 房子有太阳西斜怎么办 房子晒到太阳很热怎么办 房子被太阳热了怎么办 房间西晒窗帘不遮光怎么办 新建房屋一面墙体有裂缝怎么办 卫生间地砖缝隙出现渗水怎么办 西户窗户太晒怎么办 西晒的墙面很烫怎么办 儿童房颜色太粉了怎么办? 小孩在家里偷钱怎么办 脾气不好的猫该怎么办 二年孩子偷钱怎么办 孩子偷钱2000报警怎么办? 我儿子十岁老是偷钱怎么办 13孩子偷同学钱怎么办 孩子偷同学的钱怎么办 儿子十四岁了老偷钱怎么办 发现初中生的儿子偷钱怎么办 被亲戚怀疑儿子偷钱怎么办 房门选的太白了怎么办 大厅地砖颜色比墙砖浅怎么办 房屋外墙渗水物业不管怎么办 走丢了怎么办教学反思 托班教案迷路了怎么办 大班安全教案遇到小偷怎么办 小班孩子舞台表演找不到位置怎么办 懂你英语学完了怎么办 小班社会走丢了怎么办 帮小老鼠搬鸡蛋怎么办 小老鼠还能怎么办鸡蛋 中班教案走丢了怎么办 走丢了怎么办可后反思 社会走丢了怎么办教案 孩子一直不吃幼儿园的饭怎么办 大班安全游泳抽筋怎么办反思 汤洒了怎么办教学反思 迷路了怎么办小班详案 大班牙又痛又摇怎么办 大班安全教案着火了怎么办 小班社会生病了怎么办反思