• Feign调用异常触发降级捕获异常


    通过配置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
  • 相关阅读:
    LeetCode--2.两数相加
    以detectron2了解maskrcnn实现源码(0)--开篇
    SpringMVC之JSON数据返回&异常处理机制
    香港是如何形成现在的繁荣
    计算机毕业设计php+vue基于微信小程序的音乐播放器系统
    【TypeScript笔记】03 - TS类型声明文件
    Python 函数练习题
    Investment Guide|Star Investors: X METAVERSE PRO‘s Copy Trading System
    Jetson Nano 部署(3):TensorRT介绍
    java毕业生设计宠物店管理系统计算机源码+系统+mysql+调试部署+lw
  • 原文地址:https://blog.csdn.net/userenoch/article/details/133580162