Spring Boot Actuator 使用

Spring Boot Actuator

Spring Boot Actuator 的关键特性是在应用程序里提供众多 Web 接口,通过它们了解应用程序运行时的内部状况。Actuator 提供了 13 个接口,可以分为三大类:配置接口、度量接口和其它接口,具体如下表所示。

一 Actuator 的 REST 接口

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头等)

二 Actuator 配置

Maven 配置

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

Yaml 配置前缀

management.endpoint:									# Actuator Prefix
management.endpoint.<name>								# Endpoint Prefix

Web 上下文

# 默认配置
management.endpoints.web.base-path=/					
# 建议配置
management.endpoints.web.base-path=/actuator					

端点开放控制

  • jmx
  • web

	# 1. 启用、关闭
	management.endpoin.<name>.enabled=true				# 启用 shutdown 
	e:
	management.endpoin.shutdown.enabled=true			# 启用 shutdown
	management.endpoin.info.enabled=false				# 关闭 info
	
	# 2. 黑名单、白名单
	#  - 该配置关注 Http 、JMX 的白名单、黑名单规则。
	#  - exclude 黑名单优先级高于 include白名单
	management.endpoints.jmx.exposure.exclude=env,beans				# 排除	jmx
	management.endpoints.jmx.exposure.include=*								# 开放 jmx
	management.endpoints.web.exposure.exclude=env,beans				# 排除 jmx
	management.endpoints.web.exposure.include=*								# 开放 jmx
# 3. cache time
management.endpoint.beans.cache.time-to-live=10s

## 三 Security 配置

Maven 配置
```xml
   	<dependency>
   		<groupId>org.springframework.boot</groupId>
   		<artifactId>spring-boot-starter-security</artifactId>
   	</dependency>

Yaml 配置

	spring:
	  security:
	    user:
	      name: ***
	      password: ***

Java 配置类

package com.yuedu.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
 
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //对actuator监控所用的访问全部需要认证
        http.formLogin().and().authorizeRequests().antMatchers("/actuator/*").authenticated();
    }
}

参考:

  • https://www.jianshu.com/p/af9738634a21
  • https://blog.csdn.net/yaomingyang/article/details/84035975

官方:

  • https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html#actuator.endpoints
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值