天天看点

1.9微服务--运行状态监控Actuator运行状态监控Actuator

运行状态监控Actuator

一、Actuator简介

SpringBoot的Actuator提供了运行状态监控的功能, Actuator 的监控数据可以通过REST、远程shell和JMX方式获得。首先介绍通过REST方式查看Actuator的节点的方法,这种是最常见且简单的方法:在工程的POM文件中引入Actuator的起步依赖spring-boot-starter-actuator,代码清单如下:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
           

在配置文件application-dev.properties中配置management.port 和management.security.enabled这两个配置分别配置了Actuator 对外暴露REST API 接口的端口号和Actuator采取非安全验证方式,其代码清单如下:

server:
  port: 8082
management:
  port: 9001
  security:
    enabled: false
endpoints:
  shutdown:
    enabled: true
           

在上述的配置代码中指定了Actuator对外暴露REST API 接口的端口为9001 ,如果不指定,端口为应用程序的启动端口,这样做的目的是将程序端口和程序的监控端口分开。

SpringBoot Actuator的关键特性是在应用程序里提供众多的Web节点,通过这些节点可以实时地了解应用程序的运行状况。有了Actuator ,你可以知道Bean在Spring应用程序上下文里是如何组装在一起的,并且可以获取环境属性的信息和运行时度量信息。Actuator 提供了13 个API 接口,用于监控运行状态的SpringBoot的状况,具体如下图所示:

1.9微服务--运行状态监控Actuator运行状态监控Actuator
1.9微服务--运行状态监控Actuator运行状态监控Actuator

二、查看运行程序的健康状态

打开浏览器访问http://localhost:9001/heath,可以查看应用程序的运行状态和磁盘状态等信息。浏览器显示的信息如下:

1.9微服务--运行状态监控Actuator运行状态监控Actuator

“/health ”API 接口提供的信息是由一个或多个健康指示器提供的健康信息的组合结果,下图列出了SpringBoot自带的健康指示器

1.9微服务--运行状态监控Actuator运行状态监控Actuator

三、查看运行程序的Bean

如果需要了解Spring Boot上下文注入了哪些Bean以及这些Bean 的类型、状态、生命周期等信息时, 只需要发起一个GET类型的请求,请求API 为“/beans”, 在浏览器上访问“ http://localhost:9001/beans ,浏览器会显示如下的信息:

1.9微服务--运行状态监控Actuator运行状态监控Actuator

在返回的信息中包含了Bean的以下4类信息:

1)bean: Spring 应用程序上下文中的Bean名称或Id

2)resource: class 文件的物理位置,通常是一个Url,指向构建出的Jar文件的路径

3)scope: Bean的作用域

4)type:Bean的类型

四、使用shell 连接Actuatr

通过REST API这种方式,开发人员通过Actuator可以很方便地了解运行中的程序的监控信息。Actuator也可以通过shell的方式连接。通过shell 连接Actuator,需要在工程的POM文件加上shell的起步依赖spring-boot-starter-remote-shell,代码如下:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-remote-shell</artifactId>
</dependency>
           

在程序的pom文件加上spring-boot-starter-remote-shell起步依赖后,启动Springboot应用程序,在程序的控制台会输出连接shell的密码,密码是随机的,每次都不一样,大概格式如下:

Using default password for shell access : 45f17018-5839-478e-alal-06de4cc82d4f

与密码相对应的用户名是user,可以通过远程SSH连接shell,它的端口是2000,这是固定的,连接上shell后,这时可以通过终端查看Actuator 的各个端点。SpringBoot提供了4个特有的shell命令,如图:

1.9微服务--运行状态监控Actuator运行状态监控Actuator

Actuator是Spring Boot一个非常重要的功能, Actuator为开发人员提供了Spring Boot的运行状态信息,通过Actuator可以查看程序的运行状态的信息

————————————————————————————————————————————

内容来源---《深入理解Spring Cloud与微服务构建》

继续阅读