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();
    }
}
相关推荐
weixin_307779132 分钟前
PySpark实现ABC_manage_channel逻辑
开发语言·python·spark
??? Meggie1 小时前
【Python】保持Selenium稳定爬取的方法(防检测策略)
开发语言·python·selenium
酷爱码3 小时前
如何通过python连接hive,并对里面的表进行增删改查操作
开发语言·hive·python
画个大饼3 小时前
Go语言实战:快速搭建完整的用户认证系统
开发语言·后端·golang
喵先生!4 小时前
C++中的vector和list的区别与适用场景
开发语言·c++
Thomas_YXQ5 小时前
Unity3D Lua集成技术指南
java·开发语言·驱动开发·junit·全文检索·lua·unity3d
xMathematics5 小时前
计算机图形学实践:结合Qt和OpenGL实现绘制彩色三角形
开发语言·c++·qt·计算机图形学·cmake·opengl
yuanManGan7 小时前
C++入门小馆: 深入了解STLlist
开发语言·c++
北极的企鹅887 小时前
XML内容解析成实体类
xml·java·开发语言
BillKu7 小时前
Vue3后代组件多祖先通讯设计方案
开发语言·javascript·ecmascript