【问题标题】:Gradle spring boot - /health endpoint not respondingGradle spring boot - /health端点没有响应
【发布时间】:2019-02-03 12:32:42
【问题描述】:

我有一个 spring boot rest api 项目 - 我无法让 spring boot actuator /health 端点工作。它根本没有出现,当我尝试访问 localhost:8080/health 时,我得到一个“Whitelabel 错误页面”。我已经尝试向我的 application.properties 添加各种属性,但似乎都不起作用 - 无论如何,我不需要对执行器端点进行任何自定义映射或身份验证。下面是我的 build.gradle - 有什么建议吗?我想我错过了什么。

谢谢。

buildscript {
    repositories {
        mavenLocal()
        maven { url = 'https://artifactory.company.com/libs-release' }
        maven { url = 'https://artifactory.company.com/libs-remote' }
        maven { url = 'https://artifactory.company.com/libs-snapshot' }
    }
    dependencies {
        classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.2'
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.3.RELEASE")

    }
}

plugins {
    //id 'org.sonarqube' version '2.5'
    //id 'ch.netzwerg.release' version '1.2.5'
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'java-library'
apply plugin: 'maven-publish'
apply plugin: 'org.junit.platform.gradle.plugin'


bootJar {
    baseName = 'equipment-workorder-api'
    version =  '0.1.0'
}

// Inject test phases into the main build task
build.dependsOn('test')

jar {
    from sourceSets.main.allSource + sourceSets.test.allSource + sourceSets.test.output
}

tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}

repositories {
    mavenLocal()

    maven {
        name "libs-release"
        url "https://artifactory.company.com/libs-release"
    }
    maven {
        name "libs-snapshot"
        url "https://artifactory.company.com/libs-snapshot"
    }
    maven {
        name "libs-remote"
        url "https://artifactory.company.com/libs-remote"
    }
    maven {
        name "spring-milestones"
        url "http://repo.spring.io/milestone"
    }
    maven {
        name "jitpack.io" // this is for spring-test-junit5, remove once we upgrade to spring 5
        url "https://jitpack.io"
    }
    maven { url "http://repo.maven.apache.org/maven2" }
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    //Spring Boot
    compile group: "org.springframework.boot", name: "spring-boot-actuator", version: "1.5.8.RELEASE"
    compile group: "org.springframework.boot", name: "spring-boot-configuration-processor", version: "1.5.8.RELEASE"


    //Logging
    implementation 'org.slf4j:slf4j-api:1.7.25'
    implementation 'org.projectlombok:lombok:1.16.16'
    compile group: 'log4j', name: 'log4j', version: '1.2.17'


    //JUnit / Mockito
    testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version:'5.0.2'
    testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-params', version:'5.0.2'
    testImplementation group: 'org.mockito', name: 'mockito-all', version: '1.10.19'
    testRuntime("org.junit.jupiter:junit-jupiter-engine:5.0.2")

    //Swagger
    compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.2'
    compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2'


}


}

【问题讨论】:

  • 您是否尝试更改执行器的版本?一方面,您拥有:[...] spring-boot-gradle-plugin:2.0.3.RELEASE,而在依赖项中:[...] "spring-boot-actuator", version: "1.5.8.RELEASE"
  • 我试过这个依赖项 { classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.2' classpath("org.springframework.boot:spring-boot-gradle-plugin: 1.5.8.RELEASE") }
  • 得到同样的结果
  • 我建议将debug = true 添加到您的应用程序属性中,您应该会看到自动配置报告。您应该看到 HealthEndpointAutoConfiguration 被启用,或者如果它没有被有条件地启用的原因

标签: spring-boot spring-boot-actuator


【解决方案1】:

由于您想通过 HTTP 获取健康状态,您可能需要添加此依赖项并尝试一下:

compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '1.5.8.RELEASE'

【讨论】:

  • 在引导
  • 嗨 @DarrenForsythe 我已经使用 springboot 2.0.4.RELEASE 创建了一个示例 springboot 应用程序,但我没有看到任何 spring-boot-starter-web 传递依赖项,
  • 正如我所说,在低于 2.0 的版本中不需要它。从 Boot 2.0 开始,您需要定义依赖项,因为可以选择 webwebflux
  • 我也用springboot 1.5.14测试过。RELEASE没有web传递依赖,意味着你不能访问http
【解决方案2】:

您需要从 application.yml 文件中公开端点才能看到执行器/健康端点结果。 在 Gradle 中,我们为此使用 application.yml 文件,因此只需在此文件中写入以下配置:

management:
 endpoints:
  web:
   exposure:
    include: "*" #<2>
 endpoint:
  health:
   show-details: ALWAYS

您可以随时排除任何您希望排除的端点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-01
    • 1970-01-01
    • 2016-05-29
    • 2018-06-18
    • 2018-10-31
    • 1970-01-01
    • 2020-05-20
    相关资源
    最近更新 更多