加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

springboot 使用Spring Boot Actuator监控应用小结

发布时间:2020-05-23 21:56:28 所属栏目:Java 来源:互联网
导读:微服务的特点决定了功能模块的部署是分布式的,大部分功能模块都是运行在不同的机器上,彼此通过服务调用进行交互,前后台的业务流会经过很多个微服务的处理和传递,出现了异常如何快速定位是哪个环节出现了问题?

微服务的特点决定了功能模块的部署是分布式的,大部分功能模块都是运行在不同的机器上,彼此通过服务调用进行交互,前后台的业务流会经过很多个微服务的处理和传递,出现了异常如何快速定位是哪个环节出现了问题?

在这种框架下,微服务的监控显得尤为重要。本文主要结合Spring Boot Actuator,跟大家一起分享微服务Spring Boot Actuator的常见用法,方便我们在日常中对我们的微服务进行监控治理。

Actuator监控

Spring Boot使用“习惯优于配置的理念”,采用包扫描和自动化配置的机制来加载依赖jar中的Spring bean,不需要任何Xml配置,就可以实现Spring的所有配置。虽然这样做能让我们的代码变得非常简洁,但是整个应用的实例创建和依赖关系等信息都被离散到了各个配置类的注解上,这使得我们分析整个应用中资源和实例的各种关系变得非常的困难。

Actuator是Spring Boot提供的对应用系统的自省和监控的集成功能,可以查看应用配置的详细信息,例如自动化配置信息、创建的Spring beans以及一些环境属性等。

Actuator监控只需要添加以下依赖就可以完成

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

为了保证actuator暴露的监控接口的安全性,需要添加安全控制的依赖spring-boot-start-security依赖,访问应用监控端点时,都需要输入验证信息。Security依赖,可以选择不加,不进行安全管理,但不建议这么做。

Actuator 的 REST 接口

Actuator监控分成两类:原生端点和用户自定义端点;自定义端点主要是指扩展性,用户可以根据自己的实际应用,定义一些比较关心的指标,在运行期进行监控。

原生端点是在应用程序里提供众多 Web 接口,通过它们了解应用程序运行时的内部状况。原生端点又可以分成三类:

  1. 应用配置类:可以查看应用在运行期的静态信息:例如自动配置信息、加载的springbean信息、yml文件配置信息、环境信息、请求映射信息;
  2. 度量指标类:主要是运行期的动态信息,例如堆栈、请求连、一些健康指标、metrics信息等;
  3. 操作控制类:主要是指shutdown,用户可以发送一个请求将应用的监控功能关闭。

Actuator 提供了 13 个接口,具体如下表所示。

快速上手

相关配置

项目依赖

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

配置文件

server:
 port: 8080
management:
 security:
 enabled: false #关掉安全认证
 port: 8088 #管理端口调整成8088
 context-path: /monitor #actuator的访问路径
endpoints:
 shutdown:
 enabled: true

info:
 app:
  name: spring-boot-actuator
  version: 1.0.0

  1. management.security.enabled=false 默认有一部分信息需要安全验证之后才可以查看,如果去掉这些安全认证,直接设置management.security.enabled=false
  2. management.context-path=/monitor 代表启用单独的url地址来监控Spring Boot应用,为了安全一般都启用独立的端口来访问后端的监控信息
  3. endpoints.shutdown.enabled=true 启用接口关闭Spring Boot

配置完成之后,启动项目就可以继续验证各个监控功能了。

命令详解

autoconfig

Spring Boot的自动配置功能非常便利,但有时候也意味着出问题比较难找出具体的原因。使用 autoconfig 可以在应用运行时查看代码了某个配置在什么条件下生效,或者某个自动配置为什么没有生效。

启动示例项目,访问: http://localhost:8088/monitor/autoconfig 返回部分信息如下:

