Spring Boot 2.0 中的Spring Boot Actuator变化

位置:首页>文章>详情   分类: 教程分享 > Java教程   阅读(9215)   2023-03-28 11:29:14

一 Spring Boot 2.0 Actuator 讲解概述

在这篇文章中,我们将详细介绍Spring Actuator,并着重介绍Spring Boot 2.0中的一些变化。我们将讨论一些端点,并将为我们的应用程序创建一个自定义端点。
 

二 Spring Boot Actuator是什么?

Spring Boot Actuator提供多个端点以监视和与您的应用程序交互。它通过提供内置端点来实现,但您也可以构建自己的端点。在下一节中,我们将创建一个虚拟应用程序,启用Actuator端点,提供版本和构建信息,为端点添加安全性,并根据我们的需求定制端点。

三 创建项目

首先,我们将创建我们的Spring项目。与往常一样,我们通过Spring Initialzr的方式来这样做。我们选择以下依赖项:

  • Spring Boot 2.0.0
  • Java 9
  • 使用Spring MVC的Web
  • 执行器为了增加对Spring Actuator的依赖 - 当然这篇文章是关于这个的

当我们仔细研究POM时,我们注意到以下对于Spring Actuator的依赖性被添加:

< dependency >
    < groupId > org.springframework.boot </ groupId >
    < artifactId > spring-boot-starter-actuator </ artifactId >
</ dependency >
为了在我们的应用程序中有一些功能,我们创建了一个简单的RestController,它只是返回一个字符串'Hello Spring Boot Actuator'。控制器如下:
@RestController
public class HelloActuatorController {
    @RequestMapping(value = "/helloactuator", method= GET)
    public String helloActuator() {
        return "Hello Spring Boot Actuator";
    }
}
运行Spring Boot应用程序并调用URL  http://localhost:8080/helloactuator,它将在浏览器中返回字符串。

四.执行器端点

现在我们已经建立了简单的应用程序,现在是时候看看Spring Actuator提供的功能了。在浏览器中,转至URL  http:// localhost:8080/actuator /。这向我们展示了暴露的执行器端点的概览。与Spring Boot 1相比,执行器端点都位于该执行器端点之后。这应防止与自己的端点命名冲突,只要它们与Actuator端点具有相同的名称。调用URL返回以下内容:

{
    "_links": {
        "self": {
            "href": "http://localhost:8080/actuator",
            "templated": false
        },
        "health": {
            "href": "http://localhost:8080/actuator/health",
            "templated": false
        },
        "info": {
            "href": "http://localhost:8080/actuator/info",
            "templated": false
        }
    }
}
如您所见,默认情况下仅显示3个Actuator端点。为了公开更多端点,我们需要将一个包含或  排除配置添加  到  application.properties文件中。我们会将所有端点添加到配置中,但您也可以通过逗号分隔列表来限制暴露的端点。为了公开所有端点,我们将以下配置添加到  application.properties文件中:

management.endpoints.web.exposure.include=*

重新启动应用程序并再次调用URL:现在除了关闭以外的所有执行器端点都可用。

所有可用的端点都可以在Spring文档中找到:https :  //docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html

五.添加版本信息

在之前的一篇文章中,我们介绍了git commit id插件,它为我们提供了版本信息。我们将插件添加到我们的  POM中,就像我们在上一篇文章中所做的一样。确保格式设置为  properties而不是  JSON。无论何时将  git.properties文件放入输出目录的根目录,info Actuator端点将提供有关您的应用程序的版本信息。调用URL  http:// localhost:8080 / actuator / info,默认情况下显示以下信息:

{
    "git": {
        "branch": "master",
        "commit": {
            "id": "d7d7202",
            "time": "2018-03-24T13:24:51Z"
        }
    }
}
正如你所看到的,这不是我们git.properties文件的完整内容。为了添加完整的内容,我们将以下属性添加到我们的  application.properties文件并重新启动应用程序:
management.info.git.mode=full

在这一点上,我们可以看到完整的输出。

六.添加生成信息

与版本信息类似,我们可以添加构建信息。每当在META-INF目录中存在build-info.properties文件  时,info Actuator端点将显示构建信息  。为了生成  build-info.properties文件,我们将build-info目标添加  到我们  的pom中的  spring-boot-maven-plugin

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>build-info</goal>
            </goals>
        </execution>
    </executions>
</plugin>
当我们重新运行应用程序时,信息执行器端点上的构建信息可用,如下所示:
{
    "git": {...}, // 7 items
    "build": {
        "artifact": "myspringactuatorplanet",
        "name": "myspringactuatorplanet",
        "time": "2018-03-24T13:40:03.907109600Z",
        "version": "0.0.1-SNAPSHOT",
        "group": "com.mydeveloperplanet"
    }
}

七.保护端点

大多数时候,我们不希望所有人都能访问这类信息。在这种情况下,我们可以确保执行器的端点。首先,我们将Spring安全加入到  pom中

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
只要加入Spring Security,当试图访问执行器端点以及我们自己的helloActuator端点时,就已经为我们提供了登录页面  。我们可以使用用户  用户和Spring Boot日志中提供的密码进行登录。这看起来像下面这样(当然每次启动后哈希值都会改变):
Using generated security password: 8da882c3-4bbb-4e71-88c2-13399d9f0724
现在让我们假设,我们只是想确保执行器的端点和不是我们自己  hellActuator端点。根据Spring文档,我们需要添加以下配置类:
@Configuration
public class ActuatorSecurity extends WebSecurityConfigurerAdapter {
  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.requestMatcher(EndpointRequest.toAnyEndpoint()).authorizeRequests()
      .anyRequest().hasRole("ADMIN")
      .and()
      .httpBasic();
  }
}

