zoukankan      html  css  js  c++  java
  • 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();
    			}
    		};
    	}
    }
  • 相关阅读:
    汉诺塔实现笔记
    python-nmap的函数学习
    字符串匹配的KMP算法(转)
    QT下的贪吃蛇
    PentestBox在win10里打不开工具
    Dalvik虚拟机执行流程图
    用dx生成dex时遇到class name does not match path
    python3 小工具
    python3的Cryptodome
    前端学习笔记 day02 CSS
  • 原文地址:https://www.cnblogs.com/javalinux/p/14343210.html
Copyright © 2011-2022 走看看