{
 "positiveMatches": {
  "DevToolsDataSourceAutoConfiguration": {
   "notMatched": [
    {
     "condition": "DevToolsDataSourceAutoConfiguration.DevToolsDataSourceCondition","message": "DevTools DataSource Condition did not find a single DataSource bean"
    }
   ],"matched": [ ]
  },"RemoteDevToolsAutoConfiguration": {
   "notMatched": [
    {
     "condition": "OnPropertyCondition","message": "@ConditionalOnProperty (spring.devtools.remote.secret) did not find property 'secret'"
    }
   ],"matched": [
    {
     "condition": "OnClassCondition","message": "@ConditionalOnClass found required classes 'javax.servlet.Filter','org.springframework.http.server.ServerHttpRequest'; @ConditionalOnMissingClass did not find unwanted class"
    }
   ]
  }
 }
}

configprops

查看配置文件中设置的属性内容,以及一些配置属性的默认值。

启动示例项目,访问: http://localhost:8088/monitor/configprops 返回部分信息如下:

{
 ...
 "environmentEndpoint": {
 "prefix": "endpoints.env","properties": {
  "id": "env","sensitive": true,"enabled": true
 }
 },"spring.http.multipart-org.springframework.boot.autoconfigure.web.MultipartProperties": {
 "prefix": "spring.http.multipart","properties": {
  "maxRequestSize": "10MB","fileSizeThreshold": "0","location": null,"maxFileSize": "1MB","enabled": true,"resolveLazily": false
 }
 },"infoEndpoint": {
 "prefix": "endpoints.info","properties": {
  "id": "info","sensitive": false,"enabled": true
 }
 }
 ...
}

beans

根据示例就可以看出,展示了bean的别名、类型、是否单例、类的地址、依赖等信息。

启动示例项目,访问: http://localhost:8088/monitor/beans 返回部分信息如下:

[
 {
 "context": "application:8080:management","parent": "application:8080","beans": [
  {
  "bean": "embeddedServletContainerFactory","aliases": [
   
  ],"scope": "singleton","type": "org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory","resource": "null","dependencies": [
   
  ]
  },{
  "bean": "endpointWebMvcChildContextConfiguration","type": "org.springframework.boot.actuate.autoconfigure.EndpointWebMvcChildContextConfiguration$$EnhancerBySpringCGLIB$$a4a10f9d","dependencies": [
   
  ]
  }
 }
]

dump

/dump 接口会生成当前线程活动的快照。这个功能非常好,方便我们在日常定位问题的时候查看线程的情况。 主要展示了线程名、线程ID、线程的状态、是否等待锁资源等信息。

启动示例项目,访问: http://localhost:8088/monitor/dump 返回部分信息如下:

[
 {
 "threadName": "http-nio-8088-exec-6","threadId": 49,"blockedTime": -1,"blockedCount": 0,"waitedTime": -1,"waitedCount": 2,"lockName": "java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject@1630a501","lockOwnerId": -1,"lockOwnerName": null,"inNative": false,"suspended": false,"threadState": "WAITING","stackTrace": [
  {
  "methodName": "park","fileName": "Unsafe.java","lineNumber": -2,"className": "sun.misc.Unsafe","nativeMethod": true
  },{
  "methodName": "park","fileName": "LockSupport.java","lineNumber": 175,"className": "java.util.concurrent.locks.LockSupport","nativeMethod": false
  },{
  "methodName": "await","fileName": "AbstractQueuedSynchronizer.java","lineNumber": 2039,"className": "java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject",...
  {
  "methodName": "getTask","fileName": "ThreadPoolExecutor.java","lineNumber": 1067,"className": "java.util.concurrent.ThreadPoolExecutor",{
  "methodName": "runWorker","lineNumber": 1127,{
  "methodName": "run","lineNumber": 617,"className": "java.util.concurrent.ThreadPoolExecutor$Worker","fileName": "TaskThread.java","lineNumber": 61,"className": "org.apache.tomcat.util.threads.TaskThread$WrappingRunnable","fileName": "Thread.java","lineNumber": 745,"className": "java.lang.Thread","nativeMethod": false
  }
 ],"lockedMonitors": [
  
 ],"lockedSynchronizers": [
  
 ],"lockInfo": {
  "className": "java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject","identityHashCode": 372286721
 }
 }
 ...
]

env

展示了系统环境变量的配置信息,包括使用的环境变量、JVM 属性、命令行参数、项目使用的jar包等信息。和configprops不同的是,configprops关注于配置信息,env关注运行环境信息。

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读