feign常用俩种降级方式Fallback和FallbackFactory。

本文介绍了Feign客户端在服务调用中如何进行异常处理,通过对比fallbackFactory和fallback两种方式,强调了fallbackFactory能捕获异常并打印堆栈信息的优势。同时,提供了具体的代码示例,展示了如何使用fallbackFactory实现服务降级,返回默认结果,以及如何创建降级处理类。对于服务稳定性而言,fallbackFactory是更优的选择。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、fallbackFactory 推荐:可以捕获异常信息并返回默认降级结果。可以打印堆栈信息。

二、 fallback 不推荐:不能捕获异常打印堆栈信息,不利于问题排查。

1、 @FeignClient类 常识基础

url 参数不存在,value name 代表服务名称, feign寻找nacos 注册的服务名称

@FeignClient(value = “server-feign” ,fallbackFactory = WebFeignFallbackFactory.class)

url 参数存在 则使用该地址 name仅代表名称

@FeignClient(name = “menuFeign”, url = “${menuFeign.url}”,fallbackFactory = MentuFeignFallback.class)

特别注意:Feign Get请求参数名称必须定义value

    @GetMapping(value = "/blade-system/menu/routesByUserCode")
    MenuDataResponseVO<MenuVO> getMenu(@RequestParam("entranceSysCode") String entranceSysCode,
                                       @RequestParam("userCode") String userCode);

>>>>>>>>>>>>>>>>>>>>>>>>开启本文正题<<<<<<<<<<<<<<<<<<<<<<<<<
fallbackFactory用法捕获异常案例
此类中的@FeignClient中fallbackFactory属性指定熔断降级处理的类为WebFeignFallbackFactory。

package com.tianchang.wei.service.feign.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import com.tianchang.wei.service.feign.Hystric.WebFeignFallbackFactory;

@FeignClient(value = "server-feign" ,fallbackFactory = WebFeignFallbackFactory.class)
public interface WebFeignService {
	@PostMapping(value = "/bigDataTest")
	public Object bigDataTest(@RequestBody Object o);
}
 

 
2、 WebFeignFallbackFactory 降级类
注意FallbackFactory这里引入是包是hystrix
import feign.hystrix.FallbackFactory;
这是实现FallbackFactory<泛型 > implements FallbackFactory

此类实现FallbackFactory类,并实现方法T create(Throwable throwable);其中throwable.getMessage();就是服务回退时的异常信息。

package com.tianchang.wei.service.feign.Hystric;

import org.springframework.stereotype.Component;

import com.tianchang.wei.service.feign.service.WebFeignService;

import feign.hystrix.FallbackFactory;

@Component
@Slf4j
public class WebFeignFallbackFactory implements FallbackFactory<WebFeignService>{

	@Override
	public WebFeignService create(Throwable throwable) {
	        log.error("异常原因:{}", throwable.getMessage(), throwable);
		return new WebFeignService(){
			@Override
			public Object bigDataTest(Object o) {
			//出现异常,自定义返回内容,保证接口安全
				return throwable.getMessage();
			}
		};
	}
}

fallback 用法案例 不推荐

package com.infinitus.dmm.logic.service.feign;

import com.infinitus.dmm.common.constants.ApplicationNameContant;
import com.infinitus.dmm.common.entity.PageModel;
import com.infinitus.dmm.common.feign.dto.physical.QueryModelRecordDTO;
import com.infinitus.dmm.common.feign.dto.physical.UpdatePhysicalModelRecordDTO;
import com.infinitus.dmm.common.feign.vo.physical.DmmPhysicalRecordDetailVO;
import com.infinitus.dmm.logic.service.feign.fallback.PhysicalModelRecordClientFallBack;
import org.springblade.core.tool.api.R;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * @Author: 宋忠瑾
 * @Description:
 * @Date: Create in 14:22 2021/7/9
 */
@FeignClient(name = ApplicationNameContant.PHYSICAL_APPLICATION,fallback = PhysicalModelRecordClientFallBack.class)
public interface PhysicalModelRecordClient {

    /**
     * 物理模型变更列表分页
     * @Description 物理模型变更列表分页
     * @Method  getList
     * @Author  宋忠瑾
     * @Date   2021/7/13 16:41
     * @param changeDTO
     * @return <com.infinitus.dmm.common.feign.vo.physical.DmmPhysicalRecordDetailVO>
     */
    @GetMapping("/physical/PhysicalModelRecord")
    R<PageModel<DmmPhysicalRecordDetailVO>> getList(QueryModelRecordDTO changeDTO);

    /**
     * 物理模型变更 更新状态
     * @Description 物理模型变更 更新状态
     * @Method  update
     * @Author  宋忠瑾
     * @Date   2021/7/13 16:40
     * @param updateDTO
     * @return org.springblade.core.tool.api.R<java.lang.String>
     */
    @PutMapping("/physical/PhysicalModelRecord")
    R<Object> update(@RequestBody List<UpdatePhysicalModelRecordDTO> updateDTO);

}

PhysicalModelRecordClientFallBack 接口实现类 降级处理

这是直接实现 implements PhysicalModelRecordClient
package com.infinitus.dmm.logic.service.feign.fallback;

import com.infinitus.dmm.common.entity.PageModel;
import com.infinitus.dmm.common.feign.dto.physical.QueryModelRecordDTO;
import com.infinitus.dmm.common.feign.dto.physical.UpdatePhysicalModelRecordDTO;
import com.infinitus.dmm.common.feign.vo.physical.DmmPhysicalRecordDetailVO;
import com.infinitus.dmm.logic.service.feign.PhysicalModelRecordClient;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.tool.api.R;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author: 宋忠瑾
 * @Description:
 * @Date: Create in 9:28 2021/7/12
 */
@Component
@Slf4j
public class PhysicalModelRecordClientFallBack implements PhysicalModelRecordClient {

    @Override
    public R<PageModel<DmmPhysicalRecordDetailVO>> getList(QueryModelRecordDTO changeDTO) {
        log.error("物理模型feign >>>> 获取物理变更列表列表失败!");
        return R.fail("请求失败!");
    }

    @Override
    public R<Object> update(List<UpdatePhysicalModelRecordDTO> updateDTO) {
        log.error("物理模型feign >>>> 更新物理变更列表状态失败!");
        return R.fail("请求失败!");
    }
}

feign:
  client:
    config:
      metaDataClient:
        connect-timeout: 50000
        read-timeout: 50000
  hystrix:
    enabled: true 

更多细节 详细feign +hystrix 大全https://blog.youkuaiyun.com/niemingming/article/details/116795221

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值