Spring Boot Actuator:介绍和使用
Spring Boot Actuator提供一系列HTTP端点来暴露项目信息,用来监控和管理项目。在Maven中,可以添加以下依赖:
<!-- Spring boot starter: actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
[注] 在某些包中已经自动绑定了Spring Boot Actuator包,比如一些Cloud包spring-cloud-starter-security,spring-cloud-starter-netflix-eureka-server等,这时候不用再重复添加该依赖。
Actuator提供了以下端点,默认除了/shutdown都是Enabled。使用时需要加/actuator前缀,如http://localhost:8080/my-app/actuator/health。
| ID | Description | Enabled by default |
|---|---|---|
| auditevents | 显示当前应用程序的审计事件信息 | Yes |
| beans | 显示应用上下文中创建的所有Bean | Yes |
| caches | 获取缓存信息 | Yes |
| conditions | 显示配置类和自动配置类(configuration and auto-configuration classes) 的状态及它们被应用或未被应用的原因 | Yes |
| configprops | 该端点用来获取应用中配置的属性信息报告 (所有@ConfigurationProperties的集合列表) | Yes |
| env | 获取应用所有可用的环境属性报告。包括: 环境变量、JVM属性、应用的配置配置、命令行中的参数 | Yes |
| flyway | 显示数据库迁移路径(如果有) | Yes |
| health | 显示应用的健康信息 | Yes |
| httptrace | 返回基本的HTTP跟踪信息。 (默认最多100 HTTP request-response exchanges). | Yes |
| info | 返回一些应用自定义的信息,我们可以在application.properties 配置文件中通过info前缀来设置这些属性:info.app.name=spring-boot-hello | Yes |
| integrationgraph | Shows the Spring Integration graph. | Yes |
| loggers | Shows and modifies the configuration of loggers in the application. | Yes |
| liquibase | Shows any Liquibase database migrations that have been applied. | Yes |
| metrics | 返回当前应用的各类重要度量指标,比如:内存信息、线程信息、垃圾回收信息等 | Yes |
| mappings | 返回所有Spring MVC的控制器映射关系报告 (所有@RequestMapping路径的集合列表) | Yes |
| scheduledtasks | 显示应用程序中的计划任务 | Yes |
| sessions | 允许从Spring会话支持的会话存储中检索和删除(retrieval and deletion) 用户会话。使用Spring Session对反应性Web应用程序的支持时不可用 | Yes |
| shutdown | 允许应用以优雅的方式关闭(默认情况下不启用) | No |
| threaddump | 执行一个线程dump | Yes |
如果使用web应用(Spring MVC, Spring WebFlux, 或者 Jersey),还可以使用以下端点:
| ID | Description | Enabled by default |
|---|---|---|
| heapdump | 返回一个GZip压缩的hprof堆dump文件 | Yes |
| jolokia | 通过HTTP暴露JMX beans(当Jolokia在类路径上时,WebFlux不可用) | Yes |
| logfile | 返回日志文件内容(如果设置了logging.file或logging.path属性的话), 支持使用HTTP Range头接收日志文件内容的部分信息 | Yes |
| prometheus | 以可以被Prometheus服务器抓取的格式显示metrics信息 | Yes |
如果要启用/禁用某个端点,可以使用management.endpoint.<id>.enabled属性:
management:
endpoint:
shutdown:
enabled: true
另外可以通过management.endpoints.enabled-by-default来修改全局端口默认配置,比如下面禁用所有端点只启用info端点:
management:
endpoints:
enabled-by-default: false
endpoint:
info:
enabled: true
上面是启用/禁用(enable)某个端点,如果使某个端点暴露(exposure)出来,还需要再配置,默认情况下所有端点在JMX下是全部公开的,在Web下只公开/health和/info两个端点。下面是默认配置:
| Property | Default |
|---|---|
| management.endpoints.jmx.exposure.exclude | - |
| management.endpoints.jmx.exposure.include | '*' |
| management.endpoints.web.exposure.exclude | - |
| management.endpoints.web.exposure.include | info, health |
下面的例子是Web下公开所有端点:
management:
endpoints:
web:
exposure:
include: '*'
保护Actuator HTTP端点:
最简单的方式,就是在pom.xml中添加spring-boot-starter-security。由SpringBoot Security的特性可知,系统会自动给我们创建login/logout page,还有一个user和password,此外系统还会自动给我配置一个ManagementWebSecurityConfigurerAdapter(extends WebSecurityConfigurerAdapter),配置Actuator各个Endpoint的权限。
当然我们也可以自定义一个WebSecurityConfigurerAdapter配置自己的user和authority。
package com.mytools; import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
import org.springframework.boot.actuate.health.HealthEndpoint;
import org.springframework.boot.actuate.info.InfoEndpoint;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder; @Configuration
public class MyWebSecurityConfigurer extends WebSecurityConfigurerAdapter { @Bean
public PasswordEncoder passwordEncoder() {
return PasswordEncoderFactories.createDelegatingPasswordEncoder();
} @Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//@formatter:off
PasswordEncoder encoder = new BCryptPasswordEncoder();
auth.inMemoryAuthentication()
.withUser("user1").password("{bcrypt}" + encoder.encode("password1")).roles("ADMIN","EUREKA")
.and()
.withUser("user2").password("{bcrypt}" + encoder.encode("password2")).roles("EUREKA");
//@formatter:on
} @Override
protected void configure(HttpSecurity http) throws Exception {
// comes from ManagementWebSecurityAutoConfiguration and ManagementWebSecurityConfigurerAdapter
//@formatter:off
http.authorizeRequests()
.requestMatchers(EndpointRequest.to(HealthEndpoint.class, InfoEndpoint.class)).permitAll()
.anyRequest().authenticated()
.and()
.formLogin().and()
.httpBasic();
//@formatter:on
}
}
Spring Boot Actuator:介绍和使用的更多相关文章
- spring boot actuator专题
spring-boot-starter-actuator模块的实现对于实施微服务的中小团队来说,可以有效地减少监控系统在采集应用指标时的开发量.当然,它也并不是万能的,有时候我们也需要对其做一些简单的 ...
- Spring Boot Actuator监控使用详解
在企业级应用中,学习了如何进行SpringBoot应用的功能开发,以及如何写单元测试.集成测试等还是不够的.在实际的软件开发中还需要:应用程序的监控和管理.SpringBoot的Actuator模块实 ...
- 聊聊Spring Boot Actuator
概述 在本文中,我们将介绍Spring Boot Actuator.我们将首先介绍基础知识,然后详细讨论Spring Boot 1.x和2.x中的可用内容. 我们将在Spring Boot 1.x中学 ...
- spring boot actuator扩展httptrace的记录
SpringBoot记录HTTP请求日志 1.需求解读 需求: 框架需要记录每一个HTTP请求的信息,包括请求路径.请求参数.响应状态.返回参数.请求耗时等信息. 需求解读: Springboot框架 ...
- spring boot actuator端点高级进阶metris指标详解、git配置详解、自定义扩展详解
https://www.cnblogs.com/duanxz/p/3508267.html 前言 接着上一篇<Springboot Actuator之一:执行器Actuator入门介绍>a ...
- Complete Guide for Spring Boot Actuator
You are here to learn about Spring Boot Actuator for collecting metrics about your production grade ...
- springboot(十九):使用Spring Boot Actuator监控应用
微服务的特点决定了功能模块的部署是分布式的,大部分功能模块都是运行在不同的机器上,彼此通过服务调用进行交互,前后台的业务流会经过很多个微服务的处理和传递,出现了异常如何快速定位是哪个环节出现了问题? ...
- spring boot admin + spring boot actuator + erueka 微服务监控
关于spring boot actuator简单使用,请看 简单的spring boot actuator 使用,点击这里 spring boot admin 最新的正式版本是1.5.3 与 spri ...
- spring boot actuator 简单使用
spring boot admin + spring boot actuator + erueka 微服务监控 简单的spring boot actuator 使用 POM <dependenc ...
随机推荐
- @@identity, scope_identity ident_current 的区别
- Oracle 多表联合删除?--转
oracle和mysql多表删除数据的方法一大把,好多都是没经过证实的,你很可能已经被错误信息误导了,下面我以mysql两张表删除数据为例,来让给为注意到这一点,我在mysql中新建了两张表,分别是用 ...
- ubuntu redis 安装 &基本命令
参考资料:https://www.cnblogs.com/zongfa/p/7808807.htmlredis命令参考:http://doc.redisfans.com/安装:sudo apt-get ...
- Eclipse使用技巧小结
前言:自学Java以来,就一直用eclipse,这款ide深受广大新手和大牛喜爱.学会使用其中的技巧,越用越熟练,开发也就越快捷方便.话不多说,直接上小结吧. 一.快捷键 1.提示 :A|t+/ 2. ...
- ValueError: Expecting property name: line 1 column 2 (char 1)
代码: import json str2 = '{"domain":"456"}' str1 = "{'domain':'123'}" pr ...
- 6、组件注册-@Lazy-bean懒加载
6.组件注册-@Lazy-bean懒加载 懒加载:单实例bean,默认是在容器启动的时候创建对象:懒加载就是启动的是不创建,在第一次使用的时候再创建对象. @Lazy // 单实例下懒加载bean
- python自动华 (三)
Python自动化 [第三篇]:Python基础-集合.文件操作.字符编码与转码.函数 1. 集合 1.1 特性 集合是一个无序的,不重复的数据组合,主要作用如下: 去重,把一 ...
- [Functional Programming] Using ComposeK for both get State and modify State
We have State like this: const state = { cards: [ { id: "green-square", color: "green ...
- 020_统计 13:30 到 14:30 所有访问 apache 服务器的请求有多少个
统计 13:30 到 14:30 所有访问 apache 服务器的请求有多少个 #!/bin/bash#awk 使用-F 选项指定文件内容的分隔符是/或者:#条件判断$7:$8 大于等于 13:30, ...
- 安裝PHPBB
1.下載PHPBB https://www.phpbb.com/downloads/ 2下載PHP http://windows.php.net/download/ 很多教程都介紹在WIN7用ISAP ...