Spring Boot Actuator:介绍和使用

Spring Boot Actuator提供一系列HTTP端点来暴露项目信息,用来监控和管理项目。在Maven中,能够添加如下依赖:web

<!-- 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等,这时候不用再重复添加该依赖。spring

 

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端点:session

management:
  endpoints:
    enabled-by-default: false
  endpoint:
    info:
      enabled: true

 

上面是启用/禁用(enable)某个端点,若是使某个端点暴露(exposure)出来,还须要再配置,默认状况下全部端点在JMX下是所有公开的,在Web下只公开/health和/info两个端点。下面是默认配置:app

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下公开全部端点:ide

management:
  endpoints:
    web:
      exposure:
        include: '*'

 

保护Actuator HTTP端点:spring-boot

最简单的方式,就是在pom.xml中添加spring-boot-starter-security。由SpringBoot Security的特性可知,系统会自动给咱们建立login/logout page,还有一个user和password,此外系统还会自动给我配置一个ManagementWebSecurityConfigurerAdapter(extends WebSecurityConfigurerAdapter),配置Actuator各个Endpoint的权限。ui

固然咱们也能够自定义一个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
    }
}
相关文章
相关标签/搜索