zoukankan      html  css  js  c++  java
  • Hystrix Feign 特定状态码不熔断

    背景

    部分业务校验规范或疏忽场景,传入bad request 导致接口熔断,影响接口正常流量

    处理

    重写Feign error decoder逻辑

    import com.netflix.hystrix.exception.HystrixBadRequestException;
    import feign.Response;
    import feign.Util;
    import feign.codec.ErrorDecoder;
    
    import static java.lang.String.format;
    
    /**
     * @author yugj
     * @date 2020/4/29 9:13 上午.
     */
    public class SkipHttpStatusErrorDecoder extends ErrorDecoder.Default {
    
        public SkipHttpStatusErrorDecoder() {
            super();
        }
    
        @Override
        public Exception decode(String methodKey, Response response) {
    
            int status = response.status();
            if (status == 400 || status == 404) {
                String message = statusFormat(methodKey, response);
                return new HystrixBadRequestException(message);
            }
    
            return super.decode(methodKey, response);
        }
    
        private String statusFormat(String methodKey, Response response) {
    
            String message = format("status %s reading %s", response.status(), methodKey);
            if (response.body() != null) {
                try {
                    String body = Util.toString(response.body().asReader());
                    message += "; content:
    " + body;
                } catch (Exception e) {
                    //do nothing
                }
            }
    
            return message;
        }
    }
    
    @Configuration
    public class SkipHttpStatusConfiguration {
        @Bean
        public SkipHttpStatusErrorDecoder errorDecoder() {
            return new SkipHttpStatusErrorDecoder();
        }
    
    

    原理

    com.netflix.hystrix.AbstractCommand#executeCommandAndObserve异常控制HystrixBadRequestException没有走到熔断逻辑

    转:https://my.oschina.net/yugj/blog/4257960

  • 相关阅读:
    韩信的糊涂
    用友U8两个怪问题
    写给对前途迷茫的朋友:五句话定会改变你的人生
    换博客了
    各种杀毒工具的优缺点
    又是一年中秋到 别有一般更思乡
    谁说黑夜是孤单的
    李想:创业不一定是创办企业
    SQ小组KTV点歌系统简介
    注意!在subList生成子列表之后,一定不要随便更改原列表
  • 原文地址:https://www.cnblogs.com/duanxz/p/14605515.html
Copyright © 2011-2022 走看看