spring-boot actuator(监控)配置和使用
2016-07-27 15:01
302 查看
在生产环境中,需要实时或定期监控服务的可用性。spring-boot 的actuator(监控)功能提供了很多监控所需的接口。简单的配置和使用如下:
1、引入依赖:
如果使用http调用的方式,还需要这个依赖:
2、配置:
application.yml中指定监控的HTTP端口(如果不指定,则使用和server相同的端口);指定去掉某项的检查(比如不监控health.mail):
3、使用:
查看health指标:http://localhost:54001/health
4、自定义指标:
4.1 /health:在某个类中implements HealthIndicator接口,然后实现其中的health()方法即可:
代码:
/health 运行结果(注意第二个指标):
4.2 /info:配置如下,可以直接给一个字符串,也可以从pom.xml配置中获取
info:
app:
name: "@project.name@" #从pom.xml中获取
description: "@project.description@"
version: "@project.version@"
spring-boot-version: "@project.parent.version@"/info的结果如下:
官网:http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready
源代码参考:https://github.com/xujijun/my-spring-boot
1、引入依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
如果使用http调用的方式,还需要这个依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
2、配置:
application.yml中指定监控的HTTP端口(如果不指定,则使用和server相同的端口);指定去掉某项的检查(比如不监控health.mail):
server: port: 8082 management: port: 54001 health: mail: enabled: false
3、使用:
查看health指标:http://localhost:54001/health
{"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()方法即可:
代码:
@SpringBootApplication @EnableScheduling public class MySpringBootApplication implements HealthIndicator{ private static Logger logger = LoggerFactory.getLogger(MySpringBootApplication.class); public static void main(String[] args) { SpringApplication.run(MySpringBootApplication.class, args); logger.info("My Spring Boot Application Started"); } /** * 在/health接口调用的时候,返回多一个属性:"mySpringBootApplication":{"status":"UP","hello":"world"} */ @Override public Health health() { return Health.up().withDetail("hello", "world").build(); } }
/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配置中获取
info:
app:
name: "@project.name@" #从pom.xml中获取
description: "@project.description@"
version: "@project.version@"
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
相关文章推荐
- Spring boot中使用MyBatis Generator
- Spring boot Jetty 服务器相比tomcat的一些不足
- 使用了Spring boot devtools, dozer转换嵌套对象失败解决记录
- Springfox swagger-ui 覆盖默认request host
- Spring Boot学习笔记-快速示例
- Spring Boot学习笔记—日志
- Spring Boot学习笔记-SQL数据库使用
- Spring Boot学习笔记-错误处理及自定义
- Spring Boot学习笔记-外部化属性配置
- 使用Spring Boot Gradle 打war包的一点心得体会
- spring boot和mybatis整合
- Spring-boot生成可执行jar包
- 监控和管理生产环境spring boot actuator
- 开始使用Spring Boot
- spring-boot 所有的配置
- spring boot log4j2配置(使用log4j2.yml文件)
- spring boot 指定maven package命令生成的jar文件名
- spring boot在Eclipse中,修改代码后无需重启就生效的配置
- spring boot 实现Restful API
- spring boot 静态页面展示