一、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