当前位置: 移动技术网 > IT编程>开发语言>Java > 详解spring-boot actuator(监控)配置和使用

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

2019年07月19日  | 移动技术网IT编程  | 我要评论

摩托罗拉v系列,陕西传媒网,柳河县

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

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"}}

官网:

源代码参考:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网