在分布式系统中,每一个服务均可能会调用不少其余服务,被调用的那些服务就是依赖服务,有的时候某些依赖服务出现故障也是很正常的。
Hystrix 能够让咱们在分布式系统中对服务间的调用进行控制,加入一些调用延迟或者依赖故障的容错机制。
Hystrix 经过将依赖服务进行资源隔离,进而阻止某个依赖服务出现故障时在整个系统全部的依赖服务调用中进行蔓延;同时Hystrix 还提供故障时的 fallback 降级机制。
总而言之,Hystrix 经过这些方法帮助咱们提高分布式系统的可用性和稳定性。api
feign能够用okhttp代替传统的httpclient,性能更好app
feign: hystrix: enabled: true httpclient: enabled: false okhttp: enabled: true
ribbon这块也能够对okhttp进行配置分布式
ribbon.httpclient.enabled=false ribbon.okhttp.enabled=true
hystrix熔断器能够对请求进行重试,对超时时间进行配置等。ide
hystrix: command: default: execution: timeout: enabled: true isolation: thread: #目前有两个容器实例,单个请求超时5s,+重试>10s,超15s则熔断 timeoutInMilliseconds: 15000 ribbon: #ribbon请求链接的超时时间- 限制3秒内必须请求到服务,并不限制服务处理的返回时间 connectTimeout: 3000 #请求处理的超时时间 下级服务响应最大时间,超出时间消费方(路由也是消费方)返回timeout,超时时间不可大于断路器的超时时间 readTimeout: 5000
feign上使用fallback以前,当服务端出现问题时,能够快速响应,而且不会抛出异常,就象为feign加了一层try...cache同样。性能
开始feign.hystrix的功能code
feign: hystrix: enabled: true httpclient: enabled: false okhttp: enabled: true
当feign的服务端出现问题时,返回值为fallback的值,你能够本身定,例如:对象
@Component @Slf4j public class UserClientFallback implements UserClient { @Override public Map getUser(Long id) { logger.info("getUser.error"); return null; } @Override public Map getUsers() { logger.info("getUsers.error.retrun.default"); return ImmutableMap.of("name", "lind", "sex", "male"); } }
调用userclient失败以后,将返回默认的对象资源
@GetMapping("/api/v1/test") public ResponseEntity<?> userGet() { Map users= userClient.getUsers(); return ResponseUtils.okMessage("success"); }
结果将是默认值路由
{"status":200,"message":"操做成功","data":{"name":"lind","sex":"male"},"totalRecords":0}