一 介绍
fallback是Hystrix命令执行失败时的后备方法,用来实现服务的降级处理。
二 HystrixCommand的服务降级示例
package com.didispace.web;
import static org.junit.Assert.*;
import java.util.concurrent.Future;
import org.junit.Test;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
public class CommandHelloFailure extends HystrixCommand<String> {
private final String name;
public CommandHelloFailure(String name) {
super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));
this.name = name;
}
@Override
protected String run() {
throw new RuntimeException("this command always fails");
}
//HystrixCommand中可以通过重载getFallback方法来实现服务降级逻辑,
//Hystrix会在run执行过程中出现错误。超时、线程池拒绝、断路器熔断等情况时,执行
//fwrFallback方法内的逻辑
@Override
protected String getFallback() {
return "Hello Failure " + name + "!";
}