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();
    }
}
相关推荐
for_ever_love__20 分钟前
Objective-C学习 NSSet 和 NSMutableSet 功能详解
开发语言·学习·ios·objective-c
似水明俊德6 小时前
02-C#.Net-反射-面试题
开发语言·面试·职场和发展·c#·.net
Thera7777 小时前
C++ 高性能时间轮定时器:从单例设计到 Linux timerfd 深度优化
linux·开发语言·c++
炘爚8 小时前
C语言(文件操作)
c语言·开发语言
阿蒙Amon8 小时前
C#常用类库-详解SerialPort
开发语言·c#
凸头8 小时前
CompletableFuture 与 Future 对比与实战示例
java·开发语言
wuqingshun3141598 小时前
线程安全需要保证几个基本特征
java·开发语言·jvm
Moksha2628 小时前
5G、VoNR基本概念
开发语言·5g·php
jzlhll1239 小时前
kotlin Flow first() last()总结
开发语言·前端·kotlin
W.D.小糊涂9 小时前
gpu服务器安装windows+ubuntu24.04双系统
c语言·开发语言·数据库