在你的项目中引入Hystrix
MAVEN依赖方式如下:
group: org.springframework.cloud
artifact-id: spring-cloud-starter-hystrix

SpringBoot例子程序:

@SpringBootApplication
@EnableCircuitBreaker
public class Application {
 
    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }
 
}
 
@Component
public class StoreIntegration {
 
    @HystrixCommand(fallbackMethod = "defaultStores")
    public Object getStores(Map<String, Object> parameters) {
        //do stuff that might fail
    }
 
    public Object defaultStores(Map<String, Object> parameters) {
        return /* something useful */;
    }
}

@HystrixCommand是由一个名为“javanica”的Netflix设计库提供的。Spring Cloud自动将Spring bean封装在与Hystrix断路器连接的代理中。断路器计算何时开启和关闭电路,以及万一发生故障时该做什么。

要配置@HystrixCommand,您可以使用commandProperties属性和一个@HystrixCommand注释列表。详情请见这里。有关属性的详细信息,请参阅Hystrix wiki

英文原文:
13.1 How to Include Hystrix
To include Hystrix in your project use the starter with group org.springframework.cloud and artifact id spring-cloud-starter-hystrix. See the Spring Cloud Project page for details on setting up your build system with the current Spring Cloud Release Train.

Example boot app:

@SpringBootApplication
@EnableCircuitBreaker
public class Application {
 
    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }
 
}
 
@Component
public class StoreIntegration {
 
    @HystrixCommand(fallbackMethod = "defaultStores")
    public Object getStores(Map<String, Object> parameters) {
        //do stuff that might fail
    }
 
    public Object defaultStores(Map<String, Object> parameters) {
        return /* something useful */;
    }
}

The @HystrixCommand is provided by a Netflix contrib library called “javanica”. Spring Cloud automatically wraps Spring beans with that annotation in a proxy that is connected to the Hystrix circuit breaker. The circuit breaker calculates when to open and close the circuit, and what to do in case of a failure.

To configure the @HystrixCommand you can use the commandProperties attribute with a list of @HystrixProperty annotations. See here for more details. See the Hystrix wiki for details on the properties available.

分享到: 更多