Springboot的actuator监控

在生产环境中,需要实时监控程序的可用性,出现问题之后我们需要快速定位,spring-boot 的 actuator 功能提供了很多监控所需的接口。actuator是spring boot提供的对应用系统的自省和监控的集成功能,可以对应用系统进行配置查看、健康检查、相关功能统计等;方便运维人员查看spring boot的运行状况。

使用actuator

spring boot为actuator提供了起步依赖starter,我们需要在pom中添加下面starter:



    org.springframework.boot
    spring-boot-starter-web



    org.springframework.boot
    spring-boot-starter-actuator



    de.codecentric
    spring-boot-admin-starter-client

在配置文件中添加下面配置

#设置actuator监控端口
management.server.port=8081

#开启所有监控,默认只开启health和info
management.endpoints.web.exposure.include=*

#添加info信息
info.author=baron
info.url=www.baron

启动spring boot程序,在浏览器中输入:

http://localhost:8081/actuator/info

之后在浏览器中就可以看到配置文件里面添加的info信息了。

actuator的rest接口

在actuator中提供了很多接口,通过这些接口可以监控一些信息,这里列举一部分:

  • beans

    展示了bean的别名、类型、是否单例、类的地址、依赖等信息

  • env

    展示了系统环境变量的配置信息,包括使用的环境变量、JVM 属性等

  • health

    描述了应用程序的整体健康状态,UP 表明应用程序是健康的

  • mappings

    URl路径和控制器的映射关系。

spring boot admin图形化界面

上面通过actuator提供的rest接口,返回的数据都是json格式,这个对于不懂json格式的人来说不太方便,因此就产生了spring boot admin,它提供了图形化界面,通过界面来展示这些数据。

创建新的模块,加入相关依赖

    
        org.springframework.boot
        spring-boot-starter-actuator
    
    
        org.springframework.boot
        spring-boot-starter-web
    
    
    
        de.codecentric
        spring-boot-admin-starter-server
    

在spring boot的启动类上添加下面注解,开启admin

@EnableAdminServer

在配置文件中设置下端口号:

server.port=8082

上面的项目是作为server端,将之前的spring boot项目作为client端,由server端统一监控client端。

client的模块中,在配置文件里面添加下面内容:

#开启所有监控,默认只开启health和info
management.endpoints.web.exposure.include=*

#配置actuator admin主机地址
spring.boot.admin.client.url=http://localhost:8082

启动server和client,访问server端,http://localhost:8082 就可以看到spring boot admin的页面了。

你可能感兴趣的:(Springboot的actuator监控)