spring-boot actuator(监控)配置和使用
2017-10-25 11:20
459 查看
转自Clement-Xu的csdn博客:http://blog.csdn.net/clementad/article/details/52045495
在生产环境中,需要实时或定期监控服务的可用性。spring-boot 的actuator(监控)功能提供了很多监控所需的接口。简单的配置和使用如下:
1、引入依赖:
[html]
view plain
copy
print?
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
如果使用http调用的方式,还需要这个依赖:
[html]
view plain
copy
print?
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2、配置:
application.yml中指定监控的HTTP端口(如果不指定,则使用和server相同的端口);指定去掉某项的检查(比如不监控health.mail):
[p
4000
lain]
view plain
copy
print?
server:
port: 8082
management:
port: 54001
health:
mail:
enabled: false
3、使用:
查看health指标:http://localhost:54001/health
[plain]
view plain
copy
print?
{"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?
@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 运行结果(注意第二个指标):
4.2 /info:配置如下,可以直接给一个字符串,也可以从pom.xml配置中获取
[plain]
view plain
copy
print?
info:
app:
name: "@project.name@" #从pom.xml中获取
description: "@project.description@"
version: "@project.version@"
spring-boot-version: "@project.parent.version@"
官网:http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready
源代码参考:https://github.com/xujijun/my-spring-boot
在生产环境中,需要实时或定期监控服务的可用性。spring-boot 的actuator(监控)功能提供了很多监控所需的接口。简单的配置和使用如下:
1、引入依赖:
[html]
view plain
copy
print?
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
如果使用http调用的方式,还需要这个依赖:
[html]
view plain
copy
print?
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
2、配置:
application.yml中指定监控的HTTP端口(如果不指定,则使用和server相同的端口);指定去掉某项的检查(比如不监控health.mail):
[p
4000
lain]
view plain
copy
print?
server:
port: 8082
management:
port: 54001
health:
mail:
enabled: false
server: port: 8082 management: port: 54001 health: mail: enabled: false
3、使用:
查看health指标:http://localhost:54001/health
[plain]
view plain
copy
print?
{"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}}}
{"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?
@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();
}
}
@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配置中获取
[plain]
view plain
copy
print?
info:
app:
name: "@project.name@" #从pom.xml中获取
description: "@project.description@"
version: "@project.version@"
spring-boot-version: "@project.parent.version@"
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 actuator(监控)配置和使用
- spring-boot actuator(监控)配置和使用
- spring-boot actuator(监控)配置和使用
- spring-boot actuator(监控)配置和使用
- 详解spring-boot actuator(监控)配置和使用
- Spring Boot 使用 Druid 和监控配置
- Spring Boot 使用 Druid 和监控配置
- Spring Boot 使用 Druid 和监控配置
- mybatis使用spring-druid数据源连接池配置log4j打印sql语句以及开启监控平台
- Spring Boot 使用 Druid 和监控配置
- Spring Boot使用Druid和监控配置方法
- (15)Spring Boot使用Druid和监控配置【从零开始学Spring Boot】
- 使用Spring Boot Actuator、Jolokia和Grafana实现准实时监控--转
- Spring Boot 使用 Druid 和监控配置
- SpringBoot:spring boot使用Druid和监控配置
- 使用Spring Boot Actuator、Jolokia和Grafana实现准实时监控
- 15、Spring Boot使用Druid和监控配置【从零开始学Spring Boot】
- Spring Boot使用Druid和监控配置
- 使用Spring Boot Actuator、Jolokia和Grafana实现准实时监控
- 15、Spring Boot使用Druid和监控配置【从零开始学Spring Boot】