/health 下不暴露 Hystrix 状态

Hystrix status is not exposed under /health

根据 doc 我的应用程序应该在 /health 下提供 hystrix 数据。尽管断路器打开了,但我在 url 下看到的唯一东西是

{"status":"UP"}

我希望看到类似的东西

{
    "hystrix": {
        "openCircuitBreakers": [
            "somedata::somedata"
        ],
        "status": "CIRCUIT_OPEN"
    },
    "status": "UP"
}

我错过了什么?

我的build.gradle

buildscript {
    ext {
        springBootVersion = '1.5.10.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }

}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

group = 'com.somecompany'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}


ext {
    springCloudVersion = 'Edgware.SR1'
}

dependencies {
    compile('org.springframework.cloud:spring-cloud-starter-hystrix')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-actuator')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

申请class

@EnableCircuitBreaker
@SpringBootApplication
public class HystrixDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(HystrixDemoApplication.class, args);
    }
}

Hystrix 控制下的资源

@RestController
public class SomeResourceController {

    @HystrixCommand(fallbackMethod = "defaultValue", commandProperties = {@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds", value="1500")})
    @RequestMapping(path = "/resource-with-hystrix", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public String someResource(){
        URI uri = URI.create("http://localhost:8080/some-resource");
        RestOperations restTemplate = new RestTemplate();
        return restTemplate.getForObject(uri, String.class);
    }

    private String defaultValue(){
        return "local value in case of something goes wrong";
    }
}

将健康端点设置为不敏感(默认):

endpoints.health.sensitive=false

Depending on how an endpoint is exposed, the sensitive property may be used as a security hint. For example, sensitive endpoints will require a username/password when they are accessed over HTTP (or simply disabled if web security is not enabled).

来自健康端点的文档:

Shows application health information (when the application is secure, a simple ‘status’ when accessed over an unauthenticated connection or full message details when authenticated).

根据documentation

The information exposed by the health endpoint depends on the management.endpoint.health.show-details property <...> The default value is never.

因此,为了显示 hystrix 信息,您需要将 management.endpoint.health.show-details 设置为以下之一:

  • when_authorized
  • 总是

像这样:

management.endpoint.health.show-details = always

msfoster 提供的答案不再有效(从 Spring Boot 2.1.x 和云发布火车 Greenwich.RELEASE 开始):

Endpoint sensitive flag is no longer customizable as Spring Boot no longer provides a customizable security auto-configuration . Create or adapt your security configuration accordingly