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/

相關文章
相關標籤/搜索