Spring Boot 执行器端点

Spring Boot Actuator Endpoints 允许我们监控和与我们的应用程序进行交互。 Spring Actuator 是一个 Spring Boot子模块,并提供内置的终端,我们可以为我们的应用程序启用和禁用。

Spring Boot Actuator 终端点

Spring Boot Actuator 终端点暴露于 JMX和 HTTP,大多数时候我们使用基于 HTTP 的 Actuator 终端点,因为它们通过浏览器,CURL命令,壳脚本等易于访问。

  1. 豆子:此终端返回我们应用程序中配置的所有豆子的列表
  2. env:提供有关春季环境属性的信息
  3. 健康:显示应用程序健康
  4. 信息:显示应用程序信息,我们可以在春季环境属性中配置
  5. 地图:显示所有 @RequestMapping路径的列表
  6. shutdown:允许我们礼貌地关闭应用程序
  7. threadd:提供应用程序的流量

您可以从 这里获得春运器终端的完整列表。

春季执行器终端安全

只有健康信息终端没有任何安全性,为了访问所有其他终端,我们需要配置我们的应用程序为春季安全。

启用 Spring Actuator 终端点

当我们将 Spring Actuator Dependencies 添加到我们的 Spring boot 项目时,它会自动启用 actuator 终端。

1<dependency>
2    <groupId>org.springframework.boot</groupId>
3    <artifactId>spring-boot-starter-actuator</artifactId>
4</dependency>

现在,当您运行应用程序时,您将看到执行器终端点在日志中被绘制。

12018-06-19 15:23:20.715 INFO 6493 --- [main] o.s.b.a.e.web.EndpointLinksResolver: Exposing 2 endpoint(s) beneath base path '/actuator'
22018-06-19 15:23:20.723 INFO 6493 --- [main] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/actuator/health],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)
32018-06-19 15:23:20.724 INFO 6493 --- [main] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/actuator/info],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)

Notice that only two endpoints - health and info has been mapped. Below image shows the output of these actuator endpoints. spring boot actuator endpoints health spring boot actuator endpoint info Did you noticed that there is no data in the /actuator/info, it's because we haven't configured them. Just add following properties to your application.properties file.

1info.app.name=Spring Actuator Example
2info.app.java.version=10
3info.app.type=Spring Boot

Restart the application and you will get following output: spring boot actuator info properties value

定制执行器终端点基路径

通过执行器端点的默认基路径为 /actuator',我们可以通过在应用程序属性文件中设置 management.endpoints.web.base-path 来将其更改为任何其他值。

1management.endpoints.web.base-path=/management

曝光其他 Actuator 终端

我们可以通过属性文件启用和禁用其他动作器终端,如果您想要启用所有动作器终端,请添加以下属性。

1management.endpoints.web.exposure.include=*

若要仅启用特定电动器终端点,请提供终端 id 列表。

1management.endpoints.web.exposure.include=health,info,beans,env

执行器终端的春季安全

请注意,我们需要为我们的应用程序添加 Spring Security以启用额外的终端,因为所有其他终端都至少需要基本的身份验证。

1<dependency>
2    <groupId>org.springframework.boot</groupId>
3    <artifactId>spring-boot-starter-security</artifactId>
4</dependency>

此外,在应用程序属性文件中添加春季安全用户名和密码。

1spring.security.user.name=pankaj
2spring.security.user.password=pankaj

重新启动应用程序,你会看到额外的终端点被绘制。

12018-06-19 16:18:22.211 INFO 6627 --- [main] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/management/beans],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)
22018-06-19 16:18:22.212 INFO 6627 --- [           main] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/management/env],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)
32018-06-19 16:18:22.212 INFO 6627 --- [           main] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/management/env/{toMatch}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)

Now when you will try to access the secured actuator endpoints, you will have to provide login credentials. spring boot actuator endpoint security username password Below images shows the response of beans and env/java.home endpoints. spring boot actuator endpoint beans spring boot actuator endpoint env java.home

春季动态器定制终端点

春节框架的一个很棒的特点是,它很容易扩展,我们可以使用@Endpoint对一类的注释创建自己的定制动作器终端点,然后我们必须使用@ReadOperation,@WriteOperation,或@DeleteOperation对方法的注释,以将它们作为动作器终端点。

 1package com.journaldev.spring;
 2
 3import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
 4import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
 5import org.springframework.context.annotation.Bean;
 6import org.springframework.stereotype.Component;
 7
 8@Endpoint(id="myendpoint")
 9@Component
10public class MyCustomEndpoints {
11
12    @ReadOperation
13    @Bean
14    public String hi() {
15    	return "Hi from custom endpoint";
16    }
17}

您是否注意到端点ID?我们还需要在动态器端点列表中配置此功能,以便启用。

1management.endpoints.web.exposure.include=health,info,beans,env,myendpoint

现在,当你要启动应用程序时,检查这个新的终端在日志中被绘制。

12018-06-19 17:06:59.743 INFO 6739 --- [main] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/management/myendpoint],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)

Below image shows the output when we invoke our custom actuator endpoint. spring boot actuator custom endpoint

摘要

Spring Boot Actuator 是一个生产准备的管理和信息模块,我们可以轻松地扩展,添加自己的 API 并管理我们的应用程序。

您可以从我们的 GitHub 存储库下载完整的项目。

Published At
Categories with 技术
Tagged with
comments powered by Disqus