Hystrix Feign 特定状态码不熔断
背景
部分业务校验规范或疏忽场景,传入bad request 导致接口熔断,影响接口正常流量
处理
重写Feign error decoder逻辑
1import com.netflix.hystrix.exception.HystrixBadRequestException; 2import feign.Response; 3import feign.Util; 4import feign.codec.ErrorDecoder; 5 6import static java.lang.String.format; 7 8/** 9 * @author yugj 10 * @date 2020/4/29 9:13 上午. 11 */ 12public class SkipHttpStatusErrorDecoder extends ErrorDecoder.Default { 13 14 public SkipHttpStatusErrorDecoder() { 15 super(); 16 } 17 18 @Override 19 public Exception decode(String methodKey, Response response) { 20 21 int status = response.status(); 22 if (status == 400 || status == 404) { 23 String message = statusFormat(methodKey, response); 24 return new HystrixBadRequestException(message); 25 } 26 27 return super.decode(methodKey, response); 28 } 29 30 private String statusFormat(String methodKey, Response response) { 31 32 String message = format("status %s reading %s", response.status(), methodKey); 33 if (response.body() != null) { 34 try { 35 String body = Util.toString(response.body().asReader()); 36 message += "; content:\n" + body; 37 } catch (Exception e) { 38 //do nothing 39 } 40 } 41 42 return message; 43 } 44} 45 46 47@Configuration 48public class SkipHttpStatusConfiguration { 49 @Bean 50 public SkipHttpStatusErrorDecoder errorDecoder() { 51 return new SkipHttpStatusErrorDecoder(); 52 } 53
原理
com.netflix.hystrix.AbstractCommand#executeCommandAndObserve异常控制HystrixBadRequestException没有走到熔断逻辑