子龙 子龙
首页
学习指南
工具
AI副业
开源项目
技术书籍

程序员子龙

Java 开发从业者
首页
学习指南
工具
AI副业
开源项目
技术书籍
  • 基础

  • JVM

  • Spring

  • 并发编程

  • Mybatis

  • 网络编程

  • 数据库

  • 缓存

  • 设计模式

  • 分布式

  • 高并发

  • SpringBoot

  • SpringCloudAlibaba

    • Spring Cloud 入门篇
    • SpringCloud 注册中心
    • SpringCloud Nacos
    • Spring Cloud Gateway
    • Spring Security 整合OAuth2
    • oauth 整合 jwt
    • 单点登录
    • Spring Cloud Feign
    • Spring Cloud Nacos Config
    • openfeign远程调用异常统一处理
      • Spring Cloud Ribbon
      • sentinel 实战
      • SpringCloud集成 报错 An attempt was made to call a method that does not exist
      • 什么是jwt
    • Nginx

    • 面试

    • 生产问题

    • 系统设计

    • 消息中间件

    • Java
    • SpringCloudAlibaba
    程序员子龙
    2024-01-29
    目录

    openfeign远程调用异常统一处理

    在目前微服务流行的年代,稍微大点的项目都会使用微服务架构模式。

    当服务提供方响应为非 2xx 状态码时, feign调用将会抛出FeignException. 由于其异常message经过了Feint的封装, 所以不再是服务提供方的原始异常信息. 若想展示原始信息则需要重写ErrorDecoder来实现。

    # 返回数据结构

    @Data
    public class ApiResponse<T> {
        /**
         * 返回码 0 为成功 其他为异常
         */
        private int code;
        /**
         * 异常提示信息
         */
        private String msg;
        /**
         * 返回的数据
         */
        private T result;
    }
    
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16

    重写ErrorDecoder

    public class FeignErrorDecoder implements ErrorDecoder {
    
    
    	@Override
    	public Exception decode(String methodKey, Response response) {
    
    		String message = null;
    		try {
    			if (response.body() != null) {
    				message = Util.toString(response.body().asReader(Util.UTF_8));
    				ApiResponse  result = JsonUtil.parse(message, ApiResponse .class);
    
    				return new ServiceException(result.getMsg());
    			}
    		} catch (Exception ignored) {
    		}
    		return new RuntimeException(message);
    	}
    
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20

    # 配置全局feign配置类

    @Configuration
    public class CustomizedConfiguration {
    
    	@Bean
    	public ErrorDecoder feignDecoder() {
    		return new FeignErrorDecoder();
    	}
    }
    
    
    1
    2
    3
    4
    5
    6
    7
    8
    9

    说明:如果不加@Configuration,需要在@FeignClient指定配置类

    在Feign进行远程调用后,需要对结果进行解码成具体的Java对象,如ApiResponse对象。而解码操作的类,必须实现Decoder接口,并重新decode方法,最后需要让其注入到spring的bean工厂中。 通过查看springcloud源代码,发现是一个叫ResponseEntityDecoder的解析器进行解码的(ResponseEntityDecoder也实现了Decoder接口),为了充分利用这个解析器,上述自定义的解析器对ResponseEntityDecoder进行了适配。

    也可以重写Decoder接口,通过状态判断,对异常信息进行捕获。

    @Component
    public class MyResponseEntityDecoder implements Decoder,SmartInitializingSingleton {
        @Autowired
        private ObjectFactory<HttpMessageConverters> messageConverters;
    
        private ResponseEntityDecoder responseEntityDecoder;
    
        @Override
        public Object decode(final Response response, Type type) throws IOException,
                FeignException {
            //返回码不为200,表示远程访问有运行时异常,则直接抛出异常即可
            if (response.status() ==200) {
                //充分利用spring框架提供的解析器
                Object result = responseEntityDecoder.decode(response, type);
                ApiResponse baseResponse = (ApiResponse)result;
                int code = baseResponse.getCode();
                if (code==0){
                    return baseResponse;
                }
                throw new ServiceException(baseResponsegetMsg());
            }
            throw new RuntimeException("异常返回");
        }
    
        @Override
        public void afterSingletonsInstantiated() {
            //初始化spring提供的解析器
            responseEntityDecoder = new ResponseEntityDecoder(new SpringDecoder(this.messageConverters));
        }
    }
    
    
    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

    # 总结

    通过重写Decoder接口或者ErrorDecoder接口,可以做全局异常处理,避免在程序中,写入大量的异常处理。

    上次更新: 2024/01/30, 15:08:57
    Spring Cloud Nacos Config
    Spring Cloud Ribbon

    ← Spring Cloud Nacos Config Spring Cloud Ribbon→

    最近更新
    01
    保姆级教程 用DeepSeek+飞书,批量写文案、写文章,太高效了
    06-06
    02
    还在为整理视频思维导图发愁?2 种超实用技巧,让你 10 分钟搞定,高效又省心!
    06-06
    03
    熬夜做PPT?AI一键生成高逼格幻灯片,效率提升10倍!
    06-06
    更多文章>
    Theme by Vdoing | Copyright © 2024-2025

        辽ICP备2023001503号-2

    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式