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

项目结构:

代码:

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();
}
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);@Overridepublic RemoteRcsService create(Throwable throwable) {log.error("rcs服务失败:{}", throwable.getMessage());return new RemoteRcsService() {@Overridepublic JSONObject getRepeatWarnRuleInfo() {return null;}@Overridepublic JSONObject getDisposePlanListForOrganDispose(String warnType, String warnLevel, String zhId, String yhqk, Integer lc, String bkrs) {return null;}@Overridepublic JSONObject queryListAddressBook() {return null;}@Overridepublic JSONObject getAddressBooksByParentId(String itemParentId, String userId) {return null;}@Overridepublic JSONObject queryAddressBookByIds(String ids) {return null;}@Overridepublic JSONObject getParentSysDictDataDetail(String dictValue, String dictType) {return null;}@Overridepublic JSONObject getDisposePlanListForWarnLevenUp(String warnType, String warnLevel, String zhId, String yhqk, Integer lc, String bkrs, String rswz, Integer rsmj) {return null;}@Overridepublic JSONObject getImportantWarnNoticeList() {return null;}@Overridepublic JSONObject getCarDutyRecord(String carNumber, String startTime, String endTime) {return null;}@Overridepublic JSONObject getResImportantUnitByName(String name) {return null;}@Overridepublic JSONObject addSysNoticeNew(String json) {return null;}};}
}

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

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

Feign注解的contextId:

@FeignClient(contextId = "remoteCdsService", value = ServiceNameConstants.CDS_SERVICE, fallbackFactory = RemoteCdsFallbackFactory.class)
  • 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

Feign的调用报错时,降级处理方式,fallBackFactory相关推荐

  1. 线上问题处理-feign调用报错(Illegal character ((CTRL-CHAR, code 31)): only regular white space (\r, \n, \t) )

    线上问题处理-feign调用报错 业务场景:服务1通过Feign调用服务2,测试阶段一切正常,线上有数据丢失(为避免敏感本地简单复现了下).报错如下: 2021-12-04 13:47:47.774 ...

  2. U9接口服务被调用报错怎么查看

    当U9服务被调用时报错时我们怎么查看错误呢?  如报错如下:  在 UFSoft.UBF.View.Query.QueryContext.Translate(String oql, Globaliza ...

  3. 网站报错时,自动跳转到指定报错页(error.html)的办法

    ASP.NET在web层的web.config下进行如下配置: 当web层中的页面报错时,会自动跳转到根目录下的error.htm页面,提示用户,非常人性化. <system.web> & ...

  4. 当在 终端 中用 npm 安装 Vant 组件库时,发生“npm ERR code ERESOLVE ;npm ERRERESOLVE could not resolve;”报错时,该怎么办?

    出现的问题: 当在 终端 中用 npm 安装 Vant 组件库时,发生 npm ERR! code ERESOLVE: npm ERR! ERESOLVE could not resolve:报错时, ...

  5. Nuxt.js - 最新自定义报错、缺省、404、500 定制化 error.vue(页面、接口报错时自动跳转到该自定义页面)支持自定义文案、状态码等功能

    前言 在开发 Nuxt.js 时,当页面出错或接口后台数据返回异常时,页面就会 "直接呈现" 报错的信息. 正常情况下,当页面 404.500 或页面报错时, 前端应该 自动跳转到 ...

  6. Unity当GameObject.Find(““)获取不到对象报错时

    Unity当GameObject.Find("")获取不到对象报错时 有时候Hierarchy明明有我们需要获取的对象,但当他不启用时,我们用GameObject.Find(&qu ...

  7. 继承WebMvcConfigurer 和 WebMvcConfigurerAdapter类依然CORS报错? springboot 两种方式稳定解决跨域问题

    继承WebMvcConfigurer 和 WebMvcConfigurerAdapter类依然CORS报错???springboot 两种方式稳定解决跨域问题! 之前我写了一篇文章,来解决CORS报错 ...

  8. spring cloud整合feign和nacos报错:No Feign Client for loadBalancing defined. Did you forget to include

    Did you forget to include spring-cloud-starter-loadbalancer 问题描述 项目环境 解决方案 1.引入eureka依赖--无效 2.降低spri ...

  9. feign 第一次调用超时_feign + hystrix 降级

    feign 启用 hystrix feign 默认没有启用 hystrix,添加配置,启用 hystrix feign.hystrix.enabled=true application.yml 添加配 ...

最新文章

  1. 21个令程序员泪流满面的瞬间
  2. Hadoop集群配置(最全面总结)
  3. sed线上经典案例之-同时替换多个字符串
  4. JPA HttpMessageNotWritableException: Could not write content: Infinite recursion (StackOverflowError
  5. Qt QString 中文 char* UTF-8 QByteArray QTextCodec unicode gb2312 GBK 乱码与转码问题
  6. HTML5 多图片上传(前端+后台详解)
  7. MYSQL 更新字段,向字段追加字符串
  8. 工作所思--IT新人
  9. Android 设置闹铃步骤和基础代码
  10. pyqt5优秀项目python_【项目】PYQT5--Python/C++实现网络聊天室
  11. 计算机考研408每日一题 day161
  12. virtualbox vm 虚拟机 迁移
  13. Windows下PHP环境的搭建
  14. 金山打字通计算机英语,金山打字通英文版
  15. 上海是怎么错失这些年的互联网机遇的?
  16. CMDB建设补充:教你用django+drf 怎么去生成漂亮的API文档
  17. 掌上黄金期货通隐私网址
  18. 【认知】眼见为实吗?也许你看到的并不是真实的:视觉恒常性
  19. Android如何自定义服务器DynamicMockServer的使用
  20. Tensorflow2.0学习-Keras Tuner 妙用 (六)

热门文章

  1. Redisson之lock()和tryLock()的区别
  2. AB Test详细讲解
  3. 人人都能学会的SHELL编程
  4. 【程序2】题目:企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%;20
  5. GPS Guided Policy Search
  6. 海淀驾校比亚迪学车记--科目二考试
  7. ETL工具Talend最佳实践
  8. 深入浅出谈存储之NAS是什么
  9. python 减法函数_详解 Python 的二元算术运算,为什么说减法只是语法糖?
  10. 【Midjourney】Midjourney 基本操作 ① ( 使用 Midjourney 生成图片 | V 按钮继续生成 | U 按钮获取结果 | Midjourney 设置面板 )