此时,只有执行器端点已被保护。我花了一些时间才发现它仅仅是执行器的端点而不是执行器的基本路径。我认为EndpointRequest。toAnyEndpoint也会保护URL http://localhost:8080/actuator,但情况并非如此。这背后的想法是,它不是一个实际的端点。Spring引导的一个问题是:https://github.com/spring-projects/spring-boot/issue /12353。
我们仍然停留在生成的密码上。我们可以通过向执行器安全类添加userDetailsService来克服这个问题。我们这样做的方式只允许进行演示,不应该在实际的生产环境中使用。

@Bean
@Override
public UserDetailsService userDetailsService() {
    UserDetails user =
        User.withDefaultPasswordEncoder()
            .username("user")
            .password("password")
            .roles("ADMIN")
            .build();
    return new InMemoryUserDetailsManager(user);
}

执行器端点现在可通过用户  用户名 和密码  密码访问

八.执行器健康端点

执行器运行状况端点在默认情况下不显示任何细节。目前有几位内置的健康指标,所显示的信息是从这些健康指标收集的。但是默认情况下,您只会看到以下内容:

{
    "status": "UP"
}
为了查看内置的healthindicators的信息,您需要将以下行添加到application.properties中。 这样,来自检索到的健康指示器的所有信息都被显示出来。默认值  永远不会显示健康指标信息。我们选择在授权用户请求信息时显示健康指标信息。
management.endpoint.health.show-details=when-authorized
重新运行应用程序并调用Actuator运行状况端点。以下信息显示在我们的案例中:
{
    "status": "UP",
    "details": {
        "diskSpace": {
            "status": "UP",
            "details": {
                "total": 408943587328,
                "free": 75006865408,
                "threshold": 10485760
            }
        }
    }
}
很可能,您还需要创建自定义健康指标以用于监控软件。在这种情况下,您可以通过实施HealthIndicator界面来创建自定义健康指示器。在以下示例中,我们将检查调用请求时我们是处于奇数还是偶数分钟。这样,我们可以轻松测试健康指标状态的变化。当我们在一分钟内,我们将返回UP状态,当我们处于奇数分钟时,我们将返回DOWN状态。我们的自定义类OddOrEvenMinuteHealthCheck变为以下内容:
@Component
public class OddOrEvenMinuteHealthIndicator implements HealthIndicator {
    @Override
    public Health health() {
        int errorCode = 0;
        LocalTime now = LocalTime.now();
        if (now.getMinute() % 2 != 0) {
            errorCode = 1;
        }
        if (errorCode != 0) {
            return Health.down().withDetail("Error Code", errorCode).build();
        }
        return Health.up().build();
    }
}
重新运行应用程序并等待一分钟。调用执行器运行状况端点显示以下内容:
"oddOrEvenMinute": {
    "status": "DOWN",
    "details": {
        "Error Code": 1
    }
},
也可以添加自定义状态代码。假设我们希望在我们的健康检查中具有奇数和偶数的状态。我们需要将它们设置为application.properties中的标准状态代码序列。我们将在UPP状态下执行此操作:
management.health.status.order=DOWN, OUT_OF_SERVICE, UNKNOWN, ODD, EVEN, UP
我们需要做的最后一件事是改变我们ActuatorHealth类的返回状态  :
if (errorCode != 0) {
    return Health.status("ODD").withDetail("Error Code", errorCode).build();
}
return Health.status("EVEN").build();

总结

在这篇文章中,我们看了Spring Boot 2.0 Actuator。我们看到了如何启用执行器端点,如何向info执行器端点添加版本和构建信息,如何保护执行器端点,最后,如何向健康执行器端点定制一些健康指示器。
地址:https://www.leftso.com/article/445.html

相关阅读

Spring Boot 2.0,Spring框架的Spring Boot 中的Spring Boot Actuator变化讲解。并且了解如何在Spring Boot 2.0中使用Actuator...
Spring Boot 2.0 Redis整合,通过spring boot 2.0整合Redis作为spring缓存框架的实现。
Spring Boot 2.0 有哪些新特性_Spring Boot 2.0新功能,在本文中,我们将探讨为Spring Boot 2.0计划的一些更改和功能。我们还会描述这些变化如何帮助我们提高...
spring boot 2.0 security 5.0 整合,实现自定义表单登录。spring boot 2.0框架使用。
Spring Boot 2.1 新特性,已升级Spring 版本为5.1,支持servlet 4.0,支持Tomcat 9.0等等
Spring Boot 2.0 支持的Apache Camel 版本发布了_Apache Camel 2.22发布支持Spring Boot 2.0
Spring Boot 2.0 绑定properties属性资源文件 Spring Boot 2.0 读取properties配置文件值 Spring Boot 2.0获取properties配...
Spring Boot 2.0 入门 logoback配置实战教程,俗话说好马配好鞍。Spring Boot 框架从各方面给我们带来了开发效率。日志自然也不会落下。本文将讲解与Spring Bo...
java编程中spring框架5.0介绍说明/概述,spring5,spring框架,java编程