如何启用" hystrix.stream"在Spring Boot 2.0中?

时间:2018-01-24 21:03:24

标签: java spring-boot hystrix

我似乎无法找到如何在Spring Boot 2.0中启用hystrix.stream。 当我尝试通过转到http://localhost:8080/hystrix.stream来访问该文件时,我收到404文件未找到错误。

控制器中调用的方法:

@GetMapping("/")
public Mono<String> index(Model model) {
    model.addAttribute("images",
            imageService
                    .findAllImages()
                    .map(image -> new HashMap<String, Object>() {{
                        put("id", image.getId());
                        put("name", image.getName());
                        put("imageComments", commentHelper.getComments(image));
                }})
    );
    return Mono.just("index");
}

CommentHelper代码,请注意正在使用@HystrixCommand:

@Component
public class CommentHelper {
    private final RestTemplate restTemplate;

    CommentHelper(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @HystrixCommand(fallbackMethod = "defaultComments")
    public List<Comment> getComments(Image image) {
        return restTemplate.exchange(
                "http://COMMENTS/comments/{imageId}",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<Comment>>() {},
                image.getId()).getBody();

    }

    public List<Comment> defaultComments(Image image) {
        return Collections.emptyList();
    }
}

这些是build.gradle的依赖项:

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-webflux'
    compile 'org.synchronoss.cloud:nio-multipart-parser'
    compile 'org.springframework.boot:spring-boot-starter-data-mongodb-reactive'
    compile 'org.springframework.boot:spring-boot-starter-thymeleaf'
    compile 'org.springframework.boot:spring-boot-starter-actuator'
    compile 'org.springframework.boot:spring-boot-devtools'

    compile 'org.springframework.cloud:spring-cloud-starter-stream-rabbit'
    compile 'org.springframework.cloud:spring-cloud-stream-reactive'

    compile 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
    compile 'org.springframework.cloud:spring-cloud-starter-netflix-hystrix'

    testCompile 'org.springframework.boot:spring-boot-starter-test'
    testCompile 'io.projectreactor:reactor-test'

    compile 'junit:junit:4.12'
}

当我转到http://localhost:8080/application/features时,您可以看到Hystrix已启用,如下所示:

{
    "enabled": [
        {
            "type": "com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect",
            "name": "Hystrix",
            "version": "1.5.12",
            "vendor": null
        },
        {
            "type": "com.netflix.discovery.EurekaClient",
            "name": "Eureka Client",
            "version": "1.8.4",
            "vendor": null
        },
        {
            "type": "org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClient",
            "name": "DiscoveryClient",
            "version": "2.0.0.M3",
            "vendor": "Pivotal Software, Inc."
        },
        {
            "type": "org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient",
            "name": "LoadBalancerClient",
            "version": "2.0.0.M3",
            "vendor": "Pivotal Software, Inc."
        },
        {
            "type": "com.netflix.ribbon.Ribbon",
            "name": "Ribbon",
            "version": "2.2.2",
            "vendor": null
        }
    ],
    "disabled": []
}

这到底出了什么问题? 如果有帮助,我会尝试按照此处的代码进行操作

https://github.com/learning-spring-boot/learning-spring-boot-2nd-edition-code/tree/master/7/part2

我正在学习Spring Spring Boot第二版。

3 个答案:

答案 0 :(得分:3)

您需要根据this issue

在application.properties中添加management.endpoints.web.exposure.include=*

答案 1 :(得分:2)

您需要在application.properties中添加management.endpoints.web.base-path=/。例如,check this out

路径必须为空,因此注册正确为/actuator/hystrix.stream

答案 2 :(得分:2)