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

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

java 复制代码
@FeignClient(name = "user", fallbackFactory = UserFallBackFactory.class)
public interface UserFeign {

    @PostMapping("/get/list")
    Map getList();
    
}
java 复制代码
@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;
            }
        }
    }
}
java 复制代码
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();
    }
}
相关推荐
时间的情敌27 分钟前
Three.js 射线拾取进阶:hover 高亮、点击弹窗、多物体过滤、穿透拾取
开发语言·javascript·three.js
Amumu1213843 分钟前
TypeScript学习: 基本类型(一)
开发语言·前端·typescript
敲上瘾2 小时前
Qt 常用控件:按钮、显示输入、容器与布局等
开发语言·c++·qt·qt6.3
船神丿男人2 小时前
C++ STL 入门:用 std::string 读配置,四个容易漏掉的边界
开发语言·c++
30955666614 小时前
股票接口重试怎么分类:正常、空数组与错误响应的幂等处理
java·大数据·开发语言·数据库·php·api接口·股票数据api接口
郝学胜-神的一滴15 小时前
Effective STL 条款09:慎重选择删除元素的方法
开发语言·网络·数据结构·c++·算法·stl
geovindu19 小时前
python:Logistic Regression
开发语言·后端·python·算法
摇滚侠20 小时前
《On Java 中文版 基础卷》阅读笔记 内部类 21
java·开发语言·笔记
番茄味城市甜菜20 小时前
二叉树刷题(C语言)
c语言·开发语言