• SpringCloud+Feign+Hystrix使用FallbackFactory统一处理,查看服务调用异常或失败,进入熔断降级处理的原因


    1、 @FeignClient类

    此类中的@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",produces = MediaType.APPLICATION_JSON_VALUE)
    	public Object bigDataTest(@RequestBody Object o);
    }
     

    2、 WebFeignFallbackFactory 类

    此类实现FallbackFactory类,并实现方法T create(Throwable arg0);其中arg0.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
    public class WebFeignFallbackFactory implements FallbackFactory<WebFeignService>{
    
    	@Override
    	public WebFeignService create(Throwable arg0) {
    		return new WebFeignService(){
    			@Override
    			public Object bigDataTest(Object o) {
    				return arg0.getMessage();
    			}
    		};
    	}
    }
  • 相关阅读:
    记一次小程序支付开发的坑,超级坑
    springboot集成redis 附redis基本操作类
    springboot整合mybatis及封装curd操作-配置文件
    微信小程序开发
    vue各种插件
    java数据导出成 EXCEL
    jsp自定义标签
    java生成验证码
    文字对齐格式
    css阴影效果
  • 原文地址:https://www.cnblogs.com/javalinux/p/14343210.html
Copyright © 2020-2023  润新知