统一处理@FeignClient调用接口异常----原样抛出

第三方系统调用我方系统@FeignClient接口时报错 com.netflix.hystrix.exception.HystrixRuntimeException: WorkFlowTaskOperateService#processWorkFlowTaskSyncCallback(TaskProcessDTO) failed and no fallback available.

我方系统出现FeignException.

 第三方调用者抛出的异常:HystrixRuntimeException

一检查我们系统确实没有指定fallback和configuration,并且调用方开启了feign.hystrix.enabled: true

@FeignClient(value = "taxplan-workflow")

修改方法:

 

第三方调用在Application.java添加处理Feign异常的全局处理方法

@Bean
public Feign.Builder feignBuilder() {
    return Feign.builder().requestInterceptor(new RequestInterceptor() {
        @Override
        public void apply(RequestTemplate requestTemplate) {
            Map<String, String> customHeaders = WebUtils.getCustomHeaders();
            customHeaders.forEach((k, v) -> {
                requestTemplate.header(k, v);
            });
        }
    }).errorDecoder(new CustomErrorDecoder());
}

这里使用了RequestInterceptor拦截器,可以定制请求头,如果不想定制,可以改为

return Feign.builder().errorDecoder(new CustomErrorDecoder());
 

实现ErrorDecoder接口,其中ExceptionCode是枚举类.

public Exception decode(String methodKey, Response response) {
        if (response.status() >= 400 && response.status() <= 499) {
            return new BaseBizException(ExceptionCode.CALL_INNER_ERROR, "Client error.httpStatusCode:" + response.status());
        } else {
            if (response.status() >= 500 && response.status() <= 599 && response.body() != null) {
                try {
                    String content = CharStreams.toString(new InputStreamReader(response.body().asInputStream(), StandardCharsets.UTF_8));
                    Map responseBody = (Map) JSONObject.parseObject(content, Map.class);
                    if (responseBody.containsKey("code")) {
                        return new BaseBizException(responseBody.get("code").toString(), Objects.toString(responseBody.get("msg")));
                    }
                } catch (Exception var5) {
                }
            }

            return new BaseBizException(ExceptionCode.CALL_INNER_ERROR);
        }
    }

ExceptionCode枚举类如下:可以自定义增加删除

public enum ExceptionCode {
    ILLEGAL_STATE(4001, "非法访问"),
    PARAM_REQUIRED(4002, "参数不能为空"),
    PARAM_FORMAT_ILLEGAL(4003, "参数格式错误"),
    REQUEST_DATA_DUPLICATION(4004, "重复请求"),
    REQUEST_DATA_ERROR(4005, "请求数据错误"),
    REQUEST_DATA_NOT_MATCH(4006, "请求数据不一致"),
    RECORD_NOT_EXIST(5001, "记录不存在"),
    RECORD_EXISTED(5002, "记录已存在"),
    RECORD_ILLEGAL_STATE(5003, "数据异常"),
    BALANCE_NOT_ENOUGH(5103, "余额不足"),
    CALL_INNER_ERROR(5800, "调用内部服务接口异常"),
    THIRD_PART_ERROR(5801, "调用第三方接口异常"),
    SYSTEM_ERROR(9999, "系统异常");

    public final int code;
    public final String defaultMessage;

    private ExceptionCode(int code, String defaultMessage) {
        this.code = code;
        this.defaultMessage = defaultMessage;
    }
}

 

参考:

1.Spring Cloud Feign 熔断机制填坑

2.探讨通过Feign配合Hystrix进行调用时异常的处理

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
在Spring Cloud中,可以使用@FeignClient注解来调用远程服务接口。@FeignClient注解是一个声明式的Web服务客户端,可以将一个服务接口定义成Java接口,然后使用注解的方式来调用远程服务。 具体步骤如下: 1. 引入Feign依赖 在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` 2. 创建服务接口 创建一个Java接口,用于定义远程服务的接口方法。例如: ``` @FeignClient(name = "remote-service") public interface RemoteService { @GetMapping("/hello") String sayHello(); } ``` @FeignClient注解中的name属性指定了远程服务的名称,这个名称对应了服务注册中心中的服务名。 3. 调用远程服务 在需要调用远程服务的地方,通过@Autowired注入RemoteService接口实例,然后直接调用接口中的方法即可。 ``` @RestController public class MyController { @Autowired private RemoteService remoteService; @GetMapping("/test") public String test() { return remoteService.sayHello(); } } ``` 在上面的例子中,MyController通过调用RemoteService接口中的sayHello()方法来调用远程服务中的/hello接口。 需要注意的是,@FeignClient注解默认使用的是Spring MVC注解,因此在定义服务接口方法时需要使用Spring MVC的注解来指定请求方式、请求路径等信息。例如,在RemoteService接口中的sayHello()方法上使用@GetMapping注解来指定使用GET请求访问/hello接口

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞翔的咩咩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值