1.开启支持Hytrix
在application.yml中配置开启(这是需要增加的配置)
feign: hystrix: enabled: true #开启熔断支持 client: config: remote-service: #服务名,填写default为所有服务 connectTimeout: 3000 readTimeout: 3000 hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 3000
2.写入类,实现回调类
在我们定义的接口中在添加
@FeignClient(name = "USER-SERVER", fallback = HystrixClientFallback.class)
实现HystrixClientFallback
需要使用
@Component,不然会注入失败
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package cn.jiedada.Feign.impl; import cn.jiedada.Feign.UserFeginClient; import cn.jiedada.domain.User; import org.springframework.stereotype.Component; @Component public class HystrixClientFallback implements UserFeginClient { @Override public User getUserById(Long id) { return new User(-1l,"这是HystrixClientFallback的回调"); } }