Σ(っ °Д °;)っ偶然看了别的项目组的fallback发现和自己的不一样让我们了解一下 您所在的位置:网站首页 hystrix降级原理 Σ(っ °Д °;)っ偶然看了别的项目组的fallback发现和自己的不一样让我们了解一下

Σ(っ °Д °;)っ偶然看了别的项目组的fallback发现和自己的不一样让我们了解一下

2023-06-01 16:37| 来源: 网络整理| 查看: 265

OpenFegin的两种降级

一、fallbackFactory 推荐:可以捕获异常信息并返回默认降级结果。可以打印堆栈信息。 二、 fallback 不推荐:不能捕获异常打印堆栈信息,不利于问题排查。

基础常识

@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 getMenu(@RequestParam("entranceSysCode") String entranceSysCode, @RequestParam("userCode") String userCode); FallbackFactory fegin调用类 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); } fegin调用降级类

fallbackFactory用法捕获异常案例此类中的@FeignClient中fallbackFactory属性指定熔断降级处理的类为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{ @Override public WebFeignService create(Throwable throwable) { log.error("异常原因:{}", throwable.getMessage(), throwable); return new WebFeignService(){ @Override public Object bigDataTest(Object o) { //出现异常,自定义返回内容,保证接口安全 return throwable.getMessage(); } }; } } Fallback(不推荐) fegin调用类 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 */ @GetMapping("/physical/PhysicalModelRecord") R getList(QueryModelRecordDTO changeDTO); /** * 物理模型变更 更新状态 * @Description 物理模型变更 更新状态 * @Method update * @Author 宋忠瑾 * @Date 2021/7/13 16:40 * @param updateDTO * @return org.springblade.core.tool.api.R */ @PutMapping("/physical/PhysicalModelRecord") R update(@RequestBody List updateDTO); } fegin调用降级类

直接实现 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 getList(QueryModelRecordDTO changeDTO) { log.error("物理模型feign >>>> 获取物理变更列表列表失败!"); return R.fail("请求失败!"); } @Override public R update(List updateDTO) { log.error("物理模型feign >>>> 更新物理变更列表状态失败!"); return R.fail("请求失败!"); } } 相关配置 feign: client: config: metaDataClient: connect-timeout: 50000 read-timeout: 50000 hystrix: enabled: true


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有