【问题标题】:Does spring boot actuator no longer have pause endpoint?弹簧启动执行器是否不再具有暂停端点?
【发布时间】:2022-08-03 13:30:42
【问题描述】:

Spring Boot 2.6.6 - Actuator API Reference

我检查了上面的链接,但找不到 /actuator/pause 端点。我不确定是不是我的应用程序导致了这个问题,所以我从 spring 初始化程序创建了一个新的 MVP,即使这样,暂停端点也不存在。

我记得曾经有一个 POST 端点/actuator/pause,但是如何在较新版本的 Spring Boot(2.6.6 以上)上启用它?

下面是我的 MVP\'s 代码。

构建.gradle

plugins {
    id \'org.springframework.boot\' version \'2.6.6\'
    id \'io.spring.dependency-management\' version \'1.0.11.RELEASE\'
    id \'java\'
}

group = \'com.example\'
version = \'0.0.1-SNAPSHOT\'
sourceCompatibility = \'1.8\'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
    maven { url \'https://repo.spring.io/milestone\' }
    maven { url \'https://repo.spring.io/snapshot\' }
}

dependencies {
    implementation \'org.springframework.boot:spring-boot-starter-actuator\'
    implementation \'org.springframework.boot:spring-boot-starter-web\'
    compileOnly \'org.projectlombok:lombok\'
    annotationProcessor \'org.projectlombok:lombok\'
    testImplementation \'org.springframework.boot:spring-boot-starter-test\'
}

tasks.named(\'test\') {
    useJUnitPlatform()
}

应用程序.yml

management:
  endpoints.web.exposure.include: \'*\'
  endpoint:
    pause.enabled: true
    restart.enabled: true
    resume.enabled: true
    shutdown.enabled: true

一旦我启动应用程序并点击

curl --location --request POST \'localhost:8080/actuator/pause\',发送 404。

    标签: java spring spring-boot spring-boot-actuator spring-actuator


    【解决方案1】:

    您需要确保 Spring Cloud Commons 包含在您的项目依赖项中,因为它看起来像是提供 actuator/pause 端点 (https://docs.spring.io/spring-cloud-commons/docs/current/reference/html/#endpoints) 的库。

    正如文档另外指出的那样:

    如果你禁用 /actuator/restart 端点,那么 /actuator/pause 和 /actuator/resume 端点也将被禁用,因为它们只是 /actuator/restart 的一个特例。

    【讨论】:

      【解决方案2】:

      看起来您应该将以下依赖项添加到您的 build.gradle 文件中

      implementation 'org.springframework.cloud:spring-cloud-starter:3.1.1
      

      你可以看到RestartEndpointRestartEndpoint.PauseEndpoint 类实际上是在这个包中定义的。

      显然你应该像这样改变application.yml

      management:
        endpoints.web.exposure.include: '*'
        endpoint:
          pause:
            enabled: true
          restart:
            enabled: true
      

      【讨论】:

      • 但它什么都不做?它总是返回true,然后如果我点击/actuator/health,它总是说UP。所以基本上actuator/pause 端点不工作。
      • @theprogrammer 它可以工作,您应该创建一些实现 PauseHandler 接口的类,并使用 @Component 对其进行注释。
      • 你能添加那些代码sn-ps吗?在以前的版本中,我只需要在 application.yml 中添加一些属性,它就可以在没有任何代码更改的情况下工作。
      • @theprogrammer 你能指定哪个版本吗?谢谢。
      • 以前对我有用的 Spring Boot 版本是2.1.2.RELEASE
      【解决方案3】:

      自己支持很容易。

      @RestController
      @RequestMapping("/health")
      public class HealthController implements HealthIndicator {
      
          private Health health = Health.up().build();
      
          @RequestMapping("/pause")
          @ResponseBody
          public String pause() {
              health = Health.down().build();
              return "success";
          }
      
          @RequestMapping("/start")
          @ResponseBody
          public String start() {
              health = Health.up().build();
              return "success";
          }
      
          @Override
          public Health health() {
              return health;
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-06-17
        • 1970-01-01
        • 2018-01-07
        • 2015-03-08
        • 1970-01-01
        • 1970-01-01
        • 2017-04-21
        相关资源
        最近更新 更多