feign整合hystrix
feign 默认是支持hystrix的, 但是在Spring - cloud Dalston 版本之后就默认关闭了, 因为不一定业务需求要用的到开启feign 对hystrix的支持
feign:
hystrix:
enabled: true #开启feign当中的hystrix
#自定义ribbon的超时时间 设置的要比hystrix-timeoutInMilliseconds超时时间大
ribbon:
ReadTimeout: 20000
#处理请求的超时时间,默认为5秒
ConnectTimeout: 20000
hystrix:
threadpool:
default:
coreSize: 5
方式1-fallback
1.创建一个类实现服务FeignClient接口@Component
public class GoodsFeignFallback implements GoodsFeignClient{
@Override
public Object getGoods() {
return ResponseResult.error("GoodsFeignFallback服务器正在维护,请稍后重试--");
}
}
2.在服务FeignClient接口上配置实现类
3.在控制器当中调用
方式2-fallbackFactory
1.创建一个类实现FallbackFactory@Component
public class GoodsFeignFallBackFactory implements FallbackFactory<GoodsFeignClient> {
@Override
public GoodsFeignClient create(Throwable throwable) {
return new GoodsFeignClient() {
@Override
public Object getGoods() {
String message = throwable.getMessage();
return ResponseResult.error(message);
}
};
}
}
2.在服务FeignClient接口上配置实现类
同方式1一致
3.在控制器当中调用
同方式1一致