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{

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

你可能感兴趣的:(springCloud,Feign,Hystrix)