springboot 学习笔记(四) 初识actuator

springboot 学习笔记(四) 初识actuator

spring-boot-starter-actuator:

    一、介绍:

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

    二、使用:

        在pom中新增一个依赖

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

     三、进一步使用

        他包含很多端点,我们可以通过端点来访问具体的功能,如下

        

ID Description Enabled by default

auditevents

Exposes audit events information for the current application.

Yes

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()
	}

}

四、配置端点

端点自动缓存响应以读取不带任何参数的操作。 要配置端点缓存响应的时间量,请使用其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/

转载于:https://my.oschina.net/u/1178126/blog/1811726

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/101667.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 服务器为什么要封海外,UDP攻击是什么「建议收藏」

    服务器为什么要封海外,UDP攻击是什么「建议收藏」为什么要封海外:总所周知,目前国内的大攻击大多都来自海外,因为国外的攻击成本比国内会低很多,一旦发起了攻击,并不容易找到攻击的源头。国外的家用带宽能达到千M口,咱们国内的百M口,相当于一只外国肉鸡能顶我们国内好几台肉鸡,那这个量是不得了的,而且国内的网站几乎很少有国外用户访问,目前封海外是国内的一大趋势。UDP攻击是什么:UDP攻击全称:UDP淹没攻击(UDPFloodAttack)。UDP淹没攻击是导致主机拒绝服务的一种攻击,属于带宽类攻击。UDP是一种无连接的协议,不需要用任何程序建立连接..

    2022年10月2日
    3
  • 有监督学习VS无监督学习「建议收藏」

    有监督学习VS无监督学习「建议收藏」事先先说明一下:标签就是指的分好的类别,指明标签就是告诉计算机,这个样本属于哪一类。对于聚类的话,是事先类别都没定义好,但是类别的个数一定要告诉计算机这个问题可以回答得很简单:是否有监督(supervised),就看输入数据是否有标签(label)。输入数据有标签,则为有监督学习,没标签则为无监督学习。首先看什么是学习(learning)?一个成语就可概括:举一反三。此处以高考为例,高考的题目在上

    2022年5月25日
    49
  • 原生ajax请求的五个步骤

    原生ajax请求的五个步骤什么是ajax?通过在后台与服务器进行少量数据交换,Ajax可以使网页实现异步更新。这意味着可以在不重新加载整个网页的情况下,对网页的某部分进行更新。ajax的优点:1.实现局部更新(无刷新状态下)2.减轻了服务器端的压力ajax的缺点:1.破坏了浏览器前进和后退机制(因为ajax自动更新机制)2.一个Ajax请求多了,也会出现页面加载慢的情况。3.搜索引擎的支持程度比较低。4.ajax的安全性问题不太好(可以用数据加密解决)。注:如果要使用ajax必须要有后端环境的支持(服务器端)。

    2022年5月17日
    183
  • 基于ZigBee的工业废气监测系统

    基于ZigBee的工业废气监测系统摘要本文主要对工业现场中排放的工业废气浓度进行检测。并根据国内外气体监测技术的发展现状,提出了基于ZigBee的工业废气监测系统的设计方案。本系统主要由单片机、气体浓度检测模块、模数转换模块、显示模块、按键、蜂鸣器和无线通信模块等组成。本文首先介绍了工业废气检测系统的研究背景意义,同时结合国内外气体检测技术的发展现状,提出了基于ZigBee的工业废气监测…

    2022年4月14日
    61
  • 华为裁员风波,网络一片骂声!然而,没人懂华为的无奈「建议收藏」

    华为裁员风波,网络一片骂声!然而,没人懂华为的无奈「建议收藏」毫无疑问,今天各大媒体的头条都是华为裁员。任正非&华为《任正非:华为不奋斗就垮了,不可能养不奋斗者!》一文在朋友圈刷屏,各微信群都能看到这篇文章的身影,而今日头条等各新闻客户端的首页推荐、科技等频道充斥着“华为裁员”字眼的标题。网友对此的反映也基本一致——怒斥华为对老臣子的不公对待,压榨员工。网友的抨击然而,大部分人都没看到华为的无奈

    2022年7月18日
    43
  • Python面试题之基础篇(一)「建议收藏」

    PHP中文网为大家准备了Python面试题,本文是一些基础问题。例如:为什么学习Python、通过什么途径学习Python、谈谈对Python和其他语言的区别、简述解释型和编译型编程语言,等等。

    2022年1月18日
    95

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号