当前位置:   article > 正文

Feign调用异常触发降级捕获异常_feign.feignexception降级

feign.feignexception降级

通过配置fallbackFactory来捕获异常信息,代码如下

@FeignClient(name = "user", fallbackFactory = UserFallBackFactory.class)
public interface UserFeign {

    @PostMapping("/get/list")
    Map getList();
    
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
@Component
public class UserFallBackFactory implements FallbackFactory<UserFeign> {

    @Override
    public UserFeign create(Throwable throwable) {
        // 捕获具体异常信息
        String message= FeginUtil.getMessage(throwable);
        return new UserFeign() {
            @Override
            public Map getList() {
            	 Map<String, Object> map = new HashMap<>();
                 map.put("status", 500);
                 map.put("message", message);
                 return map;
            }
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
public class FeginUtil {

    public static String getMessage(Throwable e) {
        if (e instanceof FeignException) {
            FeignException feignException  = (FeignException) e;
            String url = feignException.request().url();
            int status = feignException.status();
            String message = feignException.getMessage();
            if (status == 404) {
                return "服务未找到:" + url;
            }
            if (message.contains("Read timed out")) {
                return "服务处理请求超时:" + url;
            }
            if (message.contains("connect timed out")) {
                return "服务连接超时:" + url;
            }
        }
        
        if (e instanceof RuntimeException) {
            RuntimeException runtimeException  = (RuntimeException) e;
            String message = runtimeException.getMessage();
            if (StringUtils.isNotEmpty(message)) {
                if (message.contains("Load balancer does not have available server for client")) {
                    String[] split = message.split(":");
                    if (split.length > 2) {
                        return "没有找到可用的服务:" + split[2];
                    }
                }
                if(message.contains("[") && message.contains("]")){
                    int startIndex = message.lastIndexOf("[") + 1;
                    int endIndex = message.lastIndexOf("]");
                    String result = message.substring(startIndex, endIndex); 
                    JSONObject jsonObject = JSONObject.parseObject(result);
                    return "服务调用异常:" + jsonObject.getString("exception");
                }
            }
        }
        return "系统异常:" + e.getMessage();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/84624
推荐阅读
  

闽ICP备14008679号