• Feign的调用报错时,降级处理方式,fallBackFactory


    Feign的调用报错时,降级处理fallBackFactory

    项目结构:

    image-20220905100225659

    代码:
    remoteCdsService
    package com.gsafety.framework.api;
    
    import com.alibaba.fastjson.JSONObject;
    import com.gsafety.framework.api.constant.ServiceNameConstants;
    import com.gsafety.framework.api.factory.RemoteRcsFallbackFactory;
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    
    /**
     * @author wzx
     * @Description 公共资源服务
     * @Date 2022/8/19 10:48
     */
    @FeignClient(contextId = "remoteRcsService", value = ServiceNameConstants.RCS_SERVICE, fallbackFactory = RemoteRcsFallbackFactory.class)
    public interface RemoteRcsService {
    
        /**
         * 获取重复警情配置
         * @return
         */
        @GetMapping("/config/v1/getRepeatWarnRuleInfo")
        public JSONObject getRepeatWarnRuleInfo();
    }
    
    
    • 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
    remoteCdsFallbackFactory
    package com.gsafety.framework.api.factory;
    
    import com.alibaba.fastjson.JSONObject;
    import com.gsafety.framework.api.RemoteRcsService;
    import feign.hystrix.FallbackFactory;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Component;
    
    /**
     * @author wzx
     * @Description 公共资源服务降级处理
     * @Date 2022/8/19 10:53
     */
    @Component
    public class RemoteRcsFallbackFactory implements FallbackFactory<RemoteRcsService> {
    
        private static final Logger log = LoggerFactory.getLogger(RemoteRcsFallbackFactory.class);
    
        @Override
        public RemoteRcsService create(Throwable throwable) {
            log.error("rcs服务失败:{}", throwable.getMessage());
            return new RemoteRcsService() {
                @Override
                public JSONObject getRepeatWarnRuleInfo() {
                    return null;
                }
    
                @Override
                public JSONObject getDisposePlanListForOrganDispose(String warnType, String warnLevel, String zhId, String yhqk, Integer lc, String bkrs) {
                    return null;
                }
    
                @Override
                public JSONObject queryListAddressBook() {
                    return null;
                }
    
                @Override
                public JSONObject getAddressBooksByParentId(String itemParentId, String userId) {
                    return null;
                }
    
                @Override
                public JSONObject queryAddressBookByIds(String ids) {
                    return null;
                }
    
                @Override
                public JSONObject getParentSysDictDataDetail(String dictValue, String dictType) {
                    return null;
                }
    
                @Override
                public JSONObject getDisposePlanListForWarnLevenUp(String warnType, String warnLevel, String zhId, String yhqk, Integer lc, String bkrs, String rswz, Integer rsmj) {
                    return null;
                }
    
                @Override
                public JSONObject getImportantWarnNoticeList() {
                    return null;
                }
    
                @Override
                public JSONObject getCarDutyRecord(String carNumber, String startTime, String endTime) {
                    return null;
                }
    
                @Override
                public JSONObject getResImportantUnitByName(String name) {
                    return null;
                }
    
                @Override
                public JSONObject addSysNoticeNew(String json) {
                    return null;
                }
            };
        }
    }
    
    
    • 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
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81

    这样子在remoteCdsService接口里面调用报错的时候,就会转到RemoteCdsFallbackFactory里面的create方法进行自定义操作。

    我这边是当报错的时候,接口直接返回Null.

    Feign注解的contextId:

    @FeignClient(contextId = "remoteCdsService", value = ServiceNameConstants.CDS_SERVICE, fallbackFactory = RemoteCdsFallbackFactory.class)
    
    • 1
    • contextId:用来唯一标识当一个微服务中存在多个FeignClient接口调用同一个服务提供者时的场景(若是不提供该属性值,则在程序启动时会启动失败,并提示如下信息


    • APPLICATION FAILED TO START


      Description:

      The bean ‘service-provider8000.FeignClientSpecification’ could not be registered. A bean with that name has already been defined and overriding is disabled.

      Action:

    n ‘service-provider8000.FeignClientSpecification’ could not be registered. A bean with that name has already been defined and overriding is disabled.

    Action:

    Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

  • 相关阅读:
    每日一题:托普利茨矩阵
    地下管廊可视化管理系统搭建
    AWS组件使用
    蓝桥杯C/C++B组历届真题刷题【合集】
    LearnOpenGL - Android OpenGL ES 3.0 基础特效实现
    斐波那契查找算法(思路分析) [Java][数据结构]
    移动WEB开发之流式布局--二倍图
    【AI】PyTorch入门(五):构建神经模型
    生产环境下Flume配置
    计算机图形学之点和直线
  • 原文地址:https://blog.csdn.net/cxywangshun/article/details/126699589