Feign 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{

@Override
public WebFeignService create(Throwable arg0) {
	return new WebFeignService(){
		@Override
		public Object bigDataTest(Object o) {
			return arg0.getMessage();
		}
	};
}

}


作者:执意丨
来源:CSDN
原文:https://blog.csdn.net/ab52262879/article/details/87915309
版权声明:本文为博主原创文章,转载请附上博文链接!

你可能感兴趣的:(Feign FallbackFactory接口异常处理)