springboot 学习笔记(四) 初识actuator

spring-boot-starter-actuator:html

    1、介绍:java

        Spring Boot包含许多附加功能,可帮助您在将应用程序投入生产时监视和管理应用程序。 您能够选择使用HTTP端点或JMX来管理和监控您的应用程序。 审计,健康和指标收集也能够自动应用于您的应用程序。react

    2、使用:web

        在pom中新增一个依赖spring

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

     3、进一步使用api

        他包含不少端点,咱们能够经过端点来访问具体的功能,以下缓存

        

ID Description Enabled by default

auditevents安全

Exposes audit events information for the current application.springboot

Yessession

beans

Displays a complete list of all the Spring beans in your application.

Yes

conditions

Shows the conditions that were evaluated on configuration and auto-configuration classes and the reasons why they did or did not match.

Yes

configprops

Displays a collated list of all @ConfigurationProperties.

Yes

env

Exposes properties from Spring’s ConfigurableEnvironment.

Yes

flyway

Shows any Flyway database migrations that have been applied.

Yes

health

Shows application health information.

Yes

httptrace

Displays HTTP trace information (by default, the last 100 HTTP request-response exchanges).

Yes

info

Displays arbitrary application info.

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

Shows ‘metrics’ information for the current application.

Yes

mappings

Displays a collated list of all @RequestMapping paths.

Yes

scheduledtasks

Displays the scheduled tasks in your application.

Yes

sessions

Allows retrieval and deletion of user sessions from a Spring Session-backed session store. Not available when using Spring Session’s support for reactive web applications.

Yes

shutdown

Lets the application be gracefully shutdown.

No

threaddump

Performs a thread dump.

Yes

 若是您的应用程序是一个Web应用程序(Spring MVC,Spring WebFlux或Jersey),则可使用如下附加端点:

ID Description Enabled by default

heapdump

Returns a GZip compressed hprof heap dump file.

Yes

jolokia

Exposes JMX beans over HTTP (when Jolokia is on the classpath, not available for WebFlux).

Yes

logfile

Returns the contents of the logfile (if logging.file or logging.path properties have been set). Supports the use of the HTTP Range header to retrieve part of the log file’s content.

Yes

prometheus

Exposes metrics in a format that can be scraped by a Prometheus server.

Yes

默认状况下,大部分的端点是开启的,若是想要关闭,则能够经过

management.endpoints.enabled-by-default=false

 

出于安全考虑,能够选择公开或隐藏一些端点,下面是springboot默认的端点公开状况

ID JMX Web

auditevents

Yes

No

beans

Yes

No

conditions

Yes

No

configprops

Yes

No

env

Yes

No

flyway

Yes

No

health

Yes

Yes

heapdump

N/A

No

httptrace

Yes

No

info

Yes

Yes

jolokia

N/A

No

logfile

N/A

No

loggers

Yes

No

liquibase

Yes

No

metrics

Yes

No

mappings

Yes

No

prometheus

N/A

No

scheduledtasks

Yes

No

sessions

Yes

No

shutdown

Yes

No

threaddump

Yes

No

 

要更改公开哪些端点,请使用如下技术特定的包含和排除属性:

Property Default

management.endpoints.jmx.exposure.exclude

 

management.endpoints.jmx.exposure.include

*

management.endpoints.web.exposure.exclude

 

management.endpoints.web.exposure.include

info, health

include属性列出了公开的端点的ID。 exclude属性列出了不该该公开的端点的ID。 排除属性优先于包含属性。 包含和排除属性均可以使用端点ID列表进行配置。

例如,要中止经过JMX公开全部端点并仅公开健康和信息端点,请使用如下属性:

management.endpoints.jmx.exposure.include=health,info

*可用于选择全部端点。 例如,要经过HTTP公开除env和beans端点以外的全部内容,请使用如下属性:

management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env,beans

*在YAML中有特殊含义,所以若是要包含(或排除)全部端点,请务必添加引号,如如下示例中所示:

management:
	endpoints:
		web:
			exposure:
				include: "*"

若是您但愿在暴露端点时实施您本身的策略,您能够注册一个EndpointFilter bean。

保护HTTP端点(Securing HTTP Endpoints)

您应该注意保护HTTP端点的方式与使用其余任何敏感网址的方式相同。 若是存在Spring Security,则使用Spring Security的内容协商策略默认保护端点。 例如,若是您但愿为HTTP端点配置自定义安全性,则只容许具备特定角色的用户访问它们,Spring Boot提供了一些便捷的RequestMatcher对象,能够与Spring Security结合使用。

一个典型的Spring Security配置可能看起来像下面的例子:

@Configuration
public class ActuatorSecurity extends WebSecurityConfigurerAdapter {

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http.requestMatcher(EndpointRequest.toAnyEndpoint()).authorizeRequests()
				.anyRequest().hasRole("ENDPOINT_ADMIN")
				.and()
			.httpBasic();
	}

}

上例使用EndpointRequest.toAnyEndpoint()将请求与任何端点进行匹配,而后确保全部端点都具备ENDPOINT_ADMIN角色。 EndpointRequest上还有其余几种匹配器方法。 有关详细信息,请参阅API文档(HTML或PDF)。

若是您在防火墙后面部署应用程序,您可能更喜欢全部的执行器端点均可以在无需验证的状况下进行访问。 您能够经过更改management.endpoints.web.exposure.include属性来完成此操做,以下所示

management.endpoints.web.exposure.include=*

此外,若是存在Spring Security,则须要添加自定义安全配置,以容许对端点进行未经身份验证的访问,如如下示例所示:

@Configuration
public class ActuatorSecurity extends WebSecurityConfigurerAdapter {

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http.requestMatcher(EndpointRequest.toAnyEndpoint()).authorizeRequests()
			.anyRequest().permitAll()
	}

}

4、配置端点

端点自动缓存响应以读取不带任何参数的操做。 要配置端点缓存响应的时间量,请使用其cache.time-live属性。 如下示例将Bean端点缓存的生存时间设置为10秒:

management.endpoint.beans.cache.time-to-live=10s

 

若是您想要了解更多关于 spring-boot-starter-actuator的信息,请参考官网连接

https://docs.spring.io/spring-boot/docs/2.0.3.BUILD-SNAPSHOT/actuator-api//html/

相关文章
相关标签/搜索