分享

SpringBoot整合Actuator

 码农9527 2021-09-08

 SpringBootActuator提供了很多生产级的特性,比如监控和度量SpringBoot应用程序。Actuator的这些特性可以通过众多REST接口、远程shell和JMX获得。Actuator也可以和一些外部的应用监控系统整合(Prometheus,Graphite,DataDog,Influx,Wavefront,NewRelic等),通过一个统一友好的界面,监视和管理你的应用程序。

SpringBoot整合Actuator

    开启监控

    引入maven依赖:

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>1234复制代码类型:[java]

    依赖引入之后可以直接启动程序并在浏览器访问:

    可以得到如下结果:

    (UP:代表应用正常、DOWN:代表应用不正常、UNKNOWN代表未知状态。)

    从上面的相应结果来看,我们得到的监控信息非常有限,想要更多信息的话,需要在application.yml中进行配置:

management:
  endpoint:
 health:
   show-details: always
# 自定义一些相关信息,暴露给调用者
info:
  app:
 name: family
 describe: java123456789复制代码类型:[java]

    执行代码,在浏览器访问:

    http://localhost:8888/actuator/info

    还可以开放监控端点给服务调用者:

management:
  # 开放所有监控端点
  endpoints:
 web:
   exposure:
  include: '*'123456复制代码类型:[java]
management:
  # 开放访问的服务端点
  endpoints:
 web:
   exposure:
  include: beans,env
management:
  # 不暴露对外开放的服务端点
  endpoints:
 web:
   exposure:
  include: mappings123456789101112复制代码类型:[java]

    开启端点和开放端点是不一样的,绝大部分的监控端点是默认开启的,少部分监控端点默认是不开启的,对于默认不启用的监控服务端点,一定要先开启:

management:
  endpoint:
 shutdown:
   enabled: true1234复制代码类型:[java]

    shutdown可以替换。

    Actuator提供的接口

    Actuator提供了13个接口,可以分为三大类:配置接口、度量接口和其它接口,具体如下表所示。

HTTP 方法路径描述
GET/autoconfig提供了一份自动配置报告,记录哪些自动配置条件通过了,哪些没通过
GET/configprops描述配置属性(包含默认值)如何注入Bean
GET/beans描述应用程序上下文里全部的Bean,以及它们的关系
GET/dump获取线程活动的快照
GET/env获取全部环境属性
GET/env/{name}根据名称获取特定的环境属性值
GET/health报告应用程序的健康指标,这些值由HealthIndicator的实现类提供
GET/info获取应用程序的定制信息,这些信息由info打头的属性提供
GET/mappings描述全部的URI路径,以及它们和控制器(包含Actuator端点)的映射关系
GET/metrics报告各种应用程序度量信息,比如内存用量和HTTP请求计数
GET/metrics/{name}报告指定名称的应用程序度量值
POST/shutdown关闭应用程序,要求endpoints.shutdown.enabled设置为true
GET/trace提供基本的HTTP请求跟踪信息(时间戳、HTTP头等)

    服务保护缓存及跨域

    我们将服务端点开放时,面向的对象是用户而不是对外的所有人。所以要对角色进行控制,下面来给大家用SpringSecurity来配置实现对Actuator服务端点的保护。

    首先引入依赖:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-security</artifactId>
</dependency>1234复制代码类型:[java]

    之后在SpringSecurity权限管理配置,在Configuration文件夹下创建

    ActuatorSecurity.java:

package com.example.demo.configuration;import org.springframework.context.annotation.Configuration;import org.springframework.security.config.annotation.web.builders.HttpSecurity;import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;@Configurationpublic class ActuatorSecurity extends WebSecurityConfigurerAdapter { @Override
 protected void configure(HttpSecurity http) throws Exception {
  http.httpBasic().and()
 .authorizeRequests() // 有ACTUATOR_ADMIN角色标识的用户才能访问
 .antMatchers("/actuator/*").hasRole("ADMIN") // 必须登录认证才能访问
 .antMatchers("/actuator/*").authenticated();
 }
}123456789101112131415161718复制代码类型:[java]

    在application.yml中配置,添加一个用户及其具有的角色

spring:
  security:
 user:
   name: family
   password: 123456
   roles:
  ADMIN1234567复制代码类型:[java]

    在浏览器访问:

    http://localhost:8888/actuator/health

    服务端点缓存

    不带参数的端点请求SpringBoot会自动进行缓存,通过下面的配置可以设置缓存时间。

management:
  endpoint:
 beans:
   cache:
  time-to-live: 200s12345复制代码类型:[java]

    如果端点添加了SpringSecurity保护,服务端点的响应结果将不会被缓存。

    我们前面一直默认使用“/actuator”作为服务访问的根路径,但是这会造成安全隐患,所以可以做一些个性化配置。

management:
  endpoints:
 web:
   base-path: /family
   path-mapping:
  health: healthcheck123456复制代码类型:[java]

    配置修改之后访问链接变成:

    http://localhost:8888/actuator/health

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多