0

需要为其中一个项目使用断路器并为此目的使用 hystrix。但是即使在超时后也不会触发 hystrix 回退。如果遗漏了什么,请帮忙。先感谢您。

https://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-javanica

public class TestHystrix {

@HystrixCommand(fallbackMethod="fallbackCallFunc",
        commandProperties={
                @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "500")
        })
public String callFunc() throws InterruptedException{
    Thread.sleep(1050);
    return "success";
}

public String fallbackCallFunc(){
    return "default";
}

public static void main(String[] args) throws InterruptedException {
    ConfigurationManager.getConfigInstance().setProperty("hystrix.command.callFunc.execution.isolation.thread.timeoutInMilliseconds", "500");
    TestHysterix testClass = new TestHysterix();
    System.out.println(testClass.callFunc());
}
 }
4

2 回答 2

2

为了使 HystrixCommand 注释(Javanica)起作用,您需要将 Interceptor() 模块添加到您的服务代码中。

[AOP - 面向方面的编程] 功能。

工作:请注意这里方法拦截器将用于检测被调用的方法是否使用 HystrixCommand 注释进行注释,从而执行 hystrix 代码。

于 2020-03-21T18:34:19.287 回答
1

您需要为您的项目配置 Javanica。javanica wiki上有说明

要将 Javanica 与 Spring Boot 一起使用,您可以在
https://spring.io/guides/gs/circuit-breaker/找到简短指南

于 2018-01-10T09:16:37.790 